Skip to content

Instantly share code, notes, and snippets.

@worldmind
Created September 17, 2019 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save worldmind/93ebf4e38ef291201b568ba06db07d53 to your computer and use it in GitHub Desktop.
Save worldmind/93ebf4e38ef291201b568ba06db07d53 to your computer and use it in GitHub Desktop.
Muller's recurrence
from decimal import Decimal
from fractions import Fraction
def muller_recurrence(y, z):
return 108 - ((815-1500/z)/y)
N = 25
x_prev = 4
x_curr = 4.25
x_prev_dec = Decimal(4)
x_curr_dec = Decimal(17)/Decimal(4)
x_prev_frac = Fraction(4)
x_curr_frac = Fraction(17, 4)
for i in range(2, N+1):
x_next = muller_recurrence(x_curr, x_prev)
x_prev, x_curr = x_curr, x_next
x_next_dec = muller_recurrence(x_curr_dec, x_prev_dec)
x_prev_dec, x_curr_dec = x_curr_dec, x_next_dec
x_next_frac = muller_recurrence(x_curr_frac, x_prev_frac)
x_prev_frac, x_curr_frac = x_curr_frac, x_next_frac
print(f"{i} | {x_next} | {x_next_dec} | {float(x_next_frac)}")
@worldmind
Copy link
Author

2 | 4.470588235294116 | 4.4705882352941176470588235 | 4.470588235294118
3 | 4.6447368421052175 | 4.6447368421052631578947362 | 4.644736842105263
4 | 4.770538243625083 | 4.7705382436260623229461618 | 4.770538243626063
5 | 4.855700712568563 | 4.8557007125890736342039857 | 4.855700712589074
6 | 4.91084749866063 | 4.9108474990827932004342938 | 4.910847499082793
7 | 4.945537395530508 | 4.9455374041239167246519529 | 4.945537404123916
8 | 4.966962408040999 | 4.9669625817627005962571288 | 4.966962581762701
9 | 4.980042204293014 | 4.9800457013556311118526582 | 4.980045701355631
10 | 4.987909232795786 | 4.9879794484783912679439415 | 4.987979448478392
11 | 4.991362641314552 | 4.9927702880620482067468253 | 4.992770288062068
12 | 4.967455095552268 | 4.9956558915062356478184985 | 4.995655891506634
13 | 4.42969049830883 | 4.9973912683733697540253088 | 4.997391268381344
14 | -7.817236578459315 | 4.9984339437852482376781601 | 4.998433943944817
15 | 168.93916767106458 | 4.9990600687785413938424188 | 4.999060071970894
16 | 102.03996315205927 | 4.9994358732880376990501184 | 4.999435937146839
17 | 100.0999475162497 | 4.9996602467866575821700634 | 4.999661524103767
18 | 100.00499204097244 | 4.9997713526716167817979714 | 4.9997969007134175
19 | 100.0002495792373 | 4.9993671517118171375788238 | 4.999878135477931
20 | 100.00001247862016 | 4.9897059157620938291040004 | 4.9999268795046
21 | 100.00000062392161 | 4.7951151851630947311130380 | 4.999956127061158
22 | 100.0000000311958 | 0.7281074924258006736651754 | 4.9999736760057125
23 | 100.00000000155978 | -581.7081261405031229400219627 | 4.999984205520272
24 | 100.00000000007799 | 105.8595186892360167901632650 | 4.999990523282228
25 | 100.0000000000039 | 100.2767586430669099906187869 | 4.99999431395856

@h8nor
Copy link

h8nor commented Sep 19, 2019

(EN) The problem of rounding a row of irrational numbers: https://latkin.org/blog/2014/11/22/mullers-recurrence-roundoff-gone-wrong/
(RU) Проблема округления ряда иррациональных чисел: https://habr.com/post/258483/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment