Skip to content

Instantly share code, notes, and snippets.

@kiennt
Last active December 14, 2015 04:49
Show Gist options
  • Save kiennt/5031287 to your computer and use it in GitHub Desktop.
Save kiennt/5031287 to your computer and use it in GitHub Desktop.
Fibonaci using array
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