Fibonacci sequence in Ruby (recursion)

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

    I think this is the best answer, which was a response from another SO post asking a similar question.

    The accepted answer from PriteshJ here uses naive fibonacci recursion, which is fine, but becomes extremely slow once you get past the 40th or so element. It's much faster if you cache / memoize the previous values and passing them along as you recursively iterate.

提交回复
热议问题