Ascending primes: Difference between revisions
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
Thundergnat (talk | contribs) m (Add and tidy links) |
||
Line 7: | Line 7: | ||
Tip: filtering all 7,027,260 primes below 123,456,789 probably won't kill you, but there is |
Tip: filtering all 7,027,260 primes below 123,456,789 probably won't kill you, but there is |
||
at least one significantly better and much faster way, needing a mere 511 odd/prime tests. |
at least one significantly better and much faster way, needing a mere 511 odd/prime tests. |
||
;See also |
|||
;* [[oeis:A052015|OEIS:A052015 - Primes with distinct digits in ascending order]] |
|||
;Related: |
;Related: |
||
*[[ |
*[[Primes with digits in nondecreasing order]] (infinite series allowing duplicate digits, whereas this isn't and doesn't) |
||
*[[ |
*[[Pandigital prime]] (whereas this is the smallest, with gaps in the used digits being permitted) |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
Revision as of 20:22, 8 March 2022
Generate and show all primes with strictly ascending decimal digits.
Aside: Try solving without peeking at existing solutions. I had a weird idea for generating a prime sieve faster, which needless to say didn't pan out. The solution may be p(r)etty trivial but generating them quickly is at least mildly interesting. Tip: filtering all 7,027,260 primes below 123,456,789 probably won't kill you, but there is at least one significantly better and much faster way, needing a mere 511 odd/prime tests.
- See also
- Related
- Primes with digits in nondecreasing order (infinite series allowing duplicate digits, whereas this isn't and doesn't)
- Pandigital prime (whereas this is the smallest, with gaps in the used digits being permitted)
ALGOL 68
Uses Pete's hint to enumerate the 512 possible numbers.
The numbers are generated in order of the first digit, so we have to sort them.
As there are only 512 possible numbers to consider, it doesn't attempt the optimisation that the final digit can't be 4, 6 or 8 and can only be 2 or 5 if it is the only digit (also, I always forget that can't be even thing...).
<lang algol68>BEGIN # find all primes with strictly increasing digits #
PR read "primes.incl.a68" PR # include prime utilities # PR read "rows.incl.a68" PR # include array utilities # [ 1 : 512 ]INT primes; # there will be at most 512 (2^9) primes # INT p count := 0; # number of primes found so far # FOR d1 FROM 0 TO 1 DO INT n1 = d1; FOR d2 FROM 0 TO 1 DO INT n2 = IF d2 = 1 THEN ( n1 * 10 ) + 2 ELSE n1 FI; FOR d3 FROM 0 TO 1 DO INT n3 = IF d3 = 1 THEN ( n2 * 10 ) + 3 ELSE n2 FI; FOR d4 FROM 0 TO 1 DO INT n4 = IF d4 = 1 THEN ( n3 * 10 ) + 4 ELSE n3 FI; FOR d5 FROM 0 TO 1 DO INT n5 = IF d5 = 1 THEN ( n4 * 10 ) + 5 ELSE n4 FI; FOR d6 FROM 0 TO 1 DO INT n6 = IF d6 = 1 THEN ( n5 * 10 ) + 6 ELSE n5 FI; FOR d7 FROM 0 TO 1 DO INT n7 = IF d7 = 1 THEN ( n6 * 10 ) + 7 ELSE n6 FI; FOR d8 FROM 0 TO 1 DO INT n8 = IF d8 = 1 THEN ( n7 * 10 ) + 8 ELSE n7 FI; FOR d9 FROM 0 TO 1 DO INT n9 = IF d9 = 1 THEN ( n8 * 10 ) + 9 ELSE n8 FI; IF n9 > 0 THEN IF is probably prime( n9 ) THEN # have a prime with strictly ascending digits # primes[ p count +:= 1 ] := n9 FI FI OD OD OD OD OD OD OD OD OD; QUICKSORT primes FROMELEMENT 1 TOELEMENT p count; # sort the primes # FOR i TO p count DO # display the primes # print( ( " ", whole( primes[ i ], -8 ) ) ); IF i MOD 10 = 0 THEN print( ( newline ) ) FI OD
END</lang>
- Output:
2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
Phix
with javascript_semantics function ascending_primes(sequence res, atom p=0) for d=remainder(p,10)+1 to 9 do integer np = p*10+d if odd(d) and is_prime(np) then res &= np end if res = ascending_primes(res,np) end for return res end function sequence r = apply(true,sprintf,{{"%8d"},sort(ascending_primes({2}))}) printf(1,"There are %,d ascending primes:\n%s\n",{length(r),join_by(r,1,10," ")})
- Output:
There are 100 ascending primes: 2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
Raku
<lang perl6>put (
flat 2, 3, 5, 7, sort +*, gather { (1 .. 9).map: { recurse $_ }; sub recurse ($str) { .take for ($str X~ (1, 3, 7, 9)).grep: { .is-prime && [<] .comb }; my $digit = $str.substr: *-1; do for $digit ^.. 9 { recurse $str ~ $_ }; } }
).batch(10)».fmt("%8d").join: "\n";
printf "%.3f seconds", now - INIT now;</lang>
- Output:
2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789 0.088 seconds
Wren
Although they use a lot of memory, sieves usually produce good results in Wren and here we only need to sieve for primes up to 3456789 as there are just 9 possible candidates with 8 digits and 1 possible candidate with 9 digits which we can test for primality individually. The following runs in around 0.5 seconds. <lang ecmascript>import "./math" for Int import "./sort" for Sort import "./seq" for Lst import "./fmt" for Fmt
var higherPrimes = [] var candidates = [
23456789, 13456789, 12456789, 12356789, 12346789, 12345789, 12345689, 12345679, 12345678, 123456789
] for (cand in candidates) if (Int.isPrime(cand)) higherPrimes.add(cand) higherPrimes.sort()
var primes = Int.primeSieve(3456789) var ascPrimes = [] for (p in primes) {
var digits = Int.digits(p) if (Sort.isSorted(digits) && Lst.distinct(digits).count == digits.count) ascPrimes.add(p)
}
ascPrimes.addAll(higherPrimes) System.print("There are %(ascPrimes.count) ascending primes, namely:") for (chunk in Lst.chunks(ascPrimes, 10)) Fmt.print("$8d", chunk)</lang>
- Output:
There are 100 ascending primes, namely: 2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789
XPL0
Brute force solution: 4.3 seconds on Pi4. <lang XPL0>func IsPrime(N); \Return 'true' if N is prime int N, I; [if N <= 2 then return N = 2; if (N&1) = 0 then \even >2\ return false; for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false; I:= I+1; ];
return true; ];
func Ascending(N); \Return 'true' if digits are ascending int N, D; [N:= N/10; D:= rem(0); while N do
[N:= N/10; if rem(0) >= D then return false; D:= rem(0); ];
return true; ];
int Cnt, N; [Cnt:= 0; Format(9, 0); for N:= 2 to 123_456_789 do
if Ascending(N) then if IsPrime(N) then [RlOut(0, float(N)); Cnt:= Cnt+1; if rem(Cnt/10) = 0 then CrLf(0); ];
]</lang>
- Output:
2 3 5 7 13 17 19 23 29 37 47 59 67 79 89 127 137 139 149 157 167 179 239 257 269 347 349 359 367 379 389 457 467 479 569 1237 1249 1259 1279 1289 1367 1459 1489 1567 1579 1789 2347 2357 2389 2459 2467 2579 2689 2789 3457 3467 3469 4567 4679 4789 5689 12347 12379 12457 12479 12569 12589 12689 13457 13469 13567 13679 13789 15679 23459 23567 23689 23789 25679 34589 34679 123457 123479 124567 124679 125789 134789 145679 234589 235679 235789 245789 345679 345689 1234789 1235789 1245689 1456789 12356789 23456789