Created
July 2, 2016 01:49
-
-
Save DiegoSalazar/55a9632a35d2e08769e041ec5da74c3b to your computer and use it in GitHub Desktop.
Memoized Fibonacci in Ruby
This file contains 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
# 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