Sylvester's sequence: Difference between revisions

→‎{{header|Python}}: Added a functionally composed solution in Python.
(Added Perl)
(→‎{{header|Python}}: Added a functionally composed solution in Python.)
Line 236:
sum of reciprocals: 0.999999999999999999...99999999999999999635 (213 digits)
</pre>
 
=={{header|Python}}==
 
<lang python>'''Sylvester's sequence'''
 
from functools import reduce
from itertools import count, islice
 
 
# sylvester :: [Int]
def sylvester():
'''Non-finite stream of the terms
of Sylvester's sequence.
(OEIS A000058)
'''
def go(n):
return 1 + reduce(
lambda a, x: a * go(x),
range(0, n),
1
) if 0 != n else 2
 
return map(go, count(0))
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First terms, and sum of reciprocals.'''
 
print("First 10 terms of OEIS A000058:")
xs = list(islice(sylvester(), 10))
print('\n'.join([
str(x) for x in xs
]))
 
print("\nSum of the reciprocals of the first 10 terms:")
print(
reduce(lambda a, x: a + 1 / x, xs, 0)
)
 
 
# MAIN ---
if __name__ == '__main__':
main()</lang>
{{Out}}
<pre>First 10 terms of OEIS A000058:
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
 
Sum of the reciprocals of the first 10 terms:
0.9999999999999999</pre>
 
=={{header|Raku}}==
9,655

edits