The sum of the First n digits of Fibonacci and the n digits of Fibonacci

Source: Internet
Author: User

The sum of the First n digits of Fibonacci and the n digits of Fibonacci

Public class Fei_bo_na_qi {
Public static void main (String [] args) {// main method, program entry
Int I = 10; // declare an int type variable I, and assign a value of 10
Int a = 0; // declare an int data type variable
For (int j = I; j> = 1; -- j) {// for Loop: j = I = 10, j> = 1
A + = m1 (j); // call the m1 method and assign the value of m1 to
}

System. out. println (a); // output the value of.
}
Public static int m1 (int n) {// declare the m1 method, the form parameter is int n, bringing n = 10
If (n = 1) {// when n = 1, the return value is 0.
Return 0;
}
If (n = 2) {// when n2, the return value is 1
Return 1;
} Else {// when n is neither 1 nor 2, execute the following statement
Return m1 (n-1) + m1 (n-2); // After n = 10 is brought in, return m1 (9) + m1 (8), -- j = 9,9> = 1; after bringing in, m1 (8) + m1 (7) is returned, -- j = 8 is brought in, and so on. When n = 3, m1 (2) is returned) + m1 (1) = 0 + 1 = 1, m1 (3) = 1, m1 (4), m1 (4) = m1 (3) + m1 (4-2), m1 (3) = 1, bring m1 (4-2) into the run once, every time m1 (n-1) runs once, to m1 (n-2) run again; Also to n = 10 into, return m1 (9) + m1 (8), m1 (8), -- j = 7, 7> = 1; after the return m1 (6) + m1 (5), every run m1 (n-2), m1 (n-2) to run again. Finally, the m1 value is obtained, assigned to a, and finally output.
 

Fibonacci, the puzzle that plagued me for a long time, was finally solved.

 

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.