Fibonacci sequence in Ruby (recursion)

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

    fastest and smallest in lines solution:

    fiby = ->(n, prev, i, count, selfy) {
      i < count ? (selfy.call n + prev, n, i + 1, count, selfy) : (puts n)
    }
    fiby.call 0, 1, 0, 1000, fiby
    

    functional selfie pattern :)

提交回复
热议问题