I\'m trying to implement the following function, but it keeps giving me the stack level too deep (SystemStackError) error.
Any ideas what the problem mi
yet another ;)
def fib(n)
f = Math.sqrt(5)
((((1+f)/2)**n - ((1-f)/2)**n)/f).to_i
end
will be convenient to add some caching as well
def fibonacci
@fibonacci ||= Hash.new {|h,k| h[k] = fib k }
end
so we'll be able to get it like
fibonacci[3] #=> 2
fibonacci[10] #=> 55
fibonacci[40] #=> 102334155
fibonacci #=> {3=>2, 10=>55, 40=>102334155}