I'm not seeing the mathematical results I'm expecting from the following code, which I believe should produce the Harmonic Series:
from fractions import Fraction
def sum_fracs(n):
if n == 1:
return 1
return 1/n + sum_fracs(n - 1)
for n in range(1, 6):
print(sum_fracs(n).as_integer_ratio())
for n in range(1, 6):
print(Fraction(sum_fracs(n)))
Output:
(1, 1)
(3, 2)
(8256599316845909, 4503599627370496)
(2345624805922133, 1125899906842624)
(1285402393645329, 562949953421312)
1
3/2
8256599316845909/4503599627370496
2345624805922133/1125899906842624
1285402393645329/562949953421312
Neither approach gives
1
3/2
11/6
25/12
137/60
as I was hoping. I know floats can have rounding errors, but I would hope that something this basic would be possible in Python.
Any help much appreciated.