The Fibonacci numbers optimization of Geeks face test is LGN efficiency

Source: Internet
Author: User

The Fibonacci numbers are the numbers in the following integer sequence.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ...

In mathematical terms, the sequence Fn of FIBONACCI numbers are defined by the recurrence relation

With seed values

Write a function int fib (int n) that returns. For example, if n = 0, then fib () should return 0. If n = 1, then it should return 1. For n > 1, it should return

Following are different methods to get the nth number.

http://www.geeksforgeeks.org/program-for-nth-fibonacci-number/

The Fibonacci sequence is well known, and the recursive and dynamic programming method is not spoken here.

There is a more optimized method, the time efficiency of O (LGN).

Method 4 (Using power of the matrix {{1,1},{1,0}})
This another O (n) which relies on the fact so if we n times multiply the Matrix M = {1,1},{1,0}} to itself RDS Calculate Power (M. N)), then we get the (n+1) th Fibonacci number as the element at row and column (0, 0) in the Resul Tant matrix.

The matrix representation gives the following closed expression for the Fibonacci:

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

By multiplying this matrix, the time complexity is still O (n), but the binary can be further used to reduce the complexity to O (LGN).

Method 5 (Optimized Method 4)

The method 4 can is optimized to work in O (LOGN) time complexity. We can do recursive multiplication to get power (M, N) in the Prevous method (Similar to the optimization did in this post )

Here are the programs for METHOD4 and 5:

void Mulmatrix (int f[2][2]) {int a = f[0][0];  
    int b = f[1][0];  
    F[0][0] = a+b;  
    F[0][1] = A;  
    F[1][0] = A;  
F[1][1] = b;  
} void Powmatrix (int f[2][2], int n) {for (int i = 2; i < n; i++) Mulmatrix (F);  
    int fib (int n) {if (n = 0) return 0;  
    int f[2][2] = {{1,1},{1,0}};  
    Powmatrix (F, N);  
return f[0][0];  
        Class Optifib {public:void mulonematrix (int f[2][2]) {int a = f[0][0];  
        int b = f[1][0];  
        F[0][0] = a+b;  
        F[0][1] = A;  
        F[1][0] = A;  
    F[1][1] = b;  
        } void Pow2matrix (int f1[2][2]) {int a = f1[0][0];  
        int b = f1[0][1];  
        int c = f1[1][0];  
        int d = f1[1][1];  
        F1[0][0] = a*a+b*c;  
        F1[0][1] = a*b+b*d;  
        F1[1][0] = c*a+c*d;  
    F1[1][1] = c*b+d*d; } void Powmatrix (int f[2][2], int n) {if (n < 2) returN  
        int mid = n>>1;  
        Powmatrix (F, mid);  
        Pow2matrix (F);  
    if (n%2 = = 1) mulonematrix (F);  
        int fib (int n) {if (n = 0) return 0;//Note: Do not omit this special case!  
        int f[2][2] = {{1,1},{1,0}};  
        Powmatrix (F, n-1);  
    return f[0][0]; }  
};

Author: csdn Blog Jing Xin

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.