A Simple summation problem of the fibonacci series.

Source: Internet
Author: User

A Simple summation problem of the fibonacci series.

Some time ago, when I was talking about function calls, I used the Fibonacci series to demonstrate it, because I had never been touched by Fibonacci before, so I was very confused.

At that time, we asked for the number of nth bits in the Fibonacci series. What the teacher wrote at that time was:

After that, the teacher left his homework to calculate the sum of the First N digits of the Fibonacci series. He thought during his self-study at night, and he had to use the For Loop To sum the values, the following code is written:

Public class Fibonacci {public static void main (String [] args) {int I = 5; // declare an int type variable I and assign a value of 5 int B = 0; // declare an int type variable B and for (int a = I; a> = 1; -- a) {// for Loop: a = I = 5 B + = Fibonacci (a); // call the Fibonacci method and assign it to B} System. out. println (B);} public static int Fibonacci (int n) {// code of the Fibonacci series, which is not detailed here. If (n = 1) {return 0;} if (n = 2) {return 1;} else {return fig (n-1) + fig (n-2 );}}}

I would like to thank the students in the figure. He told me to implement the sum of Fibonacci Through iteration. I took a closer look and used this method to get the result:

Public class Fei_Bo_Na_Qi {public static void main (String [] args) {int m = 5; System. out. println ("the number of digits in the Fibonacci sequence" + m + "is:" + m1 (m); // call the function when outputting} public static int m1 (int I) {// create method if (I = 1) {// if... if it is equal to 1, execute the following java statement return 0; // if the returned value is 0, return it to m1 and end the java statement} if (I = 2) {// if... if it is equal to 2, execute the following java statement return 1; // return value 1 and end Java statement} else {// execute the java statement int a = 0 if it is neither = 2 nor = 1; // here represents the number of the Fibonacci series A number declares a variable named a using the int data type and assigns a value of 0 int B = 1; // The second number of Fibonacci int c = 0 here; // here it indicates the third number of Fibonacci int e = 0; for (int j = 3; j <= I; ++ j) {// for Loop what does this I-2 mean here? Because if it is equal to 3, it is the first operation here, that is, when it is equal to 3, it can be said that it is equal to 1 here, because when it is equal to 1 and 2, it is output above, if 2 is not subtracted here, the result will be recycled three times. // recycle code block c = a + B; // assign the sum of a and B to c, because every bit after the first two is composed of the first two and e + = c; // Add the number of the first N columns to System. out. println (a + "+" + B + "Fibonacci sequence" + j + "bits:" + "=" + c ); // output a + B = c a = B; // B indicates the second of the series, and a indicates the first of the series, c Represents the third of the series. B is assigned to a, which is equal to B = c; // assign c to B, which is equal to the third of the series. Then, in the next loop, c is the fourth of the series, and so on} System. out. println ("the sum of the digits before the Fibonacci sequence" + I + ":" + (e + 1); // This is the sum of the N columns before the output and return c; // return c to m1 after setting the output sequence (that is, where is the output of the main method, because it is called there ,) if the method is not called or not executed, the call is executed and the value is returned to the method call }}}

There may be more than steps and unreasonable points, but it is already the best at this stage. I hope you can provide more guidance. Thank you!

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.