De Polignac numbers: Difference between revisions
Content added Content deleted
No edit summary |
(Julia implementation) |
||
Line 637: | Line 637: | ||
The 10,000th: 273421 |
The 10,000th: 273421 |
||
</pre> |
</pre> |
||
=={{header|Julia}}== |
|||
* Needs Primes.jl |
|||
<syntaxhighlight lang="julia"> |
|||
using Primes |
|||
using Printf |
|||
function isdepolignac(n::Integer) |
|||
iseven(n) && return false |
|||
twopows = Iterators.map(x -> 2^x, 0:floor(Int, log2(n))) |
|||
return !any(twopows) do twopow |
|||
isprime(n - twopow) |
|||
end |
|||
end |
|||
function depolignacs() |
|||
naturals = Iterators.countfrom() |
|||
return Iterators.filter(isdepolignac, naturals) |
|||
end |
|||
for (i, dep) in Iterators.enumerate(depolignacs()) |
|||
if i == 1 |
|||
println("The first 50 de Polignac numbers:") |
|||
end |
|||
if i <= 50 |
|||
@printf "%4d" dep |
|||
i % 10 == 0 ? println() : print(" ") |
|||
end |
|||
if i == 50 |
|||
println() |
|||
end |
|||
if i == 1000 |
|||
println("The 1000th de Polignac number is $dep") |
|||
println() |
|||
end |
|||
if i == 10000 |
|||
println("The 10000th de Polignac number is $dep") |
|||
break |
|||
end |
|||
end |
|||
</syntaxhighlight> |
|||
<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 1000th de Polignac number is 31941 |
|||
The 10000th de Polignac number is 273421 |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |