Fibonacci sequence in Ruby (recursion)

后端 未结 24 1240
悲&欢浪女
悲&欢浪女 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:54
    a = [1, 1]
    while(a.length < max) do a << a.last(2).inject(:+) end
    

    This will populate a with the series. (You will have to consider the case when max < 2)

    If only the nth element is required, You could use Hash.new

    fib = Hash.new {|hsh, i| hsh[i] = fib[i-2] + fib[i-1]}.update(0 => 0, 1 => 1)
    
    fib[10]
    # => 55 
    
    0 讨论(0)
  • 2020-12-01 03:54

    yet another ;)

    def fib(n)
      f = Math.sqrt(5)
      ((((1+f)/2)**n - ((1-f)/2)**n)/f).to_i
    end
    

    will be convenient to add some caching as well

    def fibonacci
      @fibonacci ||= Hash.new {|h,k| h[k] = fib k }
    end
    

    so we'll be able to get it like

    fibonacci[3]  #=> 2
    fibonacci[10] #=> 55
    fibonacci[40] #=> 102334155
    fibonacci     #=> {3=>2, 10=>55, 40=>102334155}
    
    0 讨论(0)
  • 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, " "}
    
    0 讨论(0)
  • 2020-12-01 03:57

    Linear

    module Fib
      def self.compute(index)
        first, second = 0, 1
        index.times do
          first, second = second, first + second
        end
        first
      end
    end
    

    Recursive with caching

    module Fib
      @@mem = {}
      def self.compute(index)
        return index if index <= 1
    
        @@mem[index] ||= compute(index-1) + compute(index-2)
      end
    end
    

    Closure

    module Fib
      def self.compute(index)
        f = fibonacci
        index.times { f.call }
        f.call
      end
    
      def self.fibonacci
        first, second = 1, 0
        Proc.new {
          first, second = second, first + second
          first
        }
      end
    end
    

    None of these solutions will crash your system if you call Fib.compute(256)

    0 讨论(0)
  • 2020-12-01 03:58

    Someone asked me something similar today but he wanted to get an array with fibonacci sequence for a given number. For instance,

    fibo(5)  => [0, 1, 1, 2, 3, 5]
    fibo(8)  => [0, 1, 1, 2, 3, 5, 8]
    fibo(13) => [0, 1, 1, 2, 3, 5, 8, 13]
    # And so on...
    

    Here's my solution. It's not using recursion tho. Just one more solution if you're looking for something similar :P

    def fibo(n)
      seed = [0, 1]
      n.zero? ? [0] : seed.each{|i| i + seed[-1] > n ? seed : seed.push(i + seed[-1])}
    end
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题