Cousin primes: Difference between revisions

Content added Content deleted
(Added Swift solution)
(→‎{{header|ALGOL 68}}: Use ALGOL 68-primes)
Line 76: Line 76:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find cousin primes - pairs of primes that differ by 4 #
<lang algol68>BEGIN # find cousin primes - pairs of primes that differ by 4 #
# sieve the primes as required by the task #
INT max number = 1000;
# sieve the primes to max number #
PR read "primes.incl.a68" PR
[ 1 : max number ]BOOL prime;
[]BOOL prime = PRIMESIEVE 1000;
prime[ 1 ] := FALSE; prime[ 2 ] := TRUE;
FOR i FROM 3 BY 2 TO max number DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO max number DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( max number ) DO
IF prime[ i ] THEN
FOR s FROM i * i BY i + i TO max number DO prime[ s ] := FALSE OD
FI
OD;
# returns text right padded to length, if it is shorter #
# returns text right padded to length, if it is shorter #
PROC right pad = ( STRING text, INT length )STRING:
PROC right pad = ( STRING text, INT length )STRING:
Line 97: Line 90:
# look through the primes for cousins #
# look through the primes for cousins #
INT p count := 0;
INT p count := 0;
FOR i TO max number - 4 DO
FOR i TO UPB prime - 4 DO
IF prime[ i ] THEN
IF prime[ i ] THEN
IF prime[ i + 4 ] THEN
IF prime[ i + 4 ] THEN