Last active
December 14, 2015 04:49
-
-
Save kiennt/5031287 to your computer and use it in GitHub Desktop.
Fibonaci using array
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
class FiboUsingArray | |
include Singleton | |
def initialize | |
@fib = [1, 1] | |
end | |
def [](n) | |
if n < @fib.size then | |
@fib[n] | |
else | |
(@fib.size..n).to_a.each do |i| | |
@fib << @fib[-1] + @fib[-2] | |
end | |
@fib[-1] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment