Largest difference between adjacent primes: Difference between revisions
Content added Content deleted
(add gwbasic) |
(Added Algol 68) |
||
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|ALGOL 68}}== |
|||
Note, to run this with Algol 68G, the command line option <code>--heap 256M</code> whould be specified on the command line to allow a larger heap size.<br> |
|||
As with the Wren, Phix, etc. samples, shows the gaps for at a few other places. |
|||
{{libheader|ALGOL 68-primes}} |
|||
<lang algol68>BEGIN # find the largest gap between adjacent primes up to 10 000 000 # |
|||
PR read "primes.incl.a68" PR |
|||
[]BOOL prime = PRIMESIEVE 10 000 000; # sieve the primes to 10 000 000 # |
|||
# find the largest gap between primes # |
|||
INT max gap := 0; # gap between 2 and 3 # |
|||
INT max prime := 0; # the prime with the maximum gap # |
|||
INT max prev := 0; # the prime before the maximum gap # |
|||
INT prev prime := 2; # the previous prime # |
|||
INT power of 10 := 10; # next number to print the gap for # |
|||
FOR i FROM 3 BY 2 TO UPB prime DO |
|||
IF prime[ i ] THEN |
|||
# have a prime # |
|||
INT gap = i - prev prime; |
|||
IF gap > max gap THEN |
|||
# have a bigger gap than before # |
|||
max prime := i; |
|||
max prev := prev prime; |
|||
max gap := gap |
|||
FI; |
|||
prev prime := i |
|||
FI; |
|||
IF i = power of 10 - 1 THEN |
|||
# have reached the next power of 10 - report the max gap so far # |
|||
print( ( "Largest gap between primes up to ", whole( power of 10, -9 ) |
|||
, ": ", whole( max gap, -9 ), " between " |
|||
, whole( max prev, 0 ), " and ", whole( max prime, 0 ) |
|||
, newline |
|||
) |
|||
); |
|||
power of 10 *:= 10 |
|||
FI |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
Largest gap between primes up to 10: 2 between 3 and 5 |
|||
Largest gap between primes up to 100: 8 between 89 and 97 |
|||
Largest gap between primes up to 1000: 20 between 887 and 907 |
|||
Largest gap between primes up to 10000: 36 between 9551 and 9587 |
|||
Largest gap between primes up to 100000: 72 between 31397 and 31469 |
|||
Largest gap between primes up to 1000000: 114 between 492113 and 492227 |
|||
Largest gap between primes up to 10000000: 154 between 4652353 and 4652507 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |