Three ways to print Fibonacci sequences using Java __java

Source: Internet
Author: User

Definition of the Fibonacci sequence (Fibonacci sequence) : The Fibonacci sequence refers to such a sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1 597,2584,4181,6765,10946,17711,28657,46368 ..., this series begins with the 3rd item, each of which equals the sum of the first two.

The Fibonacci sequence, also known as the Golden Section sequence, was introduced by the mathematician Leonardo's Fibonacci (Leonardoda Fibonacci) as an example of rabbit breeding, which is called the "rabbit sequence". Mathematically, the Fibonacci sequence is defined as the following recursive method: F (0) =0,f (1) =1, f (n) =f (n-1) +f (n-2) (n>=2,n∈n*).


now, we use Java to print the first 10 digits of the Fibonacci series:

The first way: Direct Assignment Method

public class Printfib {public

	static void Main (string[] args) {
		
		//defines the first addends a, the initial value is 1, the second addends B is defined, the initial value is 1; the sum of two addends is defined as C, The initial value is 0
		int a = 1;
		int b = 1;
		int c = 0;
		First print out the value of the first and second numbers in the
		System.out.print (A + "T" + B + "T");
		Establish a For loop for the number of digits in the third to tenth digits in the loop output sequence for
		(int i = 3; I <= i++) {
			//third number is c,a+b equal to c
			= a + b;
			Assigns the first addends A to the value of the second number B in the series
			a = b;
			Assign the second Addends B to the value of C in the third number in the series
			B = C;
			In the second round of printing, the fourth number in the series will be printed: B + c = B + (A + b) 
			System.out.print (c + "T");}}

The method can also be simplified to:

public class Printfib {public
	
	static void Main (string[] args) {
		
		int a = 1;
		int b = 1;	
		for (int i = 1;i <= 5;i++) {	
			//loop print a,b Two number, two two print
			System.out.print (A + "T" + B + "T");
			Print the third to fourth number
			A = a + B;
			b = a + b;}}

The second way: Create and print an array

public class Printfib {public
	
	static void Main (string[] args) {
		
		//Establish an array of length 10 to hold the number in the series
		int[] arr = new int[1 0];
		Define first and second numbers in a series first
		arr[0] = 1;
		ARR[1] = 1;
		Creates a for loop that prints the elements in the array for
		(int i = 0;i < arr.length;i++) {
			//judgment: Assign the third number to the first value
			if (i > 1) {arr[i When printing the third number)
				] = Arr[i-2] + arr[i-1];
			}
			System.out.print (Arr[i] + "\ t");}}

The Third way: calling a function

The public class Printfib {//
	
	creates a function to compute each item in the sequence public
	static int fib (int num) {
		//judge: Whether it is the first number and the second number
		if (num = 1 | | num = 2) {return
			1;
		} else {
			//Loop call this function return
			fib (num-2) + fib (num-1);
		}
	
	The main function (program entry) public
	static void Main (string[] args) {
		
		//creates a for loop that prints the first to tenth digits for
		(int i = 1;i <= 10;i + +) {
			//Call function to print
			System.out.print (fib (i) + "\ t");
	
}}


Related Article

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.