Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Created July 21, 2021 13:35
Show Gist options
  • Save igorvanloo/cb5be95452dc7af04e4aa998b3e1099f to your computer and use it in GitHub Desktop.
Save igorvanloo/cb5be95452dc7af04e4aa998b3e1099f to your computer and use it in GitHub Desktop.
Fibonacci Generator
def fibonnaci(n): #Finds the nth fibonnaci number
v1, v2, v3 = 1, 1, 0 # initialise a matrix [[1,1],[1,0]]
for rec in bin(n)[3:]: # perform fast exponentiation of the matrix (quickly raise it to the nth power)
calc = v2*v2
v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3
if rec=='1':
v1, v2, v3 = v1+v2, v1, v2
return v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment