Special neighbor primes: Difference between revisions

added AWK
m (→‎{{header|REXX}}: added a comment.)
(added AWK)
Line 52:
( 67 + 71 ) - 1 = 137
( 73 + 79 ) - 1 = 151
</pre>
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SPECIAL_NEIGHBOR_PRIMES.AWK
BEGIN {
start = 3
stop = 99
old_prime = 2
for (n=start; n<=stop; n++) {
if (is_prime(n) && is_prime(old_prime)) {
sum = old_prime + n - 1
if (is_prime(sum)) {
count++
printf("%d,%d -> %d\n",old_prime,n,sum)
}
old_prime = n
}
}
printf("Special neighbor primes %d-%d: %d\n",start,stop,count)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
</lang>
{{out}}
<pre>
3,5 -> 7
5,7 -> 11
7,11 -> 17
11,13 -> 23
13,17 -> 29
19,23 -> 41
29,31 -> 59
31,37 -> 67
41,43 -> 83
43,47 -> 89
61,67 -> 127
67,71 -> 137
73,79 -> 151
Special neighbor primes 3-99: 13
</pre>
 
477

edits