Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4615

Fibonacci sequence in Ruby (recursion)

$
0
0

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 > 1endputs fibonacci( 5 )

Viewing all articles
Browse latest Browse all 4615

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>