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 might be ?
def fibonacci( n )
[ n ] if ( 0..1 ).include? n
( fibonacci( n - 1 ) + fibonacci( n - 2 ) ) if n > 1
end
puts fibonacci( 5 )
Try this
check this post too Fibonacci One-Liner
and more .. https://web.archive.org/web/20120427224512/http://en.literateprograms.org/Fibonacci_numbers_(Ruby)
You have now been bombarded with many solutions 🙂
regarding problem in ur solution
you should return
nif its0or1and
addlast two numbers not last and next