Sum of two adjacent numbers are primes: Difference between revisions
Content added Content deleted
(Added Sidef) |
|||
Line 554: | Line 554: | ||
71=35+36 |
71=35+36 |
||
73=36+37</lang> |
73=36+37</lang> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
This entry uses a memory-less approach to computing `is_prime`, |
|||
and so a naive approach to computing the first 20 numbers satisfying |
|||
the condition is appropriate. |
|||
<lang jq>def is_prime: |
|||
. as $n |
|||
| if ($n < 2) then false |
|||
elif ($n % 2 == 0) then $n == 2 |
|||
elif ($n % 3 == 0) then $n == 3 |
|||
else 5 |
|||
| until( . <= 0; |
|||
if .*. > $n then -1 |
|||
elif ($n % . == 0) then 0 |
|||
else . + 2 |
|||
| if ($n % . == 0) then 0 |
|||
else . + 4 |
|||
end |
|||
end) |
|||
| . == -1 |
|||
end; |
|||
def naive: |
|||
limit(20; |
|||
range(0; infinite) as $i |
|||
| (2*$i + 1) as $q |
|||
| select($q | is_prime) |
|||
| "\($i) + \($i + 1) = \($q)" ); |
|||
naive</lang> |
|||
{{out}} |
|||
<pre> |
|||
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> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |