De Polignac numbers: Difference between revisions
Content added Content deleted
(Added second XPL0 example.) |
|||
Line 416: | Line 416: | ||
273421</syntaxhighlight> |
273421</syntaxhighlight> |
||
(We use 999 here for the 1000th number because 0 is the first J index.) |
(We use 999 here for the 1000th number because 0 is the first J index.) |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
<syntaxhighlight lang=jq> |
|||
# Input should be an integer |
|||
def isPrime: |
|||
. 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; |
|||
# Generate the stream of de Polignac integers: |
|||
def dePolignacs: |
|||
1, |
|||
( range(3; infinite; 2) as $n |
|||
| first( |
|||
foreach range(0; infinite) as $i (null; |
|||
if . == null then 1 else .*2 end; |
|||
if . > $n then $n |
|||
elif ($n - .) | isPrime |
|||
then -1 |
|||
else empty |
|||
end) ) |
|||
| select(.>0) ) ; |
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; |
|||
def task($n): |
|||
(label $out |
|||
| foreach dePolignacs as $i ({}; |
|||
.count += 1 |
|||
| if .count <= 50 |
|||
then .dp += [$i] |
|||
elif .count == 1000 |
|||
then .dp1000 = $i |
|||
elif .count == 10000 |
|||
then .dp10000 = $i |
|||
else . |
|||
end; |
|||
if .count == 10000 then ., break $out else empty end)) |
|||
| "The first \($n) de Polignac numbers:", |
|||
(.dp | _nwise(10) | map(lpad(4)) | join(" ")), |
|||
"\nThe 1,000th: \(.dp1000)", |
|||
"\nThe 10,000th: \(.dp10000)"; |
|||
task(50) |
|||
</syntaxhighlight> |
|||
Invocation: jq -nrc -f de-polignac.jq |
|||
{{output}} |
|||
<pre> |
|||
The first 50 de Polignac numbers: |
|||
1 127 149 251 331 337 373 509 599 701 |
|||
757 809 877 905 907 959 977 997 1019 1087 |
|||
1199 1207 1211 1243 1259 1271 1477 1529 1541 1549 |
|||
1589 1597 1619 1649 1657 1719 1759 1777 1783 1807 |
|||
1829 1859 1867 1927 1969 1973 1985 2171 2203 2213 |
|||
The 1,000th: 31941 |
|||
The 10,000th: 273421 |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |