Home  >  Article  >  Java  >  Three ways to implement Fibonacci sequence in java

Three ways to implement Fibonacci sequence in java

王林
王林forward
2019-12-06 12:00:535463browse

Three ways to implement Fibonacci sequence in java

Definition of Fibonacci sequence: Fibonacci sequence refers to a sequence of 1, 1, 2, 3, 5, 8, 13, 21 , 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368..., this sequence starts from the 3rd item, every Each term is equal to the sum of the previous two terms.

The Fibonacci Sequence, also known as the Golden Section Sequence, was introduced by mathematician Leonardoda Fibonacci using the example of rabbit reproduction, so it is also called the "Rabbit Sequence". Mathematically, the Fibonacci sequence is recursively defined as follows: F(0)=0, F(1)=1, F(n)=F(n-1) F(n-2) ( n>=2, n∈N*).

Free online video tutorial sharing: java online video

Now, we use Java to print the first 10 numbers of the Fibonacci sequence:

The first way: direct assignment method

public class PrintFib {
 
	public static void main(String[] args) {
		
		//定义第一个加数a,初始值为1;定义第二个加数b,初始值为1;定义两个加数之和为c,初始值为0
		int a = 1;
		int b = 1;
		int c = 0;
		//首先在控制台打印出数列中第一个数和第二个数的值
		System.out.print(a + "\t" + b + "\t");
		//建立一个for循环,用于循环输出数列中第三位至第十位的数字
		for (int i = 3; i <= 10; i++) {
			//第三个数即为c,a+b等于c的值
			c = a + b;
			//将第一个加数a赋值为数列中的第二个数b的值
			a = b;
			//将第二个加数b赋值为数列中的第三个数c的值
			b = c;
			//在第二次循环打印时,将打印数列中的第四个数为:b + c = b + (a + b) 
			System.out.print(c + "\t");
		}
	}
}

This 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++) {	
			//循环打印a,b两个数,即两个两个打印
			System.out.print(a + "\t" + b + "\t");
			//打印第三、四个数
			a = a + b;
			b = a + b;		
		}
	}
}

The second way: create and print the array

public class PrintFib {
	
	public static void main(String[] args) {
		
		//建立一个长度为10的数组用于存放数列中的数
		int[] arr = new int[10];
		//先定义数列中的第一个和第二个数
		arr[0] = 1;
		arr[1] = 1;
		//建立一个for循环,打印数组中的元素
		for(int i = 0;i < arr.length;i++) {
			//判断:当打印第三个数前,给第三个数赋值
			if(i > 1) {
				arr[i] = arr[i - 2] + arr[i - 1];
			}
			System.out.print(arr[i] + "\t");
		}
		
	}
}

The third way: calling the function

public class PrintFib {
	
	//建立一个函数,用于计算数列中的每一项
	public static int fib(int num) {
		//判断:是否是第一个数和第二个数
		if(num == 1 || num == 2) {
			return 1;
		}else {
			//循环调用本函数
			return fib(num - 2) + fib(num - 1);
		}
	}
	
	//主函数(程序入口)
	public static void main(String[] args) {
		
		//建立一个for循环,用于打印第一个至第十个数字
		for(int i = 1;i <= 10;i++) {
			//调用函数进行打印
			System.out.print(fib(i) + "\t");
		}	
	}
	
}

For more related articles and tutorials, please visit: java introductory learning

The above is the detailed content of Three ways to implement Fibonacci sequence in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:How to open java jar fileNext article:How to open java jar file