Created
April 1, 2012 10:19
-
-
Save mugenen/2274252 to your computer and use it in GitHub Desktop.
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
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