An iterative algorithm for Fibonacci numbers

后端 未结 13 1055
野性不改
野性不改 2020-11-28 10:37

I am interested in an iterative algorithm for Fibonacci numbers, so I found the formula on wiki...it looks straight forward so I tried it in Python...it doesn\'t have a prob

相关标签:
13条回答
  • 2020-11-28 11:24

    Assuming these values for the fibonacci sequence:

    F(0) = 0;

    F(1) = 1;

    F(2) = 1;

    F(3) = 2

    For values of N > 2 we'll calculate the fibonacci value with this formula:

    F(N) = F(N-1) + F(N-2)

    One iterative approach we can take on this is calculating fibonacci from N = 0 to N = Target_N, as we do so we can keep track of the previous results of fibonacci for N-1 and N-2

    public int Fibonacci(int N)
    {
        // If N is zero return zero
        if(N == 0)
        {
            return 0;
        }
    
        // If the value of N is one or two return 1
        if( N == 1 || N == 2)
        {
           return 1;
        }
    
        // Keep track of the fibonacci values for N-1 and N-2
        int N_1 = 1;
        int N_2 = 1;
    
        // From the bottom-up calculate all the fibonacci values until you 
        // reach the N-1 and N-2 values of the target Fibonacci(N)
        for(int i =3; i < N; i++)
        {
           int temp = N_2;
           N_2 = N_2 + N_1;
           N_1 = temp;
        }
    
        return N_1 + N_2; 
    }
    
    0 讨论(0)
  • 2020-11-28 11:27

    I came across this on another thread and it is significantly faster than anything else I have tried and wont time out on large numbers. Here is a link to the math.

    def fib(n):
        v1, v2, v3 = 1, 1, 0  
        for rec in bin(n)[3:]: 
            calc = v2*v2
            v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3
            if rec=='1':    v1, v2, v3 = v1+v2, v1, v2
        return v2
    
    0 讨论(0)
  • 2020-11-28 11:27

    This work (intuitively)

    def fib(n):
        if n < 2:
            return n
        o,i = 0,1
        while n > 1:
            g = i
            i = o + i
            o = g
            n -= 1
        return i
    
    0 讨论(0)
  • 2020-11-28 11:28

    How about this simple but fastest way... (I just discovered!)

    def fib(n):
        x = [0,1]
        for i in range(n >> 1):
            x[0] += x[1]
            x[1] += x[0]
        return x[n % 2]
    

    Note! as a result, this simple algorithm only uses 1 assignment and 1 addition, since loop length is shorten as 1/2 and each loop includes 2 assignment and 2 additions.

    0 讨论(0)
  • 2020-11-28 11:29

    You are returning a value within a loop, so the function is exiting before the value of y ever gets to be any more than 1.

    If I may suggest something shorter, and much more pythonful:

    def fibs(n):                                                                                                 
        fibs = [0, 1, 1]                                                                                           
        for f in range(2, n):                                                                                      
            fibs.append(fibs[-1] + fibs[-2])                                                                         
        return fibs[n]
    

    This will do exactly the same thing as your algorithm, but instead of creating three temporary variables, it just adds them into a list, and returns the nth fibonacci number by index.

    0 讨论(0)
  • 2020-11-28 11:29

    Non recursive Fibonacci sequence in python

    def fibs(n):
        f = []
        a = 0
        b = 1
        if n == 0 or n == 1:
            print n
        else:
            f.append(a)
            f.append(b)
            while len(f) != n:
                temp = a + b
                f.append(temp)
                a = b
                b = temp
    
        print f
    
    fibs(10)
    

    Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

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