斐波那契数列在于每个数字都是前两个值之和的过程,并且数列始终以基本整数 0 和 1 开始。斐波那契数与黄金比例密切相关。在本主题中,我们将了解 Java 中的斐波那契数列。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
公式:
an = an − 2 + an − 1Fibonacci series for first 21 numbers | ||||||||||||||||||||
F0 | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | F13 | F14 | F15 | F16 | F17 | F18 | F19 | F20 |
0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 | 144 | 233 | 377 | 610 | 987 | 1597 | 2584 | 4181 | 6765 |
以下是斐波那契数列在 Java 中的主要应用:
以下是斐波那契数列程序:
代码:
// Fibonacci series program public class Fibonacci { // main program public static void main(String[] args) { int count = 10, var1 = 0, var2 = 1; System.out.print("First " + count + " terms: "); // Fibonacci series formation loop for (int i = 1; i <= count; ++i) { System.out.print(var1 + " + "); int added_sum= var1 + var2; var1 = var2; var2 = added_sum; } } }
输出:
说明:
代码:
for (int i = 1; i <= count; ++i) { System.out.print(var1 + " + "); int added_sum= var1 + var2; var1 = var2; var2 = added_sum; }
因此,在此实例结束时,对于单个逻辑流,将应用以下事件:
在对给定的需要的值计数执行以下逻辑序列的过程中,可以实现斐波那契数列。
代码:
import java.util.Arrays; public class Main { public static void main(String[] args) { int Count = 15; long[] array = new long[Count]; array[0] = 0; array[1] = 1; for (int x = 2; x < Count; x++) { array[x] = array[x - 1] + array[x - 2]; } System.out.print(Arrays.toString(array)); } }
输出:
说明:
代码:
public class Fibonaccifunction { private static int indexvalue = 0; private static int endPoint = 9; public static void main (String[] args) { int number1 = 0; int number2 = 1; fibonaccifunction(number1, number2); } public static void fibonaccifunction(int number1, int number2) { System.out.println("index value : " + indexvalue + " -> " + number1); if (indexvalue == endPoint) return; indexvalue++; fibonaccifunction(number2, number1+number2); } }
输出:
说明:
代码:
public class Fibonacci_with_conditions { static int number2=1; static int number1=0; static int next=0; public static void Fibonacci_conditions( int number) { if(number<10) { if(number == 0) { System.out.print(" "+number); number++; Fibonacci_conditions (number); } else if(number == 1) { System.out.print(" "+number); number++; Fibonacci_conditions(number); } else{ next=number1+number2; System.out.print(" "+next); number1=number2; number2=next; number++; Fibonacci_conditions(number); } } } public static void main(String[] args) { Fibonacci_conditions(0); } }
输出:
说明:
Code:
import java.util.*; public class Fibonacci_series { public static void main(String[] args) { System.out.println("Input:"); int number= 10,value1=1,value2=0,value3=0; num(number, value1, value2, value3); } public static void num(int number,int value1,int value2,int value3) { if(value1 <= number) { System.out.println(value1); value3=value2; value2=value1; value1=value2+value3; num(number,value1,value2,value3); } } }
Output:
Explanation:
These programs are implied to achieve the Fibonacci series for a given integer value. A largely classified set of techniques are implied in the given list of examples. Techniques like an array-oriented approach and a condition-alone approach are very much peculiar.
以上是Java 中的斐波那契数列的详细内容。更多信息请关注PHP中文网其他相关文章!