Skip to content

Instantly share code, notes, and snippets.

@mugenen
Created April 1, 2012 10:19
Show Gist options
  • Save mugenen/2274252 to your computer and use it in GitHub Desktop.
Save mugenen/2274252 to your computer and use it in GitHub Desktop.
def fib_recursive(n):
if n == 1:
return 1
if n == 2:
return 1
return fib_recursive(n - 1) + fib_recursive(n - 2)
memo = {}
def fib_memo(n):
if n == 1:
return 1
if n == 2:
return 1
if n in memo:
return memo[n]
else:
memo[n] = fib_memo(n - 1) + fib_memo(n - 2)
return memo[n]
def fib_loop(n):
a = 1
b = 1
for i in xrange(n - 2):
a, b = a + b, a
return a
if __name__ == "__main__":
fib_recursive(40)
fib_memo(40)
fib_loop(40)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment