Created
October 22, 2018 23:29
-
-
Save rranelli/5e16082a69856991b1464a8fac037dca to your computer and use it in GitHub Desktop.
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
# fibo O(n) | |
CACHE = {} | |
def fib(n) | |
raise ArgumentError if n < 0 | |
if CACHE[n] | |
CACHE[n] | |
else | |
CACHE[n] = if n == 0 || n == 1 | |
1 | |
else | |
fib(n-1) + fib(n -2) | |
end | |
end | |
end | |
(1..30).each do |n| | |
puts "#{n} => #{fib(n)}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment