斐波那契數列 PHP

王林
發布: 2024-08-29 13:12:20
原創
592 人瀏覽過

用外行的語言來說,斐波那契數列就是將前兩個元素相加形成下一個元素,直到得到所需的數列大小而形成或獲得的一系列元素。我們通常從 0 和 1 開始斐波那契數列。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

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

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

系列一旦形成,如下:

0, 1, 1, 2 ,3, 5, 8, 13, 21, 34

如上所述,下一個數字是前兩個數字相加而成的。

  • 上述序列中第4 個 位置(第n 位置)上的「2」是將其前面的兩個數字相加而獲得的[ [n- 1]和n-2]), 1.
  • ‘3’是將其前面的兩個數字 2 相加得到的。
  • ‘5’是將其前面的兩個數字 3 相加得到的。
  • 等等。

PHP 中的斐波那契系列及其邏輯

在這裡,我們將看到在 PHP 環境中工作時具體獲取斐波那契數列。差異在於我們編碼的格式,即使用 PHP 腳本的起始標籤及其結束標籤。

<?php
…;
…;
…;
?>
登入後複製

這將幫助您理解和學習如何在 PHP 中使用迭代方式和遞歸方式兩種方法來產生斐波那契數列。

當給我們一個數字,即「n」(系列大小)時,我們將嘗試找到不超過給定數字的斐波那契系列。

例如,如果我們需要為 n=5 建立斐波那契,我們將顯示元素直到第 5 項。

範例#1

  • 輸入:n = 9
  • 輸出:0 1 1 2 3 5 8 13 21

範例 #2

  • 輸入:n=13
  • 輸出: 0 1 1 2 3 5 8 13 21 34 55 89 144

PHP 中的邏輯

邏輯與上述相同。這裡我們給定 n=10,即我們需要找到直到第 n 項的元素。因此,我們將繼續遵循我們的邏輯,直到我們的系列中有 n 個術語。

讓我們來看看上面給出的範例之一。

在上面的一個範例中,我們有 n=9,邏輯表示:

  • 將第一個數字初始化為 0。
  • 將第二個數字初始化為 1。
  • 列印第一個和第二個數字。
  • 循環從這裡開始。
  • 對於系列中的下一個元素,即第3rd 元素[nth 元素],我們將添加其緊鄰的前兩個數字[(n-1)和(n- 2)] 取得該系列中的下一個數字,如下所示,0 + 1 = 1。

對於 n=3

  • n – 1 = 3 – 1 = 系列的第二個元素 = 1
  • n – 2 = 3 – 2 = 系列的第一個元素 = 0 3rd 元素 = (n-1) + (n-2) = 1 + 0 = 1

因此,級數中的第三個元素是 1。

  • 類似地,根據邏輯,要獲得該系列的第 4 元素 [n],我們需要添加其前面的數字 e。 (n-1) 和 (n-2) 元素。

此時,‘n’等於‘4’:

  • n – 1 = 4 – 1 = 系列的第 3 個元素 = 1
  • n – 2 = 4 – 2 = 系列中的第 2 個元素 = 1 4 元素 = (n-1) + (n-2) = 1 + 1 = 2

因此,我們得到第 4 個 元素為 2。

因此,對於「n」等於 9,按照與上面解釋相同的邏輯,我們得到序列為,斐波那契序列是 0 1 1 2 3 5 8 13 21

使用兩種方法進行斐波那契印刷的 PHP 系列

關於如何用 PHP 編寫程式來列印斐波那契數列,基本上有兩個著名的版本:

  • 沒有遞迴
  • 使用遞迴

像 PHP 中一樣,我們將使用「echo」語句來列印輸出。

1.非遞歸方式

也稱為使用迭代。在這種方法中,我們將從 0 和 1 開始序列。之後我們將列印第一個和第二個數字。接下來我們將使用迴圈開始迭代,這裡我們使用 while 迴圈。

用於列印前 10 個斐波那契數列元素的 PHP 腳本。

代碼:

<?php
function Fibonacci($n)
{
$num1= 0;
$num2= 1;
$counter= 0; while($counter < $n)
{
echo ' '.$num1;
$num3= $num2 + $num1;
$num1= $num2;
$num2= $num3;
$counter= $counter+1;
}
}
//for a pre defined number for Fibonacci.
$n=10; Fibonacci($n);
?>
登入後複製

代碼說明:

  1. Here n is defined as equal to 10, so the logic will run till nth element e. Until we have n=10 elements in the series.
  2. First element is initialized to 0 and second element is initialized to 1, e. num1 = 0 and num2 = 1.
  3. The two elements i.e. num1 and num2 are printed as the first and second elements of the Fibonacci series.
  4. The logic we discussed will be used from here on and our iteration loop starts.
  5. According to our logic, to get num3, we need to add num1 and num2. Since currently num1 = 0 and num2 = 1, num3 comes out as 1.
  6. Now new number obtained is placed in num2 variable and num2 is saved in num1 variable. Basically simple swapping is taking place so that, now num1 is equal to ‘1’ and num2 = newly obtained num3 i.e. ‘1’.
  7. So when the next iteration happens and num3 is obtained by adding current values of num1 and num2, which, according to our PHP script, are as follows:
      • num1 = 1
      • num2 = 1
      • num3 = num1 + num2 = 1 + 1 = 2

Thus we get our next number in the Fibonacci Series.

  1. Similarly, the iteration keeps on till we achieve n = 10, size of the series that was defined in the program itself.

When the above program is executed, we get the output as follows:

斐波那契數列 PHP

2. The Recursion Way

By recursion, we mean the way where the same function is called repeatedly until a base condition is achieved or matched. At this point, recursion is stopped.

The said “function is called repeatedly” phrase points to the section in your code where we will define our logic for the Fibonacci Series.

Below is an example of generating Fibonacci Series in PHP, using If-Else conditions giving way for our recursive approach.

Here is the PHP Scripts for printing the first 15 elements for Fibonacci Series.

<?php
function Fibonacci($num)
{
//If-Else IF will generate first two numbers for the series if($num == 0)
return 0;
else if($num == 1) return 1;
// This is where Recursive way comes in.
//recursive call to get the rest of the numbers in the series else
return(Fibonacci($num -1) + Fibonacci( $num -2));
}
//For a given n=15
$num =15;
for($counter = 0; $counter < $num; $counter++)
{
echo Fibonacci($counter).' ';
}
?>
登入後複製

Code Explanation:

This is the recursive way, which means our function that contains our logic is called again and again for generating the next element in the series until our condition for achieving a specific series size is obtained.

In Iterative approaches, the First and Second element is first initialized and printed. Here we allow a For Loop to give us our first and second elements starting with 0 and 1.

  1. We are using a For loop having a ‘counter’ variable that starts with 0. The For loop works up till the given ‘n’, size for the series.
  2. when loop starts, with the counter variable as 0, we use the recursion approach and call our defined function, Fibonacci ( ).
  3. Here code starts, with If and Else IF condition.
  4. First IF condition checks if ‘num’ variable holds value as ‘0’, if yes, then the code prints or returns ‘0’ as the element for the series.
  5. Similarly, second Else-If condition checks for the value ‘1’, if the ‘num’ variable holds ‘1’ as its value, the program returns it as it is.
  6. Next Else condition recursively calls the Fibonacci function for’ num’ values other than ‘0’ and ‘1’, continuing to reading the values from the For loop counter.

This is where our Fibonacci Logic comes into work and the next number in the sequence is obtained by adding its previous two numbers. Because this is the recursive method, we need to give a counter value to count the recursions equal to nth value, which is being handled by our For Loop.

When the above program or code is executed, the following output is displayed.

斐波那契數列 PHP

The Fibonacci Series does not only appear in mathematics or science calculations but in nature too, have you ever noticed Yellow chamomile flower head.

The Fibonacci Series if plotted on a graph, it forms a spiral called Fibonacci Spiral. It is also one of the gems given by Indian soil. It is found in Indian Mathematics as early as 200 BC in the works done by the mathematician, Pingala. Later Fibonacci introduced the sequence to European countries in his book Liber Abacci in 1200s.

以上是斐波那契數列 PHP的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板