Jump to content

Cousin primes: Difference between revisions

→‎{{header|ALGOL 68}}: Use ALGOL 68-primes
(Added Swift solution)
(→‎{{header|ALGOL 68}}: Use ALGOL 68-primes)
Line 76:
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<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;
#PR sieve theread "primes.incl.a68" to max number #PR
[]BOOL 1prime := maxPRIMESIEVE number ]BOOL prime1000;
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 #
PROC right pad = ( STRING text, INT length )STRING:
Line 97 ⟶ 90:
# look through the primes for cousins #
INT p count := 0;
FOR i TO maxUPB numberprime - 4 DO
IF prime[ i ] THEN
IF prime[ i + 4 ] THEN
3,038

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.