Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Memoized Fibonacci in Ruby
# memoized fibonacci in Ruby
class Fibber
attr_accessor :hash
def initialize
@hash = {}
end
def memo_fib(n)
for number in 0..n
if number < 2
@hash[number] = number
else
@hash[number] = @hash[number-1] + @hash[number-2]
end
end
@hash[n]
end
end
fibber = Fibber.new
p fibber.memo_fib(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.