Special neighbor primes: Difference between revisions

Content added Content deleted
(added AWK)
(→‎{{header|ALGOL 68}}: Use ALGOL 68-primes)
Line 8: Line 8:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find adjacent primes p1, p2 such that p1 + p2 - 1 is also prime #
<lang algol68>BEGIN # find adjacent primes p1, p2 such that p1 + p2 - 1 is also prime #
INT max prime = 100;
INT max prime = 100;
# sieve the primes to max prime * 2 #
# sieve the primes to max prime * 2 #
PR read "primes.incl.a68" PR
[ 1 : max prime * 2 ]BOOL prime;
prime[ 1 ] := FALSE; prime[ 2 ] := TRUE;
[]BOOL prime ( max prime * 2 );
FOR i FROM 3 BY 2 TO UPB prime DO prime[ i ] := TRUE OD;
FOR i FROM 4 BY 2 TO UPB prime DO prime[ i ] := FALSE OD;
FOR i FROM 3 BY 2 TO ENTIER sqrt( UPB prime ) DO
IF prime[ i ] THEN FOR s FROM i * i BY i + i TO UPB prime DO prime[ s ] := FALSE OD FI
OD;
# count the primes up to max prime #
# count the primes up to max prime #
INT p count := 0; FOR i TO max prime DO IF prime[ i ] THEN p count +:= 1 FI OD;
INT p count := 0; FOR i TO max prime DO IF prime[ i ] THEN p count +:= 1 FI OD;
Line 53: Line 49:
( 73 + 79 ) - 1 = 151
( 73 + 79 ) - 1 = 151
</pre>
</pre>

=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<lang AWK>