Skip to content

Instantly share code, notes, and snippets.

@kimyoutora
Created February 9, 2012 23:47
Show Gist options
  • Save kimyoutora/1784395 to your computer and use it in GitHub Desktop.
Save kimyoutora/1784395 to your computer and use it in GitHub Desktop.
Fibonacci with Dynamic Programming
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