Skip to content

Instantly share code, notes, and snippets.

@DiegoSalazar
Created July 2, 2016 01:49
Show Gist options
  • Save DiegoSalazar/55a9632a35d2e08769e041ec5da74c3b to your computer and use it in GitHub Desktop.
Save DiegoSalazar/55a9632a35d2e08769e041ec5da74c3b to your computer and use it in GitHub Desktop.
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