Skip to content

Instantly share code, notes, and snippets.

@Rowing0914
Last active July 13, 2020 12:19
Show Gist options
  • Save Rowing0914/55a51690a2283995f4a46e570c81ab63 to your computer and use it in GitHub Desktop.
Save Rowing0914/55a51690a2283995f4a46e570c81ab63 to your computer and use it in GitHub Desktop.
Linear algebraic way of Fibonacci Sequence
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