Sum of two adjacent numbers are primes: Difference between revisions
Content added Content deleted
(Add Factor) |
Catskill549 (talk | contribs) (added AWK) |
||
Line 45: | Line 45: | ||
35 + 36 = 71 |
35 + 36 = 71 |
||
36 + 37 = 73 |
36 + 37 = 73 |
||
</pre> |
|||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f SUM_OF_TWO_ADJACENT_NUMBERS_ARE_PRIMES.AWK |
|||
BEGIN { |
|||
n = 1 |
|||
stop = 20 |
|||
printf("The first %d pairs of numbers whose sum is prime:\n",stop) |
|||
while (count < stop) { |
|||
if (is_prime(2*n + 1)) { |
|||
printf("%2d + %2d = %2d\n",n,n+1,2*n+1) |
|||
count++ |
|||
} |
|||
n++ |
|||
} |
|||
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) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
The first 20 pairs of numbers whose sum is prime: |
|||
1 + 2 = 3 |
|||
2 + 3 = 5 |
|||
3 + 4 = 7 |
|||
5 + 6 = 11 |
|||
6 + 7 = 13 |
|||
8 + 9 = 17 |
|||
9 + 10 = 19 |
|||
11 + 12 = 23 |
|||
14 + 15 = 29 |
|||
15 + 16 = 31 |
|||
18 + 19 = 37 |
|||
20 + 21 = 41 |
|||
21 + 22 = 43 |
|||
23 + 24 = 47 |
|||
26 + 27 = 53 |
|||
29 + 30 = 59 |
|||
30 + 31 = 61 |
|||
33 + 34 = 67 |
|||
35 + 36 = 71 |
|||
36 + 37 = 73 |
|||
</pre> |
</pre> |
||