Fibonacci series (Fibonacci sequence), also known as the Golden Section series, because the mathematician Leonardo's Fibonacci (Leonardoda Fibonacci) to the rabbit breeding as an example of the introduction, so called "Rabbit series", refers to such a series: 1, 1, 2, 3, 5, 8, 13, 21, 、...... Mathematically, the Fibonacci sequence is defined as a recursive method: F (1) =1,f (2) =1, f (n) =f (n-1) +f (n-2)
[CPP] View plain copy
#include <iostream>
using namespace std;
Int main ()
{
int a[4] = {1,1,1,0};//discussed before
int b[4] = {1,0,0,1};//Unit array
int n,a1,a2,a3,a0;
Cin >> N;
if (n = = 0)
{
cout << "0";
return 0;
}
if (n = = 1)
{
cout << "1";
return 1;
}
N = n-1;
while (n > 0)
{
if (n%2 = = 1)
{
n = n-1;
a0 = b[0]*a[0] + b[1]*a[2];
A1 = b[0]*a[1] + b[1]*a[3];
A2 = b[2]*a[0] + b[3]*a[2];//here actually does not update b[2],b[3]
A3 = b[2]*a[1] + b[3]*a[3];
B[0] = a0;b[1] = a1;b[2] = a2;b[3] = A3;
}
N = n/2;
a0 = a[0]*a[0] + a[1]*a[2];
A1 = a[0]*a[1] + a[1]*a[3];
A2 = a[2]*a[0] + a[3]*a[2];
A3 = a[2]*a[1] + a[3]*a[3];
A[0] = a0;a[1] = a1;a[2] = a2;a[3] = A3;
}
cout << b[0];
return 0;
}
Copyright NOTICE: This article for Bo Master-Janching original article, without Bo Master permission not reproduced. http://www.ylsjwang.com/mingxing/40.html
C + + implements the Fibonacci sequence