Skip to content

Instantly share code, notes, and snippets.

@AmanAdastra
Last active May 12, 2021 01:26
Show Gist options
  • Save AmanAdastra/ed5e520bfd776f0217a9931799a9e259 to your computer and use it in GitHub Desktop.
Save AmanAdastra/ed5e520bfd776f0217a9931799a9e259 to your computer and use it in GitHub Desktop.
def fib(n):
I = [[1, 1], [1, 0]]
if (n == 0): return 0
power(I, n - 1)
return I[0][0]
def multiply(I, A):
x = (I[0][0] * A[0][0] + I[0][1] * A[1][0])
y = (I[0][0] * A[0][1] + I[0][1] * A[1][1])
z = (I[1][0] * A[0][0] + I[1][1] * A[1][0])
w = (I[1][0] * A[0][1] + I[1][1] * A[1][1])
I[0][0], I[0][1], I[1][0], I[1][1] = x, y, z, w
def power(I, n):
if (n == 0 or n == 1):
return
A = [[1, 1], [1, 0]]
power(I, n // 2)
multiply(I, I)
if (n % 2 != 0):
multiply(I, A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment