A summary of solving the problem of Fibonacci Fibonacci sequence by C language _c language

Source: Internet
Author: User
Tags pow

One: Recursive implementation
using the formula F[n]=f[n-1]+f[n-2], recursion is computed sequentially, and the recursive end condition is f[1]=1,f[2]=1.

Two: Array implementation
the space complexity and time complexity are all 0 (n), and the efficiency is generally faster than that of recursion.

Three:vector<int> realization
the time complexity is 0 (n), the time complexity is 0 (1), the vector is not known to be highly efficient, of course the vector has its own attributes will occupy resources.

Four:queue<int> realization
of course, queues are better than arrays for Fibonacci sequences, time complexity and space complexity are the same as vector<int>, but queues are perfect for this.
F (n) =f (n-1) +f (n-2), f (n) is related only to F (n-1) and F (n-2), f (n) is queued, F (n-2) is available.

V: Iterative Implementation
the iterative implementation is the most efficient, with a time complexity of 0 (n) and a space complexity of 0 (1).

VI: Formula realization
Baidu, found that the original Fibonacci sequence has a formula, so you can use the formula to calculate.

Because the precision of double type is not enough, so the results of the program calculation will have errors, if the formula to expand the calculation, the result is 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 FIB1 (index-1) +fib1 (index-2); 
  the int fib2 (int index)//array implements {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<index;i++) a[i]=a[i-1]+a[i-2]; 
  int m=a[index-1];     Delete A; 
Free memory space return m; 
  the int fib3 (int index)//borrow vector<int> implementation {if (index<1) {return-1;   } vector<int> A (2,1); 
  Create a vector a.reserve (3) that contains 2 elements and is 1; 
    for (int i=2;i<index;i++) {A.insert (A.begin (), a.at (0) +a.at (1)); 
  A.pop_back (); 
Return a.at (0); 
  } int fib4 (int index)//queue Implementation {if (index<1) {return-1; 
  } queue<int>q; 
  Q.push (1); 
  Q.push (1); for (int i=2;i<index;i++) {Q.push (Q.froNT () +q.back ()); 
  Q.pop (); 
return Q.back (); 
  int fib5 (int n)//iteration Implementation {int i,a=1,b=1,c=1; 
  if (n<1) {return-1;   for (i=2;i<n;i++) {c=a+b; 
    The A=b method (similar to the Euclidean method for GCD); 
  B=c; 
return C; 
  int fib6 (int n) {double gh5=sqrt (double) 5); 
Return (POW (1+GH5), N)-pow ((1-gh5), N))/(Pow ((double) 2,n) *gh5); 
  int main (void) {printf ("%d\n", FIB3 (6)); 
  System ("pause"); 
return 0; 

 }

7:2-point Matrix method

As shown above, any one of the Fibonacci series can be calculated by the power of the Matrix, and the n power can be calculated in the Logn time.
The following code is posted:

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; 
} Compute matrix multiplication, c=a*b 
 
int Fibonacci (int n,int MoD)//mod indicates the number of modules required when the number is too large 
{ 
  if (n==0) return 0; 
  else if (n<=2) return 1;//here the No. 0 item is 0, 1th, 2 is 1 
 
  int a[2][2]={{1,1},{1,0}}; 
  int result[2][2]={{1,0},{0,1}};//initialized to the unit matrix 
  int s; 
  n-=2; 
  while (n>0) 
  { 
    if (n%2 = 1) 
      multiply (result,result,a,mod); 
    Multiply (a,a,a,mod); 
    n/= 2; 
  } The binary method to find the matrix power 
  s= (result[0][0]+result[0][1])%mod;//result return 
  s; 

The second-order quadratic function of a is calculated by the secondary method.

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

Related Article

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.