Prime reciprocal sum: Difference between revisions
Content added Content deleted
(→{{header|J}}: append Free Pascal) |
m (moved) |
||
Line 138: | Line 138: | ||
11390125639471674628..31060548964273180103 (2358 digits) |
11390125639471674628..31060548964273180103 (2358 digits) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Julia}}== |
|||
<syntaxhighlight lang="julia">""" rosettacode.org/wiki/Prime_reciprocal_sum """ |
|||
using Primes |
|||
using ResumableFunctions |
|||
""" Abbreviate a large string by showing beginning / end and number of chars """ |
|||
function abbreviate(s; ds = "digits", t = 40, x = (t - 1) ÷ 2) |
|||
wid = length(s) |
|||
return wid < t ? s : s[begin:begin+x] * ".." * s[end-x:end] * " ($wid $ds)" |
|||
end |
|||
@resumable function generate_oeis75442() |
|||
psum = big"0" // big"1" |
|||
while true |
|||
n = BigInt(ceil(big"1" // (1 - psum))) |
|||
while true |
|||
n = nextprime(n + 1) |
|||
if psum + 1 // n < 1 |
|||
psum += 1 // n |
|||
@yield n |
|||
break |
|||
end |
|||
end |
|||
end |
|||
end |
|||
for (i, n) in enumerate(Iterators.take(generate_oeis75442(), 17)) |
|||
println(lpad(i, 2), ": ", abbreviate(string(n))) |
|||
end |
|||
</syntaxhighlight>{{out}} |
|||
<pre> |
|||
1: 2 |
|||
2: 3 |
|||
3: 7 |
|||
4: 43 |
|||
5: 1811 |
|||
6: 654149 |
|||
7: 27082315109 |
|||
8: 153694141992520880899 |
|||
9: 337110658273917297268061074384231117039 |
|||
10: 84241975970641143191..13803869133407474043 (76 digits) |
|||
11: 20300753813848234767..91313959045797597991 (150 digits) |
|||
12: 20323705381471272842..21649394434192763213 (297 digits) |
|||
13: 12748246592672078196..20708715953110886963 (592 digits) |
|||
14: 46749025165138838243..65355869250350888941 (1180 digits) |
|||
15: 11390125639471674628..31060548964273180103 (2358 digits) |
|||
16: 36961763505630520555..02467094377885929191 (4711 digits) |
|||
17: 21043364724798439508..14594683820359204509 (9418 digits) |
|||
</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 220: | Line 270: | ||
14 4674902516513883824...65355869250350888941 ( 1180 digits) |
14 4674902516513883824...65355869250350888941 ( 1180 digits) |
||
Real time: 1.147 s User time: 1.093 s Sys. time: 0.046 s CPU share: 99.28 %</pre> |
Real time: 1.147 s User time: 1.093 s Sys. time: 0.046 s CPU share: 99.28 %</pre> |
||
=={{header|Julia}}== |
|||
<syntaxhighlight lang="julia">""" rosettacode.org/wiki/Prime_reciprocal_sum """ |
|||
using Primes |
|||
using ResumableFunctions |
|||
""" Abbreviate a large string by showing beginning / end and number of chars """ |
|||
function abbreviate(s; ds = "digits", t = 40, x = (t - 1) ÷ 2) |
|||
wid = length(s) |
|||
return wid < t ? s : s[begin:begin+x] * ".." * s[end-x:end] * " ($wid $ds)" |
|||
end |
|||
@resumable function generate_oeis75442() |
|||
psum = big"0" // big"1" |
|||
while true |
|||
n = BigInt(ceil(big"1" // (1 - psum))) |
|||
while true |
|||
n = nextprime(n + 1) |
|||
if psum + 1 // n < 1 |
|||
psum += 1 // n |
|||
@yield n |
|||
break |
|||
end |
|||
end |
|||
end |
|||
end |
|||
for (i, n) in enumerate(Iterators.take(generate_oeis75442(), 17)) |
|||
println(lpad(i, 2), ": ", abbreviate(string(n))) |
|||
end |
|||
</syntaxhighlight>{{out}} |
|||
<pre> |
|||
1: 2 |
|||
2: 3 |
|||
3: 7 |
|||
4: 43 |
|||
5: 1811 |
|||
6: 654149 |
|||
7: 27082315109 |
|||
8: 153694141992520880899 |
|||
9: 337110658273917297268061074384231117039 |
|||
10: 84241975970641143191..13803869133407474043 (76 digits) |
|||
11: 20300753813848234767..91313959045797597991 (150 digits) |
|||
12: 20323705381471272842..21649394434192763213 (297 digits) |
|||
13: 12748246592672078196..20708715953110886963 (592 digits) |
|||
14: 46749025165138838243..65355869250350888941 (1180 digits) |
|||
15: 11390125639471674628..31060548964273180103 (2358 digits) |
|||
16: 36961763505630520555..02467094377885929191 (4711 digits) |
|||
17: 21043364724798439508..14594683820359204509 (9418 digits) |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |