Created
February 9, 2012 23:47
-
-
Save kimyoutora/1784395 to your computer and use it in GitHub Desktop.
Fibonacci with Dynamic Programming
This file contains hidden or 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(n) | |
cache = {} | |
n.times do |i| | |
if i == 0 | |
cache[0] = 0 | |
elsif i == 1 | |
cache[1] = 1 | |
else | |
cache[i] = cache[i-1] + cache[i-2] | |
end | |
end | |
return cache[n-1] | |
end | |
puts "11: #{fib(11)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment