continued frac helper

1633 days ago by bardg

def expand( given, maxsteps=30 ): a0 = floor( given ) remainder = given - a0 expansion = [a0] steps = 1 while ((remainder != 0) & (steps < maxsteps)): recip = 1/remainder newa = floor( recip ) expansion = expansion + [newa] remainder = recip - newa steps = steps + 1 print expansion 
       
r1 = n( sqrt(5), digits = 20) 
       
r1 
       
2.2360679774997896964
2.2360679774997896964
expand( r1 ) 
       
[2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 13, 7, 2, 24, 4, 1,
1, 3, 15, 1, 6, 4, 2]
[2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 13, 7, 2, 24, 4, 1, 1, 3, 15, 1, 6, 4, 2]
r1_again = n( sqrt(5), digits = 50) 
       
r1_again 
       
2.2360679774997896964091736687312762354406183596115
2.2360679774997896964091736687312762354406183596115
expand( r1_again ) 
       
[2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4]
[2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
r2 = n( pi, digits = 20 ) 
       
r2 
       
3.1415926535897932385
3.1415926535897932385
expand( r2 ) 
       
[3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 3, 51, 1,
3, 1, 7, 3, 4, 1, 10, 9]
[3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 3, 51, 1, 3, 1, 7, 3, 4, 1, 10, 9]
def help_me_reassemble( a_list ): p_list = [0, 1] q_list = [1, 0] for j in range(2, len(a_list) + 2 ): p_list = p_list + [ a_list[j-2] * p_list[j-1] + p_list[j-2] ] q_list = q_list + [ a_list[j-2] * q_list[j-1] + q_list[j-2] ] print j+1, print "th approximation", print p_list[j], print "/", print q_list[j], print " roughly ", print n( p_list[j] / q_list[j] ) 
       
help_me_reassemble( [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4] ) 
       
3 th approximation 2 / 1  roughly  2.00000000000000
4 th approximation 9 / 4  roughly  2.25000000000000
5 th approximation 38 / 17  roughly  2.23529411764706
6 th approximation 161 / 72  roughly  2.23611111111111
7 th approximation 682 / 305  roughly  2.23606557377049
8 th approximation 2889 / 1292  roughly  2.23606811145511
9 th approximation 12238 / 5473  roughly  2.23606797003472
10 th approximation 51841 / 23184  roughly  2.23606797791580
11 th approximation 219602 / 98209  roughly  2.23606797747661
12 th approximation 930249 / 416020  roughly  2.23606797750108
13 th approximation 3940598 / 1762289  roughly  2.23606797749972
14 th approximation 16692641 / 7465176  roughly  2.23606797749979
15 th approximation 70711162 / 31622993  roughly  2.23606797749979
16 th approximation 299537289 / 133957148  roughly  2.23606797749979
17 th approximation 1268860318 / 567451585  roughly  2.23606797749979
18 th approximation 5374978561 / 2403763488  roughly  2.23606797749979
19 th approximation 22768774562 / 10182505537  roughly  2.23606797749979
20 th approximation 96450076809 / 43133785636  roughly  2.23606797749979
21 th approximation 408569081798 / 182717648081  roughly 
2.23606797749979
22 th approximation 1730726404001 / 774004377960  roughly 
2.23606797749979
23 th approximation 7331474697802 / 3278735159921  roughly 
2.23606797749979
24 th approximation 31056625195209 / 13888945017644  roughly 
2.23606797749979
25 th approximation 131557975478638 / 58834515230497  roughly 
2.23606797749979
26 th approximation 557288527109761 / 249227005939632  roughly 
2.23606797749979
27 th approximation 2360712083917682 / 1055742538989025  roughly 
2.23606797749979
28 th approximation 10000136862780489 / 4472197161895732  roughly 
2.23606797749979
29 th approximation 42361259535039638 / 18944531186571953  roughly 
2.23606797749979
30 th approximation 179445175002939041 / 80250321908183544  roughly 
2.23606797749979
31 th approximation 760141959546795802 / 339945818819306129  roughly 
2.23606797749979
32 th approximation 3220013013190122249 / 1440033597185408060  roughly 
2.23606797749979
3 th approximation 2 / 1  roughly  2.00000000000000
4 th approximation 9 / 4  roughly  2.25000000000000
5 th approximation 38 / 17  roughly  2.23529411764706
6 th approximation 161 / 72  roughly  2.23611111111111
7 th approximation 682 / 305  roughly  2.23606557377049
8 th approximation 2889 / 1292  roughly  2.23606811145511
9 th approximation 12238 / 5473  roughly  2.23606797003472
10 th approximation 51841 / 23184  roughly  2.23606797791580
11 th approximation 219602 / 98209  roughly  2.23606797747661
12 th approximation 930249 / 416020  roughly  2.23606797750108
13 th approximation 3940598 / 1762289  roughly  2.23606797749972
14 th approximation 16692641 / 7465176  roughly  2.23606797749979
15 th approximation 70711162 / 31622993  roughly  2.23606797749979
16 th approximation 299537289 / 133957148  roughly  2.23606797749979
17 th approximation 1268860318 / 567451585  roughly  2.23606797749979
18 th approximation 5374978561 / 2403763488  roughly  2.23606797749979
19 th approximation 22768774562 / 10182505537  roughly  2.23606797749979
20 th approximation 96450076809 / 43133785636  roughly  2.23606797749979
21 th approximation 408569081798 / 182717648081  roughly  2.23606797749979
22 th approximation 1730726404001 / 774004377960  roughly  2.23606797749979
23 th approximation 7331474697802 / 3278735159921  roughly  2.23606797749979
24 th approximation 31056625195209 / 13888945017644  roughly  2.23606797749979
25 th approximation 131557975478638 / 58834515230497  roughly  2.23606797749979
26 th approximation 557288527109761 / 249227005939632  roughly  2.23606797749979
27 th approximation 2360712083917682 / 1055742538989025  roughly  2.23606797749979
28 th approximation 10000136862780489 / 4472197161895732  roughly  2.23606797749979
29 th approximation 42361259535039638 / 18944531186571953  roughly  2.23606797749979
30 th approximation 179445175002939041 / 80250321908183544  roughly  2.23606797749979
31 th approximation 760141959546795802 / 339945818819306129  roughly  2.23606797749979
32 th approximation 3220013013190122249 / 1440033597185408060  roughly  2.23606797749979
help_me_reassemble( [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 3, 51, 1, 3, 1, 7, 3, 4, 1, 10, 9] ) 
       
3 th approximation 3 / 1  roughly  3.00000000000000
4 th approximation 22 / 7  roughly  3.14285714285714
5 th approximation 333 / 106  roughly  3.14150943396226
6 th approximation 355 / 113  roughly  3.14159292035398
7 th approximation 103993 / 33102  roughly  3.14159265301190
8 th approximation 104348 / 33215  roughly  3.14159265392142
9 th approximation 208341 / 66317  roughly  3.14159265346744
10 th approximation 312689 / 99532  roughly  3.14159265361894
11 th approximation 833719 / 265381  roughly  3.14159265358108
12 th approximation 1146408 / 364913  roughly  3.14159265359140
13 th approximation 4272943 / 1360120  roughly  3.14159265358939
14 th approximation 5419351 / 1725033  roughly  3.14159265358982
15 th approximation 80143857 / 25510582  roughly  3.14159265358979
16 th approximation 165707065 / 52746197  roughly  3.14159265358979
17 th approximation 245850922 / 78256779  roughly  3.14159265358979
18 th approximation 411557987 / 131002976  roughly  3.14159265358979
19 th approximation 1068966896 / 340262731  roughly  3.14159265358979
20 th approximation 2549491779 / 811528438  roughly  3.14159265358979
21 th approximation 6167950454 / 1963319607  roughly  3.14159265358979
22 th approximation 21053343141 / 6701487259  roughly  3.14159265358979
23 th approximation 1079888450645 / 343739169816  roughly 
3.14159265358979
24 th approximation 1100941793786 / 350440657075  roughly 
3.14159265358979
25 th approximation 4382713832003 / 1395061141041  roughly 
3.14159265358979
26 th approximation 5483655625789 / 1745501798116  roughly 
3.14159265358979
27 th approximation 42768303212526 / 13613573727853  roughly 
3.14159265358979
28 th approximation 133788565263367 / 42586222981675  roughly 
3.14159265358979
29 th approximation 577922564265994 / 183958465654553  roughly 
3.14159265358979
30 th approximation 711711129529361 / 226544688636228  roughly 
3.14159265358979
31 th approximation 7695033859559604 / 2449405352016833  roughly 
3.14159265358979
32 th approximation 69967015865565797 / 22271192856787725  roughly 
3.14159265358979
3 th approximation 3 / 1  roughly  3.00000000000000
4 th approximation 22 / 7  roughly  3.14285714285714
5 th approximation 333 / 106  roughly  3.14150943396226
6 th approximation 355 / 113  roughly  3.14159292035398
7 th approximation 103993 / 33102  roughly  3.14159265301190
8 th approximation 104348 / 33215  roughly  3.14159265392142
9 th approximation 208341 / 66317  roughly  3.14159265346744
10 th approximation 312689 / 99532  roughly  3.14159265361894
11 th approximation 833719 / 265381  roughly  3.14159265358108
12 th approximation 1146408 / 364913  roughly  3.14159265359140
13 th approximation 4272943 / 1360120  roughly  3.14159265358939
14 th approximation 5419351 / 1725033  roughly  3.14159265358982
15 th approximation 80143857 / 25510582  roughly  3.14159265358979
16 th approximation 165707065 / 52746197  roughly  3.14159265358979
17 th approximation 245850922 / 78256779  roughly  3.14159265358979
18 th approximation 411557987 / 131002976  roughly  3.14159265358979
19 th approximation 1068966896 / 340262731  roughly  3.14159265358979
20 th approximation 2549491779 / 811528438  roughly  3.14159265358979
21 th approximation 6167950454 / 1963319607  roughly  3.14159265358979
22 th approximation 21053343141 / 6701487259  roughly  3.14159265358979
23 th approximation 1079888450645 / 343739169816  roughly  3.14159265358979
24 th approximation 1100941793786 / 350440657075  roughly  3.14159265358979
25 th approximation 4382713832003 / 1395061141041  roughly  3.14159265358979
26 th approximation 5483655625789 / 1745501798116  roughly  3.14159265358979
27 th approximation 42768303212526 / 13613573727853  roughly  3.14159265358979
28 th approximation 133788565263367 / 42586222981675  roughly  3.14159265358979
29 th approximation 577922564265994 / 183958465654553  roughly  3.14159265358979
30 th approximation 711711129529361 / 226544688636228  roughly  3.14159265358979
31 th approximation 7695033859559604 / 2449405352016833  roughly  3.14159265358979
32 th approximation 69967015865565797 / 22271192856787725  roughly  3.14159265358979