Write a function, enter N, ask Fibonacci Sequence the nth item.
Fibonacci series :
Solution: Method 1: from Fibonacci Sequence function-defined angle programming
#include <stdio.h>
int Fibonacci (int n)
{
int num1=1, num2=1, num3=0,i;
if (n <= 2)
{
printf ("%d of the Fibonacci series is:%d\n", N,NUM1);
}
Else
{
for (i = 2; i < n; i++)
{
num3 = Num1 + num2;
NUM1 = num2;
num2 = num3;
}
printf ("%d of the Fibonacci series is:%d\n", N, num3);
}
return 0;
}
int main ()
{
int num=0;
printf ("Please enter a positive integer:");
scanf ("%d", &num);
Fibonacci (NUM);
return 0;
}
Results:
Please enter a positive integer: 3
The 3rd of the Fibonacci sequence is: 2
Please press any key to continue ...
Method 2 : Recursive invocation, which clearly optimizes the amount of code
#include <stdio.h>
int Fibonacci (int n)
{
if (n <= 0)
{
return 0;
}
if (n = = 1)
{
return 1;
}
Return Fibonacci (n-1) + Fibonacci (n-2);
}
int main ()
{
int num = 0,ret=0;
printf ("Please enter a positive integer:");
scanf ("%d", &num);
Ret=fibonacci (num);
printf ("%d of the Fibonacci series is:%d\n", Num,ret);
return 0;
}
Results:
Please enter a positive integer: 4
The 4th of the Fibonacci sequence is: 3
Please press any key to continue ...
Method 3 : To improve the efficiency of recursion, to save the already obtained middle term, it is not necessary to repeat the calculation; its essence is equivalent to the idea of method one
#include <stdio.h>
int Fibonacci (int n)
{
int NUM1 = 1, num2 = 1, num3 = 0, i=0;
if (n <= 2)
{
return NUM1;
}
for (i = 2; i < n; i++)
{
num3 = Num1 + num2;
NUM1 = num2;
num2 = num3;
}
return num3;
}
int main ()
{
int num = 0,ret=0;
printf ("Please enter a positive integer:");
scanf ("%d", &num);
Ret=fibonacci (num);
printf ("%d of the Fibonacci series is:%d\n", Num,ret);
return 0;
}
Results:
Please enter a positive integer: 3
The 3rd of the Fibonacci sequence is: 2
Please press any key to continue ...
Method 4: Direct use of the mathematical formula method: f (N) ={[(1+5^0.5)/2]^n-[(1-5^0.5)/2]^n}/(5^0.5)
#include <stdio.h>
#include <math.h>
int Fibonacci (int n)
{
Return (POW ((1+SQRT (5.0))/2,n)-Pow ((1-SQRT (5.0))/2, N))/sqrt (5.0);
}
int main ()
{
int num = 0, ret = 0;
printf ("Please enter a positive integer:");
scanf ("%d", &num);
ret = Fibonacci (num);
printf ("%d of the Fibonacci series is:%d\n", NUM, ret);
return 0;
}
Results:
Please enter a positive integer: 4
The 4th of the Fibonacci sequence is: 3
Please press any key to continue ...
Method 5 : the method of mathematical formula of uncommon
F (N) f (n-1) = 1 1
[] []^ (n-1)
F (n-1) F (n-2) 1 0
The formula can be proved by mathematical induction, in the process of transformation of matrix multiplication, we should pay attention to the properties of Fibonacci sequence: The following is the sum of the preceding two items; The mathematical formula, using matrix multiplication, although the time efficiency is low, but not practical enough, the source code is too cumbersome, The following code is provided for reference only
#include <cassert>
struct MATRIX2BY2
{
Matrix2by2
(
Long Long m00 = 0,
Long Long m01 = 0,
Long Long m10 = 0,
Long Long M11 = 0
)
: m_00 (m00), m_01 (M01), M_10 (M10), M_11 (M11)
{
}
Long Long m_00;
Long Long m_01;
Long Long m_10;
Long Long m_11;
};
Matrix2by2 matrixmultiply
(
Const matrix2by2& MATRIX1,
Const matrix2by2& MATRIX2
)
{
Return Matrix2by2 (
matrix1.m_00 * matrix2.m_00 + matrix1.m_01 * matrix2.m_10,
matrix1.m_00 * matrix2.m_01 + matrix1.m_01 * matrix2.m_11,
MATRIX1.M_10 * matrix2.m_00 + matrix1.m_11 * matrix2.m_10,
MATRIX1.M_10 * matrix2.m_01 + matrix1.m_11 * matrix2.m_11);
}
Matrix2by2 matrixpower (unsigned int n)
{
ASSERT (n > 0);
Matrix2by2 Matrix;
if (n = = 1)
{
Matrix = matrix2by2 (1, 1, 1, 0);
}
else if (n% 2 = = 0)
{
Matrix = Matrixpower (N/2);
Matrix = matrixmultiply (matrix, matrix);
}
else if (n% 2 = = 1)
{
Matrix = Matrixpower ((n-1)/2);
Matrix = matrixmultiply (matrix, matrix);
Matrix = matrixmultiply (Matrix, Matrix2by2 (1, 1, 1, 0));
}
return matrix;
}
Long long fibonacci_solution3 (unsigned int n)
{
int result[2] = {0, 1};
if (n < 2)
return result[n];
Matrix2by2 PowerNMinus2 = Matrixpower (n-1);
return powernminus2.m_00;
}
==================== Test Code ====================
void Test (int n, int expected)
{
if (Fibonacci_solution1 (n) = = expected)
printf ("Test for%d in Solution1 passed.\n", N);
Else
printf ("Test for%d in Solution1 failed.\n", N);
if (Fibonacci_solution2 (n) = = expected)
printf ("Test for%d in Solution2 passed.\n", N);
Else
printf ("Test for%d in Solution2 failed.\n", N);
if (Fibonacci_solution3 (n) = = expected)
printf ("Test for%d in Solution3 passed.\n", N);
Else
printf ("Test for%d in Solution3 failed.\n", N);
}
int _tmain (int argc, _tchar* argv[])
{
Test (0, 0);
Test (1, 1);
Test (2, 1);
Test (3, 2);
Test (4, 3);
Test (5, 5);
Test (6, 8);
Test (7, 13);
Test (8, 21);
Test (9, 34);
Test (10, 55);
Test (40, 102334155);
return 0;
}
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1742164
C Language: Write a function, enter N, find the nth of Fibonacci sequence (5 methods, layer optimization)