Erdős-primes: Difference between revisions
→{{header|ALGOL 68}}: Added the Stretch goal
(→{{header|C}}: Updated in line with Wren example of which it is a translation.) |
(→{{header|ALGOL 68}}: Added the Stretch goal) |
||
Line 121:
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # find Erdős primes - primes p where p-k! is composite for all 1<=k!<p #
# returns TRUE if p is an Erdős prime #
PROC is erdos prime = ( INT p )BOOL:
Line 133 ⟶ 134:
result
FI # is erdos prime # ;
INT max prime =
INT max erdos = 7 875; # maximum Erdős prime to find #
# construct a table of factorials large enough for max prime #
[ 1 : 12 ]INT factorial;
factorial[ 1 ] := 1;
Line 140 ⟶ 142:
factorial[ f ] := factorial[ f - 1 ] * f
OD;
[]BOOL prime = PRIMESIEVE max prime; # sieve the primes to max prime #
# find the Erdős primes, showing the ones up to max show #
INT e count := 0;
IF is erdos prime( 2 ) THEN
Line 149 ⟶ 151:
e count +:= 1
FI;
INT last erdos := 0;
FOR p FROM 3 BY 2 TO
IF is erdos prime( p ) THEN
print( ( " ", whole( p, 0 ) ) );
e count +:= 1
FI
OD;
print( ( newline, "Found ", whole( e count, 0
, " Erdos primes up to ", whole( max show, 0 ), newline ) );
END</syntaxhighlight>▼
# find the max erdos'th Erdős prime #
FOR p FROM max show WHILE e count < max erdos DO
IF is erdos prime( p ) THEN
last erdos := p;
e count +:= 1
FI
OD;
print( ( whole( last erdos, 0 ), " is Erdos prime ", whole( e count, 0 ), newline ) )
END
{{out}}
<pre>
Found 25 Erdos primes up to 2500
999721 is Erdos prime 7875
</pre>
|