Special neighbor primes: Difference between revisions
Content added Content deleted
m (→{{header|Julia}}: stretching) |
(Added Algol 68) |
||
Line 6: | Line 6: | ||
Find and show here in base ten if '''p<sub>1</sub>+ p<sub>2</sub> -1''' is prime, where '''p<sub>1</sub>, p<sub>2</sub> < 100'''. |
Find and show here in base ten if '''p<sub>1</sub>+ p<sub>2</sub> -1''' is prime, where '''p<sub>1</sub>, p<sub>2</sub> < 100'''. |
||
<br><br> |
<br><br> |
||
=={{header|ALGOL 68}}== |
|||
<lang algol68>BEGIN # find adjacent primes p1, p2 such that p1 + p2 - 1 is also prime # |
|||
INT max prime = 100; |
|||
# sieve the primes to max prime * 2 # |
|||
[ 1 : max prime * 2 ]BOOL prime; |
|||
prime[ 1 ] := FALSE; prime[ 2 ] := TRUE; |
|||
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 # |
|||
INT p count := 0; FOR i TO max prime DO IF prime[ i ] THEN p count +:= 1 FI OD; |
|||
# construct a list of the primes up to max prime # |
|||
[ 1 : p count ]INT low prime; |
|||
INT low pos := 0; |
|||
FOR i WHILE low pos < UPB low prime DO IF prime[ i ] THEN low prime[ low pos +:= 1 ] := i FI OD; |
|||
# find the adjacent primes p1, p2 such that p1 + p2 - 1 is prime # |
|||
FOR i TO UPB low prime - 1 DO |
|||
IF INT p1 plus p2 minus 1 = ( low prime[ i ] + low prime[ i + 1 ] ) - 1; |
|||
prime[ p1 plus p2 minus 1 ] |
|||
THEN print( ( "(", whole( low prime[ i ], -3 ) |
|||
, " +", whole( low prime[ i + 1 ], -3 ) |
|||
, " ) - 1 = ", whole( p1 plus p2 minus 1, -3 ) |
|||
, newline |
|||
) |
|||
) |
|||
FI |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
( 3 + 5 ) - 1 = 7 |
|||
( 5 + 7 ) - 1 = 11 |
|||
( 7 + 11 ) - 1 = 17 |
|||
( 11 + 13 ) - 1 = 23 |
|||
( 13 + 17 ) - 1 = 29 |
|||
( 19 + 23 ) - 1 = 41 |
|||
( 29 + 31 ) - 1 = 59 |
|||
( 31 + 37 ) - 1 = 67 |
|||
( 41 + 43 ) - 1 = 83 |
|||
( 43 + 47 ) - 1 = 89 |
|||
( 61 + 67 ) - 1 = 127 |
|||
( 67 + 71 ) - 1 = 137 |
|||
( 73 + 79 ) - 1 = 151 |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |