Ascending primes: Difference between revisions

Content added Content deleted
(→‎J: simplify)
m (→‎{{header|Wren}}: Minor tidy)
Line 2,352: Line 2,352:
===Version 1 (Sieve)===
===Version 1 (Sieve)===
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.43 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.43 seconds.
<syntaxhighlight lang="ecmascript">import "./math" for Int
<syntaxhighlight lang="wren">import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 2,377: Line 2,376:
ascPrimes.addAll(higherPrimes)
ascPrimes.addAll(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)</syntaxhighlight>
Fmt.tprint("$8d", ascPrimes, 10)</syntaxhighlight>


{{out}}
{{out}}
Line 2,398: Line 2,397:


Much quicker than the 'sieve' approach at 0.013 seconds. I also tried using a powerset but that was slightly slower at 0.015 seconds.
Much quicker than the 'sieve' approach at 0.013 seconds. I also tried using a powerset but that was slightly slower at 0.015 seconds.
<syntaxhighlight lang="ecmascript">import "./set" for Set
<syntaxhighlight lang="wren">import "./set" for Set
import "./math" for Int
import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 2,423: Line 2,421:
ascPrimes.sort()
ascPrimes.sort()
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("$8s", chunk)</syntaxhighlight>
Fmt.tprint("$8d", ascPrimes, 10)</syntaxhighlight>


{{out}}
{{out}}