Created
November 1, 2023 08:30
-
-
Save AlbertVeli/0c964673319709791adeb0ec79a41b31 to your computer and use it in GitHub Desktop.
Calculate fib(n) using matrix exponentiation
This file contains 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
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fibonacci series are essential when you are just starting to learn code.