斐波那契數列在於每個數字都是前兩個值總和的過程,而數列總是以基本整數 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中文網其他相關文章!