Prime reciprocal sum: Difference between revisions

m
Python example
m (Python example)
Line 275:
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>
 
=={{header|Python}}==
<syntaxhighlight lang="python">''' rosettacode.org/wiki/Prime_reciprocal_sum '''
from fractions import Fraction
from sympy import nextprime
 
 
def abbreviated(bigstr, label="digits", maxlen=40, j=19):
''' Abbreviate string by showing beginning / end and number of chars '''
wid = len(bigstr)
return bigstr if wid <= maxlen else bigstr[:j] + '..' + bigstr[-j:] + f' ({wid} {label})'
 
 
def ceil(rat):
''' ceil function for Fractions '''
return rat.numerator if rat.denominator == 1 else rat.numerator // rat.denominator + 1
 
 
psum = Fraction(0, 1)
for i in range(1, 15): # get first 14 in sequence
next_in_seq = nextprime(ceil(Fraction(1, 1 - psum)))
psum += Fraction(1, next_in_seq)
print(f'{i:2}:', abbreviated(str(next_in_seq)))
</syntaxhighlight>{{out}}
<pre>
1: 2
2: 3
3: 7
4: 43
5: 1811
6: 654149
7: 27082315109
8: 153694141992520880899
9: 337110658273917297268061074384231117039
10: 8424197597064114319..3803869133407474043 (76 digits)
11: 2030075381384823476..1313959045797597991 (150 digits)
12: 2032370538147127284..1649394434192763213 (297 digits)
13: 1274824659267207819..0708715953110886963 (592 digits)
14: 4674902516513883824..5355869250350888941 (1180 digits)
</pre>
 
=={{header|Raku}}==
4,102

edits