Fibonacci sequence in Ruby (recursion)

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

    This approach is fast and makes use of memoization:

    fib = Hash.new {|hash, key| hash[key] = key < 2 ? key : hash[key-1] + hash[key-2] }
    
    fib[123] # => 22698374052006863956975682
    

    In case you're wondering about how this hash initialization works read here:

    https://ruby-doc.org/core/Hash.html#method-c-new

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

    Recursion's very slow, here's a faster way

    a = []; a[0] = 1; a[1] = 1
    i = 1
    while i < 1000000
        a[i+1] = (a[i] + a[i-1])%1000000007
        i += 1
    end
    
    puts a[n]    
    

    It's O(1), however you could use matrix exponentiation, here's one of mine's implementation, but it's in java => http://pastebin.com/DgbekCJM, but matrix exp.'s O(8logn) .Here's a much faster algorithm, called fast doubling. Here's a java implementation of fast doubling.

    class FD {
    
        static int mod = 1000000007;
    
        static long fastDoubling(int n) {
            if(n <= 2) return 1;
            int k = n/2;
            long a = fastDoubling(k+1);
            long b = fastDoubling(k);
            if(n%2 == 1) return (a*a + b*b)%mod;
            else return (b*(2*a - b))%mod;
    }
    

    Yet, using karatsuba multiplication, both matrix exp. and fast doubling becomes much faster, yet fast doubling beats matrix exp. by a constant factor, well i didn't want to be very thorough here. But i recently did a lot of research on fibonacci numbers and i want my research to be of use to anyone willing to learn, ;).

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

    This is the snippet that I used to solve a programming challenge at URI Online Judge, hope it helps.

    def fib(n)
      if n == 1
        puts 0
      else
        fib = [0,1]
        (n-2).times do
          fib << fib[-1] + fib[-2]
        end
        puts fib.join(' ')
      end
    end
    
    fib(45)
    

    An it outputs

    # => 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733
    
    0 讨论(0)
  • 2020-12-01 03:41

    Here is something I came up with, I find this more straight forward.

    def fib(n)
      n.times.each_with_object([0,1]) { |num, obj| obj << obj[-2] + obj[-1] }
    end
    fib(10)
    
    0 讨论(0)
  • 2020-12-01 03:41

    i think this is pretty easy:

    def fibo(n) a=0 b=1 for i in 0..n c=a+b print "#{c} " a=b b=c end
    
    end
    
    0 讨论(0)
  • 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.

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