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 such that p-k! is composite for all 1<=k!<p #
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 = 25001 000 000; # maximum number we will consider #
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;
#PR sieve theread "primes.incl.a68" toPR max # include prime utilities #
[]BOOL prime = PRIMESIEVE max prime; # sieve the primes to max prime #
PR read "primes.incl.a68" PR
[]BOOLINT primemax show = PRIMESIEVE max2 prime500;
# 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 UPBmax primeshow DO
IF is erdos prime( p ) THEN
print( ( " ", whole( p, 0 ) ) );
elast counterdos +:= 1p;
e count +:= 1
FI
OD;
print( ( newline, "Found ", whole( e count, 0 ), " Erdos primes" ) )
, " 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
END</syntaxhighlight>
{{out}}
<pre>
2 101 211 367 409 419 461 557 673 709 769 937 967 1009 1201 1259 1709 1831 1889 2141 2221 2309 2351 2411 2437
Found 25 Erdos primes up to 2500
999721 is Erdos prime 7875
</pre>
 
3,038

edits