Seven implementation methods for finding the general terms of the Fibonacci series

Source: Internet
Author: User
This article provides a detailed analysis of the seven implementation methods for the aggregate of the Fibonacci series. for details, refer to the following: Recursive implementation.
The formula f [n] = f [n-1] + f [N-2] is used for recursive computation in sequence. The conditions for recursive termination are f [1] = 1, f [2] = 1.
II. array implementation
The space complexity and time complexity are both 0 (n), which is generally more efficient than recursive compaction.
3. vector Implementation
The time complexity is 0 (n), and the time complexity is 0 (1), that is, the efficiency of the vector is not high. of course, the attributes of the vector will occupy resources.
IV. queue Implementation
Of course, queues are more suitable for implementing the Fibonacci series than arrays, including time complexity and space complexity and vector The same, but the queue is too suitable here,
F (n) = f (n-1) + f (n-2), f (n) only relates to f (n-1) and f (n-2), after f (n) enters the queue, f (n-2) can be out of the queue.
V. iterative implementation
Iteration implementation is the most efficient. The time complexity is 0 (n), and the space complexity is 0 (1 ).
6. formula implementation
Baidu found that the original Fibonacci series had a formula, so they could be calculated using the formula.
Because the accuracy of the double type is not enough, there will be errors in the results calculated by the program. if we expand the formula, the results will be correct.
The complete implementation code is as follows:

# Include "iostream" # include "queue" # include "cmath" using namespace std; int fib1 (int index) // recursive implementation {if (index <1) {return-1;} if (index = 1 | index = 2) return 1; return fiber 1 (index-1) + Fiber 1 (index-2 );} int fib2 (int index) // array implementation {if (index <1) {return-1;} if (index <3) {return 1 ;} int * a = new int [index]; a [0] = a [1] = 1; for (int I = 2; I
    
     
Implement {if (index <1) {return-1;} vector
     
      
A (2, 1); // create a vector a. reserve (3) with 2 elements being 1; for (int I = 2; I
      
       
Q; q. push (1); q. push (1); for (int I = 2; I
       
        

VII. binary matrix method

The following code is provided:

Void multiply (int c [2] [2], int a [2] [2], int B [2] [2], int mod) {int tmp [4]; tmp [0] = a [0] [0] * B [0] [0] + a [0] [1] * B [1] [0]; tmp [1] = a [0] [0] * B [0] [1] + a [0] [1] * B [1] [1]; tmp [2] = a [1] [0] * B [0] [0] + a [1] [1] * B [1] [0]; tmp [3] = a [1] [0] * B [0] [1] + a [1] [1] * B [1] [1]; c [0] [0] = tmp [0] % mod; c [0] [1] = tmp [1] % mod; c [1] [0] = tmp [2] % mod; c [1] [1] = tmp [3] % mod;} // Calculate matrix multiplication, c = a * bint fibonacci (int n, int mod) // mod indicates the number of modulus required when the number is too large {if (n = 0) return 0; else if (n <= 2) return 1; // Here, the first item is 0, and the first and second items are 1 int a [2] [2] = {0th }, {}}; int result [2] [2] = {}, {}; // The initialization is the unit matrix int s; n-= 2; while (n> 0) {if (n % 2 = 1) multiply (result, result, a, mod); multiply (a, mod ); n/= 2;} // returns the matrix power (s) = (result [0] [0] + result [0] [1]) % mod; // return s ;}

And then paste it to the n function that computes a by using the binary method.

int pow(int a,int n){ int ans=1; while(n) {  if(n&1)   ans*=a;  a*=a;  n>>=1; } return ans;}

For more information about the seven implementation methods of the Fibonacci series, see The PHP Chinese network!

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.