Skip to content

Instantly share code, notes, and snippets.

@creasyw
Last active August 29, 2015 14:13
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 creasyw/9c79383a5a55439a27a5 to your computer and use it in GitHub Desktop.
Save creasyw/9c79383a5a55439a27a5 to your computer and use it in GitHub Desktop.
Fibonacci Number
import numpy as np
# Recursion
fib = lambda n: fib(n-1)+fib(n-2) if n > 2 else 1
# Memoization
def fib_memo(n):
register = {0: 0, 1: 1}
def helper(count):
if count in register:
return register[count]
register[count] = helper(count-1)+helper(count-2)
return register[count]
return helper(n)
# Dynamic Programming
def fib_dp(n):
a = 0
b = 1
for __ in range(n):
a, b = b, a + b
return a
# Math of Golden Ratio
def fib_math(n):
phi = (1+np.sqrt(5))/2
return int(phi**n/np.sqrt(5)+0.5)
# Matrix Algebra
def fib_matrix(n):
return np.dot(np.linalg.matrix_power(np.array([[1,1],[1,0]], dtype=object),\
(n-1)), np.array([1,0], dtype=object))[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment