Algorithm interpretation: the basic algorithm

Source: Internet
Author: User

1. Perform cyclic operation for 1~n and

To complete this calculation, you can use the following loop steps to find out:

A. Set the initial value of the sum variable sum to 0.
b. Sum is calculated and value is Addend
C. When value is below N, repeat operation
D. Calculate the value of the Sum+value and deposit the value in sum
E. Add 1 for each value.

1          Public Static intSum (intN)2         {3             intsum =0;4              for(inti =0; I <= N; i++)5             {6sum = sum +i;7             }8             returnsum;9}
View Code

2. Fibonacci Sequence

Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ....

Therefore, the calculation of the nth value can be calculated according to the following method:

A. Make f[0] = 0, f[1] = 1
B. Variable I starting from 2
C. Perform the operation of the operations repeatedly before I reach N
D. Make f[i] = F[i-2] + f[i-1]
E. Each time I value plus 1.

1          Public Static intFibonacci (intN)2         {3             if(n = =0)4             {5                 return 0;6             }7             Else if(n = =1)8             {9                 return 1;Ten             } One             Else A             { -                 returnFibonacci (N-2) + Fibonacci (N-1); -             } the}
View Code

3. To continue ....

Algorithm interpretation: the basic algorithm

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.