Ascending primes: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Wren)
m (→‎{{header|Wren}}: Removed unnecessary blank lines.)
Line 48: Line 48:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
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.
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>
<lang ecmascript>import "./math" for Int
import "./math" for Int
import "./sort" for Sort
import "./sort" for Sort
import "./seq" for Lst
import "./seq" for Lst
Line 71: Line 70:
ascPrimes = ascPrimes + higherPrimes
ascPrimes = ascPrimes + higherPrimes
System.print("There are %(ascPrimes.count) ascending primes, namely:")
System.print("There are %(ascPrimes.count) ascending primes, namely:")
for (chunk in Lst.chunks(ascPrimes, 10)) Fmt.print("$8d", chunk)
for (chunk in Lst.chunks(ascPrimes, 10)) Fmt.print("$8d", chunk)</lang>
</lang>


{{out}}
{{out}}

Revision as of 19:44, 8 March 2022

Ascending primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

Related

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

Wren

Library: Wren-math
Library: Wren-sort
Library: Wren-seq
Library: Wren-fmt

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 = ascPrimes + 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