Prime reciprocal sum: Difference between revisions

Added Sidef
(→‎{{header|ALGOL 68}}: Find the first 15)
(Added Sidef)
 
(3 intermediate revisions by 2 users not shown)
Line 295:
16: 36961763505630520555..02467094377885929191 (4711 digits)
17: 21043364724798439508..14594683820359204509 (9418 digits)
</pre>
 
=={{header|Nim}}==
{{libheader|bignum}}
<syntaxhighlight lang="Nim">import std/strformat
import bignum
 
iterator a075442(): Int =
let One = newRat(1)
var sum = newRat(0)
var p = newInt(0)
while true:
let q = reciprocal(One - sum)
p = nextPrime(if q.isInt: q.num else: q.toInt + 1)
yield p
sum += newRat(1, p)
 
func compressed(str: string; size: int): string =
## Return a compressed value for long strings of digits.
if str.len <= 2 * size: str
else: &"{str[0..<size]}...{str[^size..^1]} ({str.len} digits)"
 
var count = 0
for p in a075442():
inc count
echo &"{count:2}: {compressed($p, 20)}"
if count == 15: break
</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)
</pre>
 
Line 674 ⟶ 719:
15: 11390125639471674628..31060548964273180103 (2358 digits)
16: 36961763505630520555..02467094377885929191 (4711 digits)</pre>
 
=={{header|RPL}}==
Limited floating-point precision prevents from finding the correct 7th term. Program starts with a non-empty sequence to avoid the <code>∑LIST</code> calculation bug that occurs when the input list has less than two items.
{{works with|HP|49g}}
≪ {2 3}
1 4 '''START''' 1 OVER INV ∑LIST - INV →NUM IP NEXTPRIME + '''NEXT'''
≫ '<span style="color:blue">∑INVPR</span>' STO
{{out}}
<pre>
1: {2 3 7 43 1811 654149}
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var A075442 = Enumerator({|callback|
var sum = 0
loop {
var p = next_prime(ceil(1/(1-sum)))
sum += 1/p
callback(p)
}
})
 
A075442.first(15).each_kv {|k,n|
var s = Str(n)
s = "#{s.first(20)}..#{s.last(20)} (#{s.len} digits)" if (s.len > 50)
say "#{'%2d' % k+1}: #{s}"
}</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)
</pre>
 
=={{header|Wren}}==
Line 679 ⟶ 769:
{{libheader|Wren-fmt}}
Even with GMP takes about 4½ minutes to find the first 16.
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz, Mpq
import "./fmt" for Fmt
 
2,747

edits