I am trying to calculate fractions in Python 2.7. The limit_denominator
method works great for the first 15 iterations of this code. However, then the code gets stuck in a loop, outputting denominators less than 1,000,000
Fraction = 1217471/860882
When I don't use limit_denominator
, I get repeat outputs like this:
Fraction = 141421356237/100000000000
Eventually I want to iterate i
to 1000, so my fractions will be very large. Any help?
from fractions import *
i = 0
x = 1/2.0
x1 = 0
count = 0
while i < 20:
(y) = (1.0 + (x))
(x) = (1 / (2.0 + (x)))
y1 = Fraction(str(y)).limit_denominator()
print("
Fraction = " + str(y1))
i += 1
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…