Fibonacci Efficient algorithm (4 algorithms for comprehensive analysis)

Source: Internet
Author: User

The Fibonacci sequence problem is an inevitable problem for algorithm learners, and as a classical problem, the first contact is usually a case study of recursive algorithm.

However, the recursive solution to Fibonacci, whose inefficiency is appalling, has been calculated for its time complexity of O (2^n). The number of time complexity of the point.

If the interviewer asks you about Fibonacci's solution, you come up with a recursive solution, which basically says you're already on the game.

So there is no more efficient algorithm, this article will be introduced.

Here are 4 solutions to Fibonacci:

1. Recursive time complexity O (2^n)

int f (int n) {if (n = = 1 | | n = = 2) {return 1;} return f (n-1) + f (n-2);}

2. Cycle time complexity O (n)

    public int f (int n) {        //write code here        int F0 = 1;        int f1 = 1;        int F2 = 0;        for (int i = 2; i < n; i++) {            F2 = F0 + F1;            F0 = F1;            f1 = F2;        }        return f2;    }


3. Matrix Solver time Complexity O (logn)

Fibonacci's recursive formula can be expressed in the following matrix form, so its



Therefore, the results can be calculated in O (logn) time according to the partition algorithm of the matrix.

Written questions:

for Fibonacci Classic questions, we are very familiar with the recursive formula f (n) = f (n-1) + f (n-2) , we can find out in linear time the first N Items F (n) and now consider the enhanced version of Fibonacci, the number of items we require retracement N the range is int a non-negative integer in the range, design an efficient algorithm that calculates the N Items F (n) . The first Fibonacci number is F (0) = 1.

given a non-negative integer, return to the Fibonacci sequence N item, in order to prevent overflow, use the result Mod 1000000007 .


    Long[][] f = new long[][]{{0,1},{1,1}};    public int GetNthNumber1 (int n) {    if (n = = 0)            return 1;        if (n = = 1)            return 1;        f = pow (n,f);    return (int) f[1][1];    }    Private long[][] POW (int n,long[][] f) {    if (n = = 1)    return F;        if (n = = 2) {    return fun (f,f)    ;    } if (n% 2 = = 0) {//even    f = pow (n/2,f);    Return fun (F, f);    } else{    return Fun (Pow (n/2,f), pow (N/2 + 1,f));    }    }        Private long[][] Fun (long[][] f,long[][] m) {    long[][] temp = new long[2][2];    Temp[0][0] = (F[0][0]*m[0][0] + f[0][1]*m[1][0])%1000000007;    TEMP[0][1] = (f[0][0]*m[0][1] + f[0][1]*m[1][1])%1000000007;    Temp[1][0] = (F[1][0]*m[0][0] + f[1][1]*m[1][0])%1000000007;    TEMP[1][1] = (f[1][0]*m[0][1] + f[1][1]*m[1][1])%1000000007;    return temp;    }


4. Equation Solving time complexity O (1)

Yes, you're right. The Fibonacci sequence is a formula. Its general formula is as follows:


Therefore, any Fibonacci number can be calculated in O (1) time, but one thing, because of the irrational number involved, there is no guarantee of accuracy.

Specific code slightly.


In summary, the most effective code efficiency is the most accurate, is the 3rd matrix solution method, written interview must be mastered.


Finish


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fibonacci Efficient algorithm (4 algorithms for comprehensive analysis)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.