Fibonacci sequence in Ruby (recursion)

后端 未结 24 1271
悲&欢浪女
悲&欢浪女 2020-12-01 02:54

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

24条回答
  •  孤独总比滥情好
    2020-12-01 03:50

    It's been a while, but you can write a fairly elegant and simple one line function:

    def fib(n)
      n > 1 ? fib(n-1) + fib(n-2) : n
    end
    

提交回复
热议问题