Last active
November 30, 2017 14:01
-
-
Save irtaylor/a017f6df3951a3c55aadc51411f753af to your computer and use it in GitHub Desktop.
dynamic programming fibonacci
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
# Dynamic Programming for calculating nth Fibonacci Number | |
# Ian Taylor, 2017 | |
def dp_fib(n, memo={}) | |
return memo[n] unless memo[n].nil? | |
(n <= 2) ? f = 1 : f = dp_fib(n - 1, memo) + dp_fib(n - 2, memo) | |
memo[n] = f | |
f | |
end | |
# standard recursive Fibonacci | |
def fib(n) | |
return 1 if n <= 2 | |
fib(n - 1) + fib(n - 2) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment