from gen_phi import continuedint
gp = continuedint()
next(gp)
'1 + 1/(1)'
eval('1 + 1/(1)')
2.0
next(gp)
'1 + 1/(1+ 1/(1))'
next(gp)
'1 + 1/(1+ 1/(1+ 1/(1)))'
fraction = next(gp)
fraction
'1 + 1/(1+ 1/(1+ 1/(1+ 1/(1))))'
eval(fraction)
1.6
for _ in range(10):
fraction = next(gp)
eval(fraction)
1.6180327868852458
fraction
'1 + 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1+ 1/(1))))))))))))))'
for _ in range(10):
fraction = next(gp)
eval(fraction)
1.6180339886704433
for _ in range(10):
fraction = next(gp)
eval(fraction)
1.6180339887498896
Floating point numbers have limited precision, as we have seen. The Decimal type has limited precision also, but we get to set the precision.
from decimal import Decimal, getcontext
getcontext().prec = 35
one = Decimal('1')
five = Decimal('5')
check = (one + five.sqrt())/2
def continued():
patt = "+ one/(one"
the_expr = '' # empty
n = 1
while True:
the_expr = the_expr + patt
the_frac = "1 " + (the_expr + ")" * n)
# print(the_frac)
yield eval(the_frac) # or show the_frac
n += 1
def test_me():
the_gen = continued()
last_time = 1
while True:
approx = next(the_gen)
print(approx)
if approx == check:
break
if abs(approx - last_time) < Decimal(1e-35):
break
last_time = approx
print("Approx:", approx)
print("Check :", check)
if __name__ == "__main__":
test_me()
! python gen_phi.py
2 1.5 1.6666666666666666666666666666666667 1.6000000000000000000000000000000000 1.625 1.6153846153846153846153846153846154 1.6190476190476190476190476190476190 1.6176470588235294117647058823529412 1.6181818181818181818181818181818182 1.6179775280898876404494382022471910 1.6180555555555555555555555555555556 1.6180257510729613733905579399141631 1.6180371352785145888594164456233422 1.6180327868852459016393442622950820 1.6180344478216818642350557244174265 1.6180338134001252348152786474639950 1.6180340557275541795665634674922601 1.6180339631667065295383879454675915 1.6180339985218033998521803399852180 1.6180339850173579389731408733784031 1.6180339901755970865563773925808819 1.6180339882053250514708448197648044 1.6180339889579020013802622498274672 1.6180339886704431856047984005331556 1.6180339887802426828565073768668704 1.6180339887383030068527324379639341 1.6180339887543225376088304054925726 1.6180339887482036213437981910782939 1.6180339887505408393827219845199750 1.6180339887496481015309718934328875 1.6180339887499890970472967792907250 1.6180339887498588483500719802484156 1.6180339887499085989254214575880602 1.6180339887498895958965978196611962 1.6180339887498968544077192553799134 1.6180339887498940819031785860452540 1.6180339887498951409056791583151413 1.6180339887498947364027181108378957 1.6180339887498948909091006809994180 1.6180339887498948318929140179920490 1.6180339887498948544350914368526269 1.6180339887498948458247458432782610 1.6180339887498948491136052051407806 1.6180339887498948478573727131275878 1.6180339887498948483372108273046466 1.6180339887498948481539289767866629 1.6180339887498948482239364141635552 1.6180339887498948481971959525508621 1.6180339887498948482074099000120490 1.6180339887498948482035085192411814 1.6180339887498948482049987140925975 1.6180339887498948482044295103092167 1.6180339887498948482046469268079431 1.6180339887498948482045638810951446 1.6180339887498948482045956017348137 1.6180339887498948482045834855286050 1.6180339887498948482045881135075620 1.6180339887498948482045863457768996 1.6180339887498948482045870209899297 1.6180339887498948482045867630815019 1.6180339887498948482045868615937553 1.6180339887498948482045868239654228 1.6180339887498948482045868383381669 1.6180339887498948482045868328482671 1.6180339887498948482045868349452223 1.6180339887498948482045868341442567 1.6180339887498948482045868344501983 1.6180339887498948482045868343333390 1.6180339887498948482045868343779753 1.6180339887498948482045868343609257 1.6180339887498948482045868343674381 1.6180339887498948482045868343649506 1.6180339887498948482045868343659007 1.6180339887498948482045868343655378 1.6180339887498948482045868343656764 1.6180339887498948482045868343656235 1.6180339887498948482045868343656437 1.6180339887498948482045868343656360 1.6180339887498948482045868343656389 1.6180339887498948482045868343656378 1.6180339887498948482045868343656382 1.6180339887498948482045868343656381 Approx: 1.6180339887498948482045868343656381 Check : 1.6180339887498948482045868343656381
Convergence!