The Fibonacci sequence (Fibonacci Sequence), also known as the Golden Division sequence. In mathematics, the Fibonacci sequence is defined recursively as follows: F0=0,f1=1,fn=f (n-1) +f (n-2) (n>=2,n∈n*) in modern physics, quasi-crystal structure, chemistry and other fields, the Fibonacci sequence has a direct application, now I from the angle of the algorithm, This is accomplished using both recursive and non-recursive methods:
One: Recursion
This sequence is a classic example of a recursive return implementation.
private static long Fibonacci (int n)
{
Long result = 1;//returns 1 when n<=2
if (N>2)//When n>2, recursive calculation
{
result= Fibonacci (n-1) +fibonacci (n-2);
}
return result;
}
Second: Non-recursive algorithm, the algorithm is mainly used to calculate the loop:
private static long Fibonacci (int n)
{
Long result = 1; Returns 1 when n<=2
if (n > 2)//When n>2, use cyclic calculation
{
Long first = 1;
Long second = 1;
int i = 0;
n = n-2; Every time, of course, two cycles are reduced.
while (I < n)
{
first = second;
Second = result;
result = first + second;
i++;
}
}
return result;
}
The rules for a list of numbers are as follows; 1,1,2,3,5,8,13,21,34. What is the 30th digit number, using both recursive and non-recursive methods of the algorithm to achieve