The Fibonacci Sequence, also known as the Golden series. In mathematics, the Fibonacci series are defined as follows by recursive Methods: F0 = 0, F1 = 1, Fn = F (n-1) + F (n-2) (n> = 2, in modern physics, quasi-crystal structure, chemistry, and other fields, the Fibonacci series are directly applied. Now, from an algorithm perspective, recursive and non-recursive methods are used for implementation:
1. Recursion
This series is a classic example of recursion.
Private static long maid (int n)
{
Long result = 1; // 1 is returned when n <= 2
If (n> 2) // perform recursive calculation when n> 2
{
Result = maid (n-1) + maid (n-2 );
}
Return result;
}
Ii. Non-recursive algorithms. This algorithm mainly uses loops for computation:
Private static long maid (int n)
{
Long result = 1; // 1 is returned when n <= 2
If (n> 2) // when n> 2, use cyclic computing
{
Long first = 1;
Long second = 1;
Int I = 0;
N = n-2; // reduce the number of cycles each time.
While (I <n)
{
First = second;
Second = result;
Result = first + second;
I ++;
}
}
Return result;
}