Find adjacent primes which differ by a square integer: Difference between revisions
Content added Content deleted
(Add Factor) |
Catskill549 (talk | contribs) (added AWK) |
||
Line 59: | Line 59: | ||
981887 - 981823 = 64 |
981887 - 981823 = 64 |
||
997877 - 997813 = 64 |
997877 - 997813 = 64 |
||
</pre> |
|||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f FIND_ADJACENTS_PRIMES_WHICH_DIFFERENCE_IS_SQUARE_INTEGER.AWK |
|||
# converted from FreeBASIC |
|||
BEGIN { |
|||
start = i = 3 |
|||
stop = 999999 |
|||
while (j <= stop) { |
|||
j = next_prime(i) |
|||
if (j-i > 36 && is_square(j-i)) { |
|||
printf("%9d %9d %9d\n",i,j,j-i) |
|||
count++ |
|||
} |
|||
i = j |
|||
} |
|||
printf("Adjacent primes which difference is square integer (>36) %d-%d: %d\n",start,stop,count) |
|||
exit(0) |
|||
} |
|||
function is_prime(n, d) { |
|||
d = 5 |
|||
if (n < 2) { return(0) } |
|||
if (n % 2 == 0) { return(n == 2) } |
|||
if (n % 3 == 0) { return(n == 3) } |
|||
while (d*d <= n) { |
|||
if (n % d == 0) { return(0) } |
|||
d += 2 |
|||
if (n % d == 0) { return(0) } |
|||
d += 4 |
|||
} |
|||
return(1) |
|||
} |
|||
function is_square(n) { |
|||
return (int(sqrt(n))^2 == n) |
|||
} |
|||
function next_prime(n, q) { # finds next prime after n |
|||
if (n == 0) { return(2) } |
|||
if (n < 3) { return(++n) } |
|||
q = n + 2 |
|||
while (!is_prime(q)) { |
|||
q += 2 |
|||
} |
|||
return(q) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
89689 89753 64 |
|||
107377 107441 64 |
|||
288583 288647 64 |
|||
367957 368021 64 |
|||
381103 381167 64 |
|||
396733 396833 100 |
|||
400759 400823 64 |
|||
445363 445427 64 |
|||
623107 623171 64 |
|||
625699 625763 64 |
|||
637003 637067 64 |
|||
710713 710777 64 |
|||
725209 725273 64 |
|||
779413 779477 64 |
|||
801883 801947 64 |
|||
803749 803813 64 |
|||
821677 821741 64 |
|||
832519 832583 64 |
|||
838249 838349 100 |
|||
844777 844841 64 |
|||
883807 883871 64 |
|||
912103 912167 64 |
|||
919447 919511 64 |
|||
954763 954827 64 |
|||
981823 981887 64 |
|||
997813 997877 64 |
|||
Adjacent primes which difference is square integer (>36) 3-999999: 26 |
|||
</pre> |
</pre> |
||