Skip to content

Instantly share code, notes, and snippets.

@irtaylor
Last active November 30, 2017 14:01
Show Gist options
  • Save irtaylor/a017f6df3951a3c55aadc51411f753af to your computer and use it in GitHub Desktop.
Save irtaylor/a017f6df3951a3c55aadc51411f753af to your computer and use it in GitHub Desktop.
dynamic programming fibonacci
# 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