Seven ways to realize the Fibonacci of Fibonacci (_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> implementation
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> implementation
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:
Copy Code code 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);
}
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 up 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 that contains 2 elements that are 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<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 Euclidean method (similar to the Euclidean method for GCD)
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

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:

Copy Code code as follows:

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;
}//computing matrix multiplication, c=a*b
int Fibonacci (int n,int MoD)//mod the number of modules that are 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 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;
}//of the Matrix power by the method of two points
S= (result[0][0]+result[0][1])%mod;//results
return s;
}

The second-order quadratic function of a is calculated by the secondary method.
Copy Code code as follows:

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.