首頁 > Java > java教程 > 主體

Java 中的斐波那契數列

WBOY
發布: 2024-08-30 16:25:45
原創
482 人瀏覽過

斐波那契數列在於每個數字都是前兩個值總和的過程,而數列總是以基本整數 0 和 1 開始。斐波那契數與黃金比例密切相關。在本主題中,我們將了解 Java 中的斐波那契數列。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

公式:

an = an − 2 + an − 1
Fibonacci 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
前 21 個數字的斐波那契數列 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 中的主要應用:

  • 英里到公里以及公里到英里的轉換。
  • 敏捷方法論的一些實例。
  • Euclid 演算法運行時分析計算就是使用該系列技術進行的。
  • 斐波那契統計數據在數學上被一些偽隨機數產生器磨損。
  • 撲克規劃過程涉及這種技術的使用。
  • 斐波那契堆的資料結構技術是利用斐波那契級數技術實現的。
  • 在光學中,當一束光在兩個由不同折射率的不同材料製成的半透明板從頭到尾堆疊的一個視點處閃爍時,它可能會從三個表面返回:頂部表面、中心表面和底面兩個盤子。具有 k 反射的不同光路徑的數量,對於 k > 1,{display style k} 是斐波那契數。

斐波那契數列程式(非遞迴程式)

以下是斐波那契數列程式:

代碼:

// 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;
}
}
}
登入後複製

輸出:

Java 中的斐波那契數列

說明:

  • 程式計算給定數字範圍的斐波那契數列。
  • 這裡這個過程是不使用遞歸技術來實現的。

程式演算法

  • 宣告根類別 Fibonacci 時,嵌入到該類別中的所有程式碼都必須解決實作 Fibonacci 數列的功能。
  • 在根類別內部,宣告了 main 方法。通常,main 方法充當重要的 Java 方法。如果程式中沒有 main 方法,JVM 就不會執行。 main方法各子組件的解釋如下圖所示,
  • 接下來隱含變數初始化部分。本節涉及三個不同變數的初始化。其中兩個是透過變數層級值交換來實現斐波那契邏輯,另一個變數用於調節需要產生斐波那契邏輯的值的數量。
  • 斐波那契數列程式的關鍵邏輯是使用下面給出的程式部分中的 for 迴圈來實現的。

代碼:

for (int i = 1; i <= count; ++i)
{
System.out.print(var1 + " + ");
int added_sum= var1 + var2;
var1 = var2;
var2 = added_sum;
}
登入後複製
  • 循環部分背後的邏輯如下;最初,在循環上執行一系列值;循環發生時每個流的範圍值都會增加。此外,在每個流程中,兩個交換變數的值都會匯總到第三個變數中。
  • 求和後,第二個變數值被隱含到第一個變數中,因此這使得第一個變數值被從這個過程中沖走。在下一步中,將求和值分配給第二個變數。

因此,在此實例結束時,對於單一邏輯流,將套用下列事件:

  • 第一個變數的值被沖走。
  • 現有的第二個變數值填入第一個變數。
  • 求和值移入第二個變數。

在對給定的需要的值計數執行以下邏輯序列的過程中,可以實現斐波那契數列。

1.斐波那契數列程式(使用陣列)

代碼:

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));
}
}
登入後複製

輸出:

Java 中的斐波那契數列

說明:

  •  暗示上面起草的程式邏輯,但在本例中,斐波那契輸入儲存為陣列的一部分。
  • 所以上面提到的所有操作都是針對陣列進行的。

2.斐波那契數列程式(不暗示任何循環)

代碼:

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);
}
}
登入後複製

輸出:

Java 中的斐波那契數列

說明:

  •  暗示上面起草的程序邏輯,但在本例中,斐波那契輸入使用名為斐波那契的函數遞歸處理。

3.斐波那契數列程式(不暗示任何循環,但僅使用條件實作)

代碼:

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);
}
}
登入後複製

輸出:

Java 中的斐波那契數列

說明:

  • Implying the program logic drafted above, but at this instance, the Fibonacci inputs are regulated only through necessary conditional statements.
  • According to the conditions, the swapping of the variables is necessarily carried out.

4. Fibonacci Series Program( Without Loops, the Concepts of Looping is Achieved using Nextint Method)

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:

Java 中的斐波那契數列

Explanation:

  • Implying the program logic drafted above, but at this instance, the Fibonacci inputs are handled recursively using a function named num, and the loop is carried out using the nextInt function.

Conclusion – Fibonacci Series in Java

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中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!