斐波那契數列 PHP
用外行的語言來說,斐波那契數列就是將前兩個元素相加形成下一個元素,直到得到所需的數列大小而形成或獲得的一系列元素。我們通常從 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); ?>
代碼說明:
- 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.
- First element is initialized to 0 and second element is initialized to 1, e. num1 = 0 and num2 = 1.
- The two elements i.e. num1 and num2 are printed as the first and second elements of the Fibonacci series.
- The logic we discussed will be used from here on and our iteration loop starts.
- 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.
- 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’.
- 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.
- 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:
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.
- 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.
- when loop starts, with the counter variable as 0, we use the recursion approach and call our defined function, Fibonacci ( ).
- Here code starts, with If and Else IF condition.
- 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.
- 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.
- 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.
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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。
