aschanna123

Continued fractions and their convergents

May 8th, 2020
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. from decimal import *
  2. from math import floor, sqrt
  3. getcontext().prec = 200
  4.  
  5.  
  6. def continued_fraction_of_sqrt_n(n):
  7.     ret_list = []
  8.     x1 = Decimal(n).sqrt()
  9.     for i in range(10):  # number of values to return
  10.         t1 = floor(x1)
  11.         d = Decimal(x1 - t1)
  12.         ret_list.append(t1)
  13.         if d == 0:
  14.             return ret_list
  15.         else:
  16.             x1 = Decimal(1) / d
  17.     return ret_list
  18.  
  19.  
  20. assert continued_fraction_of_sqrt_n(4) == [2]
  21. # This is exact square root, hence it stops before 10 entries
  22. # sqrt(4) = 2
  23. assert continued_fraction_of_sqrt_n(5) == [2, 4, 4, 4, 4, 4, 4, 4, 4, 4]
  24. # This could be infinte list, but I am returning only first 10
  25. # sqrt(5) = <2> +       1
  26. #                 -------------
  27. #                   <4> +    1
  28. #                         -------
  29. #                         <4> +  1
  30. #                               ---
  31. #                                .(so on)
  32. assert continued_fraction_of_sqrt_n(6) == [2, 2, 4, 2, 4, 2, 4, 2, 4, 2]
  33.  
  34.  
  35. def convergents(e):
  36.     n = []  # Numerator
  37.     d = []  # Denominator
  38.     ret_list = []  # Decimals after
  39.     for i, val in enumerate(e):
  40.         if i == 0:
  41.             ni = val
  42.             di = 1
  43.         elif i == 1:
  44.             ni = val * e[i - 1] + 1
  45.             di = val
  46.         else:  # i > 1
  47.             ni = val * n[i - 1] + n[i - 2]
  48.             di = val * d[i - 1] + d[i - 2]
  49.  
  50.         n.append(ni)
  51.         d.append(di)
  52.     for i, j in zip(n, d):
  53.         ret_list.append(Decimal(i) / Decimal(j))
  54.     return ret_list
  55.  
  56.  
  57. assert convergents(continued_fraction_of_sqrt_n(4)) == [2]
  58. print(convergents(continued_fraction_of_sqrt_n(5)))
  59. # A list of numbers slowly converging to sqrt(5) = 2.23606797749979... something
  60. # Basically, it finds value of continued fraction upto depth i
  61. # 2,
  62. # 2 + 1/4
  63. # 2 + 1 / ( 4 + 1/4 )
  64. # 2 + 1 / ( 4 + 1 / ( 4 + 1/4 ) )
  65. #
  66. # Here is the output
  67. # [2,
  68. # 2.25,
  69. # 2.2352941176470588235294117647058823529411764705882352941176470588235294117647058823529411764705882352941176470588235294117647058823529411764705882352941176470588235294117647058823529411764705882352941,
  70. # 2.2361111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,
  71. # 2.2360655737704918032786885245901639344262295081967213114754098360655737704918032786885245901639344262295081967213114754098360655737704918032786885245901639344262295081967213114754098360655737704918033,
  72. # 2.2360681114551083591331269349845201238390092879256965944272445820433436532507739938080495356037151702786377708978328173374613003095975232198142414860681114551083591331269349845201238390092879256965944,
  73. # 2.2360679700347158779462817467568061392289420792983738351909373287045496071624337657591814361410560935501553078750228393933857116754978987758085145258541933126256166636214142152384432669468298921980632,
  74. # 2.2360679779158040027605244996549344375431331953071083505866114561766735679779158040027605244996549344375431331953071083505866114561766735679779158040027605244996549344375431331953071083505866114561767,
  75. # 2.2360679774766060137054648759278681179932592735899968434664847417242818886252787422741296622509138673644981620829048254233318738608477838080013033428708163202965105031107128674561394576871773462717266,
  76. # 2.2360679775010816787654439690399500024037305898754867554444497860679775010816787654439690399500024037305898754867554444497860679775010816787654439690399500024037305898754867554444497860679775010816788,
  77. # ]
  78. # These are better and better estimates of Q/q = sqrt(n)
Add Comment
Please, Sign In to add comment