Created
May 30, 2014 15:52
-
-
Save hillscottc/a2c5dec8512a96ac60b9 to your computer and use it in GitHub Desktop.
Fibonacci, basic memoization.
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
fib_vals = {1:1, 2:1} | |
def fib(n): | |
if n <= 2: | |
return 1 | |
if n in fib_vals: | |
return fib_vals[n] | |
else: | |
fib_vals[n] = fib(n - 1) + fib(n - 2) | |
return fib_vals[n] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment