Cuban primes: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Minor tidy)
Line 33: Line 33:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN
<syntaxhighlight lang="algol68">
BEGIN
# find some cuban primes (using the definition: a prime p is a cuban prime if #
# find some cuban primes (using the definition: a prime p is a cuban prime if #
# p = n^3 - ( n - 1 )^3 #
# p = n^3 - ( n - 1 )^3 #
Line 68: Line 69:
# construct a list of the primes #
# construct a list of the primes #
[ 1 : prime count ]INT primes;
[ 1 : prime count ]INT primes;
INT prime pos := LWB primes;
INT prime pos := LWB primes - 1;
FOR s FROM LWB sieve TO UPB sieve DO
FOR s FROM LWB sieve TO UPB sieve DO
IF sieve[ s ] THEN primes[ prime pos ] := s; prime pos +:= 1 FI
IF sieve[ s ] THEN primes[ prime pos +:= 1 ] := s FI
OD;
OD;


Line 86: Line 87:
IF ODD p THEN
IF ODD p THEN
# 2 is not a cuban prime so we only test odd numbers #
# 2 is not a cuban prime so we only test odd numbers #
BOOL is prime := TRUE;
BOOL is prime := TRUE;
INT max factor = SHORTEN ENTIER long sqrt( p );
IF p <= UPB sieve THEN
FOR f FROM LWB primes WHILE is prime AND primes[ f ] <= max factor DO
is prime := sieve[ SHORTEN p ]
is prime := p MOD primes[ f ] /= 0
ELSE
INT max factor = SHORTEN ENTIER long sqrt( p );
OD;
FOR f FROM LWB primes WHILE is prime AND primes[ f ] <= max factor DO
is prime := p MOD primes[ f ] /= 0
OD
FI;
IF is prime THEN
IF is prime THEN
# have a cuban prime #
# have a cuban prime #
cuban count +:= 1;
IF ( cuban count +:= 1 ) <= print limit THEN
IF cuban count <= print limit THEN
# must show this cuban #
# must show this cuban #
STRING p formatted = commatise( p );
STRING p formatted = commatise( p );
Line 107: Line 111:
IF cuban count MOD 10 /= 0 THEN print( ( newline ) ) FI;
IF cuban count MOD 10 /= 0 THEN print( ( newline ) ) FI;
print( ( "The ", commatise( max cuban ), " cuban prime is: ", commatise( final cuban ), newline ) )
print( ( "The ", commatise( max cuban ), " cuban prime is: ", commatise( final cuban ), newline ) )
END
END</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>