Created
July 21, 2021 13:35
-
-
Save igorvanloo/cb5be95452dc7af04e4aa998b3e1099f to your computer and use it in GitHub Desktop.
Fibonacci Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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