Fibonacci sequence in Ruby (recursion)

后端 未结 24 1258
悲&欢浪女
悲&欢浪女 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:41

    PHI = 1.6180339887498959
    TAU = 0.5004471413430931
    
    def fibonacci(n)
      (PHI**n + TAU).to_i
    end
    

    You don't need recursion.

提交回复
热议问题