2022.10.21 MATH 3600-001 Continued Fractions

334 days ago by calkin

print(pi.n(digits=1000)) 
       
3.1415926535897932384626433832795028841971693993751058209749445923078164\
062862089986280348253421170679821480865132823066470938446095505822317253\
594081284811174502841027019385211055596446229489549303819644288109756659\
334461284756482337867831652712019091456485669234603486104543266482133936\
072602491412737245870066063155881748815209209628292540917153643678925903\
600113305305488204665213841469519415116094330572703657595919530921861173\
819326117931051185480744623799627495673518857527248912279381830119491298\
336733624406566430860213949463952247371907021798609437027705392171762931\
767523846748184676694051320005681271452635608277857713427577896091736371\
787214684409012249534301465495853710507922796892589235420199561121290219\
608640344181598136297747713099605187072113499999983729780499510597317328\
160963185950244594553469083026425223082533446850352619311881710100031378\
387528865875332083814206171776691473035982534904287554687311595628638823\
53787593751957781857780532171226806613001927876611195909216420199
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199
x=pi.n(digits=1000) print(x) 
       
3.1415926535897932384626433832795028841971693993751058209749445923078164\
062862089986280348253421170679821480865132823066470938446095505822317253\
594081284811174502841027019385211055596446229489549303819644288109756659\
334461284756482337867831652712019091456485669234603486104543266482133936\
072602491412737245870066063155881748815209209628292540917153643678925903\
600113305305488204665213841469519415116094330572703657595919530921861173\
819326117931051185480744623799627495673518857527248912279381830119491298\
336733624406566430860213949463952247371907021798609437027705392171762931\
767523846748184676694051320005681271452635608277857713427577896091736371\
787214684409012249534301465495853710507922796892589235420199561121290219\
608640344181598136297747713099605187072113499999983729780499510597317328\
160963185950244594553469083026425223082533446850352619311881710100031378\
387528865875332083814206171776691473035982534904287554687311595628638823\
53787593751957781857780532171226806613001927876611195909216420199
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199
cf_list=[] 
       
cf_list.append(floor(x)) x=1/(x-floor(x)) 
       
print(cf_list) 
       
[3]
[3]
print(x) 
       
7.0625133059310457697930051525705580427343100251455313339983168735559033\
375800560835039774759162439476488744725175186474260386582040555540477966\
776498996715377009826993322737936857034306033677957604721567860691753589\
547354471210711479992294579327019989733609201172662099933695123674140540\
893077075459531540453469466331112987157287671840408753934636464989020575\
881454043962616686027275547567721774575462610415060740860722288639817832\
252393256865743132786103491689307035766058439874771150414666033384593790\
056836187413835209996497522181133024012021556414309004877813694728367780\
289885987534147684209989975027194386510621953241075552527124266741527564\
949927710052436395997763200032107861330014054577403187673946060147478066\
534406567390345284974905312709869369489393981192946907565635346376416162\
185114284113501549506273571857775911968689146654820885425593719028126647\
594577021705672123166770681026656543617185371679881017918286980141396852\
97351559342757789796646743788471516248005268914084884335839271296
7.062513305931045769793005152570558042734310025145531333998316873555903337580056083503977475916243947648874472517518647426038658204055554047796677649899671537700982699332273793685703430603367795760472156786069175358954735447121071147999229457932701998973360920117266209993369512367414054089307707545953154045346946633111298715728767184040875393463646498902057588145404396261668602727554756772177457546261041506074086072228863981783225239325686574313278610349168930703576605843987477115041466603338459379005683618741383520999649752218113302401202155641430900487781369472836778028988598753414768420998997502719438651062195324107555252712426674152756494992771005243639599776320003210786133001405457740318767394606014747806653440656739034528497490531270986936948939398119294690756563534637641616218511428411350154950627357185777591196868914665482088542559371902812664759457702170567212316677068102665654361718537167988101791828698014139685297351559342757789796646743788471516248005268914084884335839271296
for i in srange(10): cf_list.append(floor(x)) x=1/(x-floor(x)) print(cf_list) 
       
[3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3]
[3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3]
a=3+1/(7+1/15) 
       
a.n() 
       
3.14150943396226
3.14150943396226
22./7 
       
3.14285714285714
3.14285714285714
a=3+1/(7+1/(15+1/1)) print(a) 
       
355/113
355/113
print(a.n()) 
       
3.14159292035398
3.14159292035398
print(pi.n()) 
       
3.14159265358979
3.14159265358979
a=3+1/(7+1/(15+1/(1+1/292))) print(a) 
       
103993/33102
103993/33102
a.n() 
       
3.14159265301190
3.14159265301190
pi.n() 
       
3.14159265358979
3.14159265358979
def con_frac(x,n): cf_list=[] for i in srange(n): if x==0: return(cf_list) cf_list.append(floor(x)) if x==floor(x): return(cf_list) x=1/(x-floor(x)) return(cf_list) 
       
con_frac(pi.n(100000),100) 
       
[3,
 7,
 15,
 1,
 292,
 1,
 1,
 1,
 2,
 1,
 3,
 1,
 14,
 2,
 1,
 1,
 2,
 2,
 2,
 2,
 1,
 84,
 2,
 1,
 1,
 15,
 3,
 13,
 1,
 4,
 2,
 6,
 6,
 99,
 1,
 2,
 2,
 6,
 3,
 5,
 1,
 1,
 6,
 8,
 1,
 7,
 1,
 2,
 3,
 7,
 1,
 2,
 1,
 1,
 12,
 1,
 1,
 1,
 3,
 1,
 1,
 8,
 1,
 1,
 2,
 1,
 6,
 1,
 1,
 5,
 2,
 2,
 3,
 1,
 2,
 4,
 4,
 16,
 1,
 161,
 45,
 1,
 22,
 1,
 2,
 2,
 1,
 4,
 1,
 2,
 24,
 1,
 2,
 1,
 3,
 1,
 2,
 1,
 1,
 10]
[3,
 7,
 15,
 1,
 292,
 1,
 1,
 1,
 2,
 1,
 3,
 1,
 14,
 2,
 1,
 1,
 2,
 2,
 2,
 2,
 1,
 84,
 2,
 1,
 1,
 15,
 3,
 13,
 1,
 4,
 2,
 6,
 6,
 99,
 1,
 2,
 2,
 6,
 3,
 5,
 1,
 1,
 6,
 8,
 1,
 7,
 1,
 2,
 3,
 7,
 1,
 2,
 1,
 1,
 12,
 1,
 1,
 1,
 3,
 1,
 1,
 8,
 1,
 1,
 2,
 1,
 6,
 1,
 1,
 5,
 2,
 2,
 3,
 1,
 2,
 4,
 4,
 16,
 1,
 161,
 45,
 1,
 22,
 1,
 2,
 2,
 1,
 4,
 1,
 2,
 24,
 1,
 2,
 1,
 3,
 1,
 2,
 1,
 1,
 10]
con_frac(1+sqrt(2),20) 
       
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
def approximants(L): if len(L)==1: return(L[0]) else: return(L[0]+1/approximants(L[1:])) 
       
approximants([1,1,1,1,1,1,1,1,1]) 
       
55/34
55/34
fibonacci(7) 
       
13
13
for i in srange(1,40): L=[1 for j in srange(i)] print(i,approximants(L).n()) 
       
(1, 1.00000000000000)
(2, 2.00000000000000)
(3, 1.50000000000000)
(4, 1.66666666666667)
(5, 1.60000000000000)
(6, 1.62500000000000)
(7, 1.61538461538462)
(8, 1.61904761904762)
(9, 1.61764705882353)
(10, 1.61818181818182)
(11, 1.61797752808989)
(12, 1.61805555555556)
(13, 1.61802575107296)
(14, 1.61803713527851)
(15, 1.61803278688525)
(16, 1.61803444782168)
(17, 1.61803381340013)
(18, 1.61803405572755)
(19, 1.61803396316671)
(20, 1.61803399852180)
(21, 1.61803398501736)
(22, 1.61803399017560)
(23, 1.61803398820532)
(24, 1.61803398895790)
(25, 1.61803398867044)
(26, 1.61803398878024)
(27, 1.61803398873830)
(28, 1.61803398875432)
(29, 1.61803398874820)
(30, 1.61803398875054)
(31, 1.61803398874965)
(32, 1.61803398874999)
(33, 1.61803398874986)
(34, 1.61803398874991)
(35, 1.61803398874989)
(36, 1.61803398874990)
(37, 1.61803398874989)
(38, 1.61803398874990)
(39, 1.61803398874989)
(1, 1.00000000000000)
(2, 2.00000000000000)
(3, 1.50000000000000)
(4, 1.66666666666667)
(5, 1.60000000000000)
(6, 1.62500000000000)
(7, 1.61538461538462)
(8, 1.61904761904762)
(9, 1.61764705882353)
(10, 1.61818181818182)
(11, 1.61797752808989)
(12, 1.61805555555556)
(13, 1.61802575107296)
(14, 1.61803713527851)
(15, 1.61803278688525)
(16, 1.61803444782168)
(17, 1.61803381340013)
(18, 1.61803405572755)
(19, 1.61803396316671)
(20, 1.61803399852180)
(21, 1.61803398501736)
(22, 1.61803399017560)
(23, 1.61803398820532)
(24, 1.61803398895790)
(25, 1.61803398867044)
(26, 1.61803398878024)
(27, 1.61803398873830)
(28, 1.61803398875432)
(29, 1.61803398874820)
(30, 1.61803398875054)
(31, 1.61803398874965)
(32, 1.61803398874999)
(33, 1.61803398874986)
(34, 1.61803398874991)
(35, 1.61803398874989)
(36, 1.61803398874990)
(37, 1.61803398874989)
(38, 1.61803398874990)
(39, 1.61803398874989)
(sqrt(5.)+1)/2 
       
1.61803398874989
1.61803398874989
for i in srange(1,40): L=[1 for j in srange(i)] print(i,approximants(L).n()-(1+sqrt(5.))/2) 
       
(1, -0.618033988749895)
(2, 0.381966011250105)
(3, -0.118033988749895)
(4, 0.0486326779167718)
(5, -0.0180339887498948)
(6, 0.00696601125010510)
(7, -0.00264937336527948)
(8, 0.00101363029772417)
(9, -0.000386929926365465)
(10, 0.000147829431923263)
(11, -0.0000564606600073070)
(12, 0.0000215668056606777)
(13, -8.23767693347577e-6)
(14, 3.14652861965747e-6)
(15, -1.20186464891425e-6)
(16, 4.59071787028975e-7)
(17, -1.75349769593325e-7)
(18, 6.69776591966098e-8)
(19, -2.55831884565794e-8)
(20, 9.77190839357434e-9)
(21, -3.73253694618825e-9)
(22, 1.42570222294580e-9)
(23, -5.44569944693762e-10)
(24, 2.08007167046276e-10)
(25, -7.94517784896698e-11)
(26, 3.03477243335237e-11)
(27, -1.15918386001113e-11)
(28, 4.42756942220512e-12)
(29, -1.69131375571396e-12)
(30, 6.45927755726916e-13)
(31, -2.46691556071710e-13)
(32, 9.41469124882133e-14)
(33, -3.59712259978551e-14)
(34, 1.37667655053519e-14)
(35, -5.32907051820075e-15)
(36, 1.99840144432528e-15)
(37, -8.88178419700125e-16)
(38, 2.22044604925031e-16)
(39, -2.22044604925031e-16)
(1, -0.618033988749895)
(2, 0.381966011250105)
(3, -0.118033988749895)
(4, 0.0486326779167718)
(5, -0.0180339887498948)
(6, 0.00696601125010510)
(7, -0.00264937336527948)
(8, 0.00101363029772417)
(9, -0.000386929926365465)
(10, 0.000147829431923263)
(11, -0.0000564606600073070)
(12, 0.0000215668056606777)
(13, -8.23767693347577e-6)
(14, 3.14652861965747e-6)
(15, -1.20186464891425e-6)
(16, 4.59071787028975e-7)
(17, -1.75349769593325e-7)
(18, 6.69776591966098e-8)
(19, -2.55831884565794e-8)
(20, 9.77190839357434e-9)
(21, -3.73253694618825e-9)
(22, 1.42570222294580e-9)
(23, -5.44569944693762e-10)
(24, 2.08007167046276e-10)
(25, -7.94517784896698e-11)
(26, 3.03477243335237e-11)
(27, -1.15918386001113e-11)
(28, 4.42756942220512e-12)
(29, -1.69131375571396e-12)
(30, 6.45927755726916e-13)
(31, -2.46691556071710e-13)
(32, 9.41469124882133e-14)
(33, -3.59712259978551e-14)
(34, 1.37667655053519e-14)
(35, -5.32907051820075e-15)
(36, 1.99840144432528e-15)
(37, -8.88178419700125e-16)
(38, 2.22044604925031e-16)
(39, -2.22044604925031e-16)
for i in srange(1,20): L=[2 for j in srange(i)] print(i,approximants(L).n()) 
       
(1, 2.00000000000000)
(2, 2.50000000000000)
(3, 2.40000000000000)
(4, 2.41666666666667)
(5, 2.41379310344828)
(6, 2.41428571428571)
(7, 2.41420118343195)
(8, 2.41421568627451)
(9, 2.41421319796954)
(10, 2.41421362489487)
(11, 2.41421355164605)
(12, 2.41421356421356)
(13, 2.41421356205732)
(14, 2.41421356242727)
(15, 2.41421356236380)
(16, 2.41421356237469)
(17, 2.41421356237282)
(18, 2.41421356237314)
(19, 2.41421356237309)
(1, 2.00000000000000)
(2, 2.50000000000000)
(3, 2.40000000000000)
(4, 2.41666666666667)
(5, 2.41379310344828)
(6, 2.41428571428571)
(7, 2.41420118343195)
(8, 2.41421568627451)
(9, 2.41421319796954)
(10, 2.41421362489487)
(11, 2.41421355164605)
(12, 2.41421356421356)
(13, 2.41421356205732)
(14, 2.41421356242727)
(15, 2.41421356236380)
(16, 2.41421356237469)
(17, 2.41421356237282)
(18, 2.41421356237314)
(19, 2.41421356237309)
1+sqrt(2.) 
       
2.41421356237309
2.41421356237309
for i in srange(1,20): L=[2 for j in srange(i)] print(i,approximants(L)) 
       
(1, 2)
(2, 5/2)
(3, 12/5)
(4, 29/12)
(5, 70/29)
(6, 169/70)
(7, 408/169)
(8, 985/408)
(9, 2378/985)
(10, 5741/2378)
(11, 13860/5741)
(12, 33461/13860)
(13, 80782/33461)
(14, 195025/80782)
(15, 470832/195025)
(16, 1136689/470832)
(17, 2744210/1136689)
(18, 6625109/2744210)
(19, 15994428/6625109)
(1, 2)
(2, 5/2)
(3, 12/5)
(4, 29/12)
(5, 70/29)
(6, 169/70)
(7, 408/169)
(8, 985/408)
(9, 2378/985)
(10, 5741/2378)
(11, 13860/5741)
(12, 33461/13860)
(13, 80782/33461)
(14, 195025/80782)
(15, 470832/195025)
(16, 1136689/470832)
(17, 2744210/1136689)
(18, 6625109/2744210)
(19, 15994428/6625109)
show(con_frac(exp(2).n(digits=1000), 30)) 
       
show(con_frac(exp(1/2).n(digits=1000), 30)) 
       
show(con_frac(exp(1/4).n(digits=1000), 30)) 
       
show(con_frac(exp(1/5).n(digits=1000), 30)) 
       
show(con_frac(exp(1/50).n(digits=1000), 30)) 
       
show(con_frac(exp(1).n(digits=1000), 30)) 
       
show(con_frac(exp(3).n(digits=1000), 60)) 
       
 
       
show(con_frac(pi.n(digits=1000)+exp(2).n(digits=1000), 50)) 
       
random() 
       
0.45627813519945914
0.45627813519945914
for i in srange( 20): x=1/random() print(floor(1/(x-floor(x)))) 
       
1
2
1
3
2
10
1
6
1
8
1
12
2
39
1
1
9
2
1
1
1
2
1
3
2
10
1
6
1
8
1
12
2
39
1
1
9
2
1
1
show(con_frac(random(),30)) 
       
show(con_frac(random(),30)) 
       
show(con_frac(random(),30)) 
       
show(con_frac(random(),50)) 
       
show(con_frac(random(),50))