Another formula I found out about:
$$f(n)=[\frac{\phi^n}{\sqrt5}]$$ (square brackets mean rounding down AKA truncation AKA conversion to int
the nearest integer).
from math import sqrt
root_five = sqrt(5)
phi = (root_five + 1) / 2
def fibonacci(n):
return intround((phi ** n) / root_five)
Note: only supports the normal fibonacci sequence, may or may not workuse the following function for negativesnegative support.
def fibonacci(n):
if n > 0:
return round((phi ** n) / root_five)
elif n % 2:
return round((phi ** -n) / root_five)
else:
return -round((phi ** -n) / root_five)