Special neighbor primes: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 68}}: Bug fix) |
|||
Line 222: | Line 222: | ||
Found 103,611 special neighbor primes under 10,000,000. |
Found 103,611 special neighbor primes under 10,000,000. |
||
</pre> |
|||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
This entry uses `is_prime` as defined at [[Erd%C5%91s-primes#jq]]. |
|||
<lang jq># Assumes . > 2 |
|||
def next_prime: |
|||
first(range(.+2; infinite) | select(is_prime)); |
|||
def specialNP($savePairs): |
|||
. as $limit |
|||
| {p1: 2, p2: 3} |
|||
| until( .p2 >= $limit; |
|||
if (.p1 + .p2 - 1 | is_prime) |
|||
then .pcount += 1 |
|||
| if $savePairs then .neighbors = .neighbors + [[.p1, .p2]] else . end |
|||
else . |
|||
end |
|||
| .p1 = .p2 |
|||
| .p2 = (.p1|next_prime) |
|||
) |
|||
| if $savePairs then {pcount, neighbors} else {pcount} end; |
|||
100|specialNP(true)</lang> |
|||
{{out}} |
|||
<pre> |
|||
{"pcount":13,"neighbors":[[3,5],[5,7],[7,11],[11,13],[13,17],[19,23],[29,31],[31,37],[41,43],[43,47],[61,67],[67,71],[73,79]]} |
|||
</pre> |
</pre> |
||