Find adjacent primes which differ by a square integer: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Raku}}: Change to be not so trivial) |
(Added Algol 68) |
||
Line 4: | Line 4: | ||
<br>Find adjacent primes under '''1,000,000''' whose difference '''(> 36)''' is a square integer. |
<br>Find adjacent primes under '''1,000,000''' whose difference '''(> 36)''' is a square integer. |
||
<br><br> |
<br><br> |
||
=={{header|ALGOL 68}}== |
|||
{{libheader|ALGOL 68-primes}} |
|||
<lang algol68>BEGIN # find a adjacent primes where the primes differ by a square > 36 # |
|||
INT min diff = 37; |
|||
INT max prime = 1 000 000; |
|||
PR read "primes.incl.a68" PR |
|||
# form a list of primes to max prime # |
|||
[]INT prime = EXTRACTPRIMESUPTO max prime FROMPRIMESIEVE PRIMESIEVE max prime; |
|||
# construct a table of squares, we will need at most the square root of max prime # |
|||
# but in reality much less than that - assume 1000 will be enough # |
|||
[ 1 : 1000 ]BOOL is square; |
|||
FOR i TO UPB is square DO is square[ i ] := FALSE OD; |
|||
FOR i WHILE INT i2 = i * i; |
|||
i * i <= UPB is square |
|||
DO |
|||
is square[ i2 ] := TRUE |
|||
OD; |
|||
# find the primes # |
|||
FOR p TO UPB prime - 1 DO |
|||
INT q = p + 1; |
|||
INT diff = prime[ q ] - prime[ p ]; |
|||
IF diff > min diff AND is square[ diff ] THEN |
|||
print( ( whole( prime[ q ], -6 ), " - ", whole( prime[ p ], -6 ), " = ", whole( diff, 0 ), newline ) ) |
|||
FI |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
89753 - 89689 = 64 |
|||
107441 - 107377 = 64 |
|||
288647 - 288583 = 64 |
|||
368021 - 367957 = 64 |
|||
381167 - 381103 = 64 |
|||
396833 - 396733 = 100 |
|||
400823 - 400759 = 64 |
|||
445427 - 445363 = 64 |
|||
623171 - 623107 = 64 |
|||
625763 - 625699 = 64 |
|||
637067 - 637003 = 64 |
|||
710777 - 710713 = 64 |
|||
725273 - 725209 = 64 |
|||
779477 - 779413 = 64 |
|||
801947 - 801883 = 64 |
|||
803813 - 803749 = 64 |
|||
821741 - 821677 = 64 |
|||
832583 - 832519 = 64 |
|||
838349 - 838249 = 100 |
|||
844841 - 844777 = 64 |
|||
883871 - 883807 = 64 |
|||
912167 - 912103 = 64 |
|||
919511 - 919447 = 64 |
|||
954827 - 954763 = 64 |
|||
981887 - 981823 = 64 |
|||
997877 - 997813 = 64 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |