Skip to content

Instantly share code, notes, and snippets.

@raeq
Created September 5, 2020 10:21
Show Gist options
  • Save raeq/0e394e66634ee3c88ede2c05d745e27e to your computer and use it in GitHub Desktop.
Save raeq/0e394e66634ee3c88ede2c05d745e27e to your computer and use it in GitHub Desktop.
Golden Ration method to calculate Fibonacci Numbers
def fibonacci_goldenratio(target):
"""
This method works by calculating the golden ratio, and then
raising the golden ration to the power of n, whereby n is the position of the fibonacci sequence.
As we look for higher and higher values of n, rounding errors throw us off. This method works for values of n
less than 72 or so.
:param target:
:return:
>>> fibonacci_goldenratio(80)
23416728348467685
"""
import math
if target < 2:
return target
if target > 71:
return matrix_fibonacci(target)
sqrt5 = math.sqrt(5)
golden_ratio = (1 + sqrt5) / 2
return math.floor(math.pow(golden_ratio, target) / sqrt5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment