Seven ways to realize Fibonacci (Fibonacci) sequence

Source: Internet
Author: User
Tags greatest common divisor
One: Recursive implementation
Using the formula F[n]=f[n-1]+f[n-2], recursive calculation in turn, the recursive end condition is f[1]=1,f[2]=1.
Two: Array implementation
Spatial complexity and time complexity are all 0 (n), which is more efficient and faster than recursion.
Three:vector<int> realization
Time complexity is 0 (n), time complexity is 0 (1), is not aware of vector efficiency is not high, of course, Vector has its own properties will occupy resources.
Four:queue<int> realization
Of course the queue is more suitable than an array to implement the Fibonacci sequence, the time complexity and spatial complexity are the same as vector<int>, but the queue is too good for here,
F (n) =f (n-1) +f (n-2), f (n) only and F (n-1) and F (n-2), F (n) after the queue, F (n-2) can be out of the queue.
V: Iterative Implementation
Iterative implementations are the most efficient, with a time complexity of 0 (N) and a spatial complexity of 0 (1).
VI: Formula implementation
Baidu's time, found the original Fibonacci sequence has a formula, so you can use the formula to calculate.
Because the precision of the double type is not enough, the result of the program calculation will be error, if the formula is expanded to calculate, 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) {R eturn-1; } if (Index==1 | | index==2) return 1; Return FIB1 (index-1) +fib1 (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<index;i++) a[i]=a[i-1]+a[i-2]; int m=a[index-1];         Delete A; Free memory space return m;}      int fib3 (int index)//borrow vector<int> implementation {if (index<1) {return-1;} vector<int> A (2,1); Create a vector containing 2 elements of 1 a.reserve (3);  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&lt  ; 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 method of tossing and adding (similar to the greatest common divisor method) A=b; 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

For example, any item in the Fibonacci sequence can be calculated with the power of a matrix, and the power of n can be calculated in Logn time.
Paste the following code:

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*bint Fibonacci (int n,int MoD)//mod indicates the number of modules that need to be modulo when the number is too large {if (n==0) return 0; else if (n<=2) return 1;//here means that the No. 0 item is 0, 1th , 2 items are 1 int a[2][2]={{1,1},{1,0}}; The int result[2][2]={{1,0},{0,1}};//is 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; }//s= (result[0][0]+result[0][1])%mod;//result return s;}

The accompanying re-labeling method computes the n-th-square function of a.

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

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.