Fibonacci sequence in Ruby (recursion)

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

    This may help you.

    def fib_upto(max)
      i1, i2 = 1, 1
      while i1 <= max
        yield i1
        i1, i2 = i2, i1+i2
      end
    end
    
    fib_upto(5) {|f| print f, " "}
    

提交回复
热议问题