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

Source: Internet
Author: User

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

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.