Largest difference between adjacent primes: Difference between revisions
Line 5: | Line 5: | ||
Find and show on this page the largest difference between adjacent primes under 1,000,000. |
Find and show on this page the largest difference between adjacent primes under 1,000,000. |
||
<br><br> |
<br><br> |
||
=={{header|Phix}}== |
|||
{{trans|Wren}} |
|||
<!--<lang Phix>(phixonline)--> |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span> |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1e8</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1e9</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">maxI</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> |
|||
<span style="color: #000000;">maxDiff</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> |
|||
<span style="color: #004080;">atom</span> <span style="color: #000000;">nextStop</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The largest differences between adjacent primes under the following limits is:\n"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #004080;">integer</span> <span style="color: #000000;">diff</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">diff</span><span style="color: #0000FF;">></span><span style="color: #000000;">maxDiff</span> <span style="color: #008080;">then</span> |
|||
<span style="color: #000000;">maxDiff</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">diff</span> |
|||
<span style="color: #000000;">maxI</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">==</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]></span><span style="color: #000000;">nextStop</span> <span style="color: #008080;">then</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Under %,d: %,d - %,d = %,d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">nextStop</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">maxI</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">maxI</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">maxDiff</span><span style="color: #0000FF;">})</span> |
|||
<span style="color: #000000;">nextStop</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span> |
|||
<!--</lang>--> |
|||
<!--</lang>--> |
|||
{{out}} |
|||
<pre> |
|||
The largest differences between adjacent primes under the following limits is: |
|||
Under 10: 5 - 3 = 2 |
|||
Under 100: 97 - 89 = 8 |
|||
Under 1,000: 907 - 887 = 20 |
|||
Under 10,000: 9,587 - 9,551 = 36 |
|||
Under 100,000: 31,469 - 31,397 = 72 |
|||
Under 1,000,000: 492,227 - 492,113 = 114 |
|||
Under 10,000,000: 4,652,507 - 4,652,353 = 154 |
|||
Under 100,000,000: 47,326,913 - 47,326,693 = 220 |
|||
Under 1,000,000,000: 436,273,291 - 436,273,009 = 282 |
|||
"12.7s" |
|||
</pre> |
|||
<small>Almost all the time is spent constructing the list of primes. It is about 4 times slower under pwa/p2js so |
|||
limited to 1e8 when running in a browser to keep the time below 5s instead of over 50s.</small> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
Revision as of 14:29, 19 November 2021
- Task
Find and show on this page the largest difference between adjacent primes under 1,000,000.
Phix
with javascript_semantics atom t0 = time() constant limit = iff(platform()=JS?1e8:1e9) - 1 sequence primes = get_primes_le(limit) integer maxI = 0, maxDiff = 0 atom nextStop = 10 printf(1,"The largest differences between adjacent primes under the following limits is:\n") for i=2 to length(primes) do integer diff = primes[i] - primes[i-1] if diff>maxDiff then maxDiff = diff maxI = i end if if i==length(primes) or primes[i+1]>nextStop then printf(1,"Under %,d: %,d - %,d = %,d\n", {nextStop, primes[maxI], primes[maxI-1], maxDiff}) nextStop *= 10 end if end for ?elapsed(time()-t0)
- Output:
The largest differences between adjacent primes under the following limits is: Under 10: 5 - 3 = 2 Under 100: 97 - 89 = 8 Under 1,000: 907 - 887 = 20 Under 10,000: 9,587 - 9,551 = 36 Under 100,000: 31,469 - 31,397 = 72 Under 1,000,000: 492,227 - 492,113 = 114 Under 10,000,000: 4,652,507 - 4,652,353 = 154 Under 100,000,000: 47,326,913 - 47,326,693 = 220 Under 1,000,000,000: 436,273,291 - 436,273,009 = 282 "12.7s"
Almost all the time is spent constructing the list of primes. It is about 4 times slower under pwa/p2js so limited to 1e8 when running in a browser to keep the time below 5s instead of over 50s.
Raku
Built-ins
<lang perl6>for 2..8 -> $n {
printf "Largest prime gap up to {10 ** $n}: %d - between %d and %d.\n", .[0], |.[1] given max (^10**$n).grep(&is-prime).rotor(2=>-1).map({.[1]-.[0],$_})
}</lang>
- Output:
Largest prime gap up to 100: 8 - between 89 and 97. Largest prime gap up to 1000: 20 - between 887 and 907. Largest prime gap up to 10000: 36 - between 9551 and 9587. Largest prime gap up to 100000: 72 - between 31397 and 31469. Largest prime gap up to 1000000: 114 - between 492113 and 492227. Largest prime gap up to 10000000: 154 - between 4652353 and 4652507. Largest prime gap up to 100000000: 220 - between 47326693 and 47326913.
Or, significantly faster using a
Module
<lang perl6>use Math::Primesieve; my $sieve = Math::Primesieve.new;
for 2..8 -> $n {
printf "Largest prime gap up to {10 ** $n}: %d - between %d and %d.\n", .[0], |.[1] given max $sieve.primes(10 ** $n).rotor(2=>-1).map({.[1]-.[0],$_})
}</lang> Same output
Ring
<lang ring> load "stdlib.ring" see "working..." + nl limit = 1000000 Primes = [] maxOld = 0 newDiff = 0 oldDiff = 0
for n = 1 to limit
newDiff = n - maxOld if isprime(n) if newDiff > oldDiff add(Primes,[n,maxOld]) oldDiff = newDiff ok maxOld = n ok
next
len = len(Primes) diff = Primes[len][1] - Primes[len][2] see Primes[len(Primes)] see nl + "Largest difference is = " + diff + nl see "done..." + nl </lang>
- Output:
working... 492227 492113 Largest difference is = 114 done...
Wren
<lang ecmascript>import "./math" for Int, Nums import "/fmt" for Fmt
var limit = 1e9 - 1 var primes = Int.primeSieve(limit) var maxI = 0 var maxDiff = 0 var nextStop = 10 System.print("The largest differences between adjacent primes under the following limits is:") for (i in 1...primes.count) {
var diff = primes[i] - primes[i-1] if (diff > maxDiff) { maxDiff = diff maxI = i } if (i == primes.count - 1 || primes[i+1] > nextStop) { Fmt.print("Under $,d: $,d - $,d = $,d", nextStop, primes[maxI], primes[maxI-1], maxDiff) nextStop = nextStop * 10 }
}</lang>
- Output:
The largest differences between adjacent primes under the following limits is: Under 10: 5 - 3 = 2 Under 100: 97 - 89 = 8 Under 1,000: 907 - 887 = 20 Under 10,000: 9,587 - 9,551 = 36 Under 100,000: 31,469 - 31,397 = 72 Under 1,000,000: 492,227 - 492,113 = 114 Under 10,000,000: 4,652,507 - 4,652,353 = 154 Under 100,000,000: 47,326,913 - 47,326,693 = 220 Under 1,000,000,000: 436,273,291 - 436,273,009 = 282