The rules for a column of numbers are as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34 ........ Calculate the number of 30th-bit digits, and implement it using the recursive and non-recursive methods.

Source: Internet
Author: User

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;
}

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.