Writes the 1~n and nth digits of the Fibonacci sequence with a for loop and a recursive call

Source: Internet
Author: User

First Note:

The code is executed from the top down, from left to right!!

This is the m= arbitrary number written for the For loop. Represents how many bits of 1~ and

public class fei_bo_na_qi{
public static void Main (string[] args) {
int m = 30; This represents the 1~30 and
System.out.println ("+m+" of the Fibonacci sequence is: "+m1 (m));//Call function at output
}
public static int M1 (int i) {//Create method
if (i = = 1) {//if if ... Executes the following Java statement if it equals 1
return 0; Return value of 0, return to M1 and end the Java statement
}
if (i = = 2) {//if if ... Executes the following Java statement if it equals 2
return 1; Returns a value of 1 and ends the Java statement
} else {//If you do not ==2 or ==1, execute the Java statement
int a = 0; Here represents the first number of the Fibonacci sequence. Declare a variable with an int data type name A and assign a value of 0
int b = 1; This represents the second number of Fibonacci.
int c = 0; This represents 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 of the operations here, it is equal to 3, and here is equal to 1, so to speak, because it is equal to 1 and 2 in the above output, If we don't subtract 2 here, it's going to loop three times and that's going to affect the result.
Recycle code block
C=a+b; Give the and assignment of A and B to C because each of the first two digits is made up of the first two bits.
E+=c; This is adding up the number of the first n columns.
System.out.println (A + "+" +b+ "Fibonacci sequence of the first" +j+ "bit value is" + "=" +c "); Output a+b=c
A=b; b for the second of the series, a for the first of the series, and C for the third of the series I'm assigning a value to a A is the second one.
B=c; And then assign the C value to b b is equal to the third of the series so the next cycle, C is the fourth of the sequence of the analogy

}
System.out.println ("The former of the Fibonacci sequence" + i + "bit number" and for: "+e"); This is the output of the first n columns and
return C; After the output of the set number of columns and then return the C to the M1 return to the method call out (that is, where the output of the main method, because the call in there,) method does not call do not execute, call execution, and return the value to the method of the call out


}
}
}

It's a recursive write.

public class qian_n_wei_he{
public static void Main (string[] args) {
int i = 30; int data type Create I here represents Fibonacci i-bit number
int he = 0; The following + = indicates the 1~ of the first and
for (int j=i; J >=1;--j) {//for The value of the loop initial value of I can be set at my own discretion
He + = M1 (j); Let the number of J times and he be added to the next one is the sum of the j-1.
}
System.out.println ("Fibonacci 1~" +i+ "the number of bits and for:" +he); Output

}
public static int M1 (int n) {//Create a method
if (n = = 1) {//n==1 Returns a value of 0 otherwise execute the statement
return 0; Back to M1
}
if (n = = 2) {///n==2, the return value is 1 otherwise execute the statement
return 1; Back to M1
}else{//Otherwise execute the statement
Return M1 (N-1) + m1 (n-2); This is the number of the nth digit, which is the number of the I bit we set.
}
}
}

Writes the 1~n and nth digits of the Fibonacci sequence with a for loop and a recursive call

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.