Twin primes: Difference between revisions
Content deleted Content added
C++ program allows limits to be specified on command line |
→{{header|ALGOL 68}}: Obvious optimisation and remove unnecessary comments |
||
Line 44: | Line 44: | ||
PROC sieve = ( REF[]BOOL s )VOID: |
PROC sieve = ( REF[]BOOL s )VOID: |
||
BEGIN |
BEGIN |
||
# start with everything flagged as prime # |
|||
FOR i TO UPB s DO s[ i ] := TRUE OD; |
FOR i TO UPB s DO s[ i ] := TRUE OD; |
||
# sieve out the non-primes # |
|||
s[ 1 ] := FALSE; |
s[ 1 ] := FALSE; |
||
FOR i FROM 2 TO ENTIER sqrt( UPB s ) DO |
FOR i FROM 2 TO ENTIER sqrt( UPB s ) DO |
||
Line 63: | Line 61: | ||
# note 2 cannot be one of the primes in a twin prime pair, so we start at 3 # |
# note 2 cannot be one of the primes in a twin prime pair, so we start at 3 # |
||
INT twin count := 0; |
INT twin count := 0; |
||
FOR p FROM 3 TO max number DO IF primes[ p ] AND primes[ p - 2 ]THEN twin count +:= 1 FI OD; |
FOR p FROM 3 BY 2 TO max number DO IF primes[ p ] AND primes[ p - 2 ] THEN twin count +:= 1 FI OD; |
||
print( ( "twin prime pairs below ", whole( max number, 0 ), ": ", whole( twin count, 0 ), newline ) ) |
print( ( "twin prime pairs below ", whole( max number, 0 ), ": ", whole( twin count, 0 ), newline ) ) |
||
END</lang> |
END</lang> |