Safe and Sophie Germain primes: Difference between revisions
Content added Content deleted
(add gwbasic) |
Catskill549 (talk | contribs) (added AWK) |
||
Line 31: | Line 31: | ||
953 1013 1019 1031 1049 1103 1223 1229 1289 1409 1439 1451 |
953 1013 1019 1031 1049 1103 1223 1229 1289 1409 1439 1451 |
||
1481 1499 |
1481 1499 |
||
</pre> |
|||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f SAFE_AND_SOPHIE_GERMAIN_PRIMES.AWK |
|||
BEGIN { |
|||
limit = 50 |
|||
printf("The first %d Sophie Germain primes:\n",limit) |
|||
while (count < limit) { |
|||
if (is_prime(++i)) { |
|||
if (is_prime(i+i+1)) { |
|||
printf("%5d%1s",i,++count%10?"":"\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 50 Sophie Germain primes: |
|||
2 3 5 11 23 29 41 53 83 89 |
|||
113 131 173 179 191 233 239 251 281 293 |
|||
359 419 431 443 491 509 593 641 653 659 |
|||
683 719 743 761 809 911 953 1013 1019 1031 |
|||
1049 1103 1223 1229 1289 1409 1439 1451 1481 1499 |
|||
</pre> |
</pre> |
||