Skip to content

Instantly share code, notes, and snippets.

@AlbertVeli
Created November 1, 2023 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertVeli/0c964673319709791adeb0ec79a41b31 to your computer and use it in GitHub Desktop.
Save AlbertVeli/0c964673319709791adeb0ec79a41b31 to your computer and use it in GitHub Desktop.
Calculate fib(n) using matrix exponentiation
import numpy as np
def fib(n):
"""Fibonacci using matrix exponentiation"""
m = np.array([[1, 1], [1, 0]])
pow_m = np.linalg.matrix_power(m, n)
return pow_m[0, 1]
# Print the first 20 to see that it actually works
for n in range(20):
print(fib(n))
@gbwsdownload
Copy link

Fibonacci series are essential when you are just starting to learn code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment