Last active
July 13, 2020 12:19
-
-
Save Rowing0914/55a51690a2283995f4a46e570c81ab63 to your computer and use it in GitHub Desktop.
Linear algebraic way of Fibonacci Sequence
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 f(n): | |
sqr_5 = 5**(1/2) | |
first = ((1 + sqr_5)/2)**n | |
second = ((1 - sqr_5)/2)**n | |
return int((1/sqr_5)*(first - second)) | |
if __name__ == '__main__': | |
answer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] | |
for i in range(1, 11): | |
res = f(n=i) | |
print(f"N = {i} -> {res} | Answer: {answer[i-1]}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment