Skip to content

Instantly share code, notes, and snippets.

@nschloe
Created November 14, 2021 08:32
Show Gist options
  • Save nschloe/ab6c3c90b4a6bc02c40405803fa8fa35 to your computer and use it in GitHub Desktop.
Save nschloe/ab6c3c90b4a6bc02c40405803fa8fa35 to your computer and use it in GitHub Desktop.
Golden ratio vs. Fibonacci
from math import sqrt
fibonacci = [1, 1]
phi = (sqrt(5) - 1) / 2
print(phi)
print()
errors = []
n = 40
for _ in range(n):
approx = fibonacci[0] / fibonacci[1]
errors.append(abs(phi - approx))
print(fibonacci, errors[-1])
s = fibonacci[0] + fibonacci[1]
fibonacci[0] = fibonacci[1]
fibonacci[1] = s
@nschloe
Copy link
Author

nschloe commented Nov 14, 2021

The error decreases linearly:

Figure_1

0.6180339887498949

[1, 1] 0.3819660112501051
[1, 2] 0.1180339887498949
[2, 3] 0.04863267791677173
[3, 5] 0.018033988749894925
[5, 8] 0.0069660112501050975
[8, 13] 0.0026493733652794837
[13, 21] 0.0010136302977241662
[21, 34] 0.00038692992636546464
[34, 55] 0.00014782943192326314
[55, 89] 5.6460660007306984e-05
[89, 144] 2.15668056606777e-05
[144, 233] 8.237676933475768e-06
[233, 377] 3.1465286196574738e-06
[377, 610] 1.201864649025275e-06
[610, 987] 4.590717869179528e-07
[987, 1597] 1.7534976970434712e-07
[1597, 2584] 6.697765930763211e-08
[2584, 4181] 2.5583188345557062e-08
[4181, 6765] 9.771908504596638e-09
[6765, 10946] 3.732536946188247e-09
[10946, 17711] 1.4257022229458016e-09
[17711, 28657] 5.445698336714599e-10
[28657, 46368] 2.080070560239733e-10
[46368, 75025] 7.945166746736732e-11
[75025, 121393] 3.034783535582619e-11
[121393, 196418] 1.1591949622413722e-11
[196418, 317811] 4.427680444507587e-12
[317811, 514229] 1.6913137557139635e-12
[514229, 832040] 6.459277557269161e-13
[832040, 1346269] 2.468025783741723e-13
[1346269, 2178309] 9.414691248821327e-14
[2178309, 3524578] 3.608224830031759e-14
[3524578, 5702887] 1.3655743202889425e-14
[5702887, 9227465] 5.329070518200751e-15
[9227465, 14930352] 1.9984014443252818e-15
[14930352, 24157817] 7.771561172376096e-16
[24157817, 39088169] 2.220446049250313e-16
[39088169, 63245986] 1.1102230246251565e-16
[63245986, 102334155] 0.0
[102334155, 165580141] 1.1102230246251565e-16

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