新聞中心
創(chuàng)新互聯(lián)Python教程:

如何使用 While 循環(huán)、For 循環(huán)和遞歸編寫 Python 斐波那契數(shù)列程序?。根據(jù)數(shù)學,斐波那契數(shù)或數(shù)列是 0,1,1,2,3,5,8,13,21,34 …
使用 While 循環(huán)的 Python 斐波那契數(shù)列程序
這個 Python 程序允許用戶輸入任何正整數(shù)。接下來,這個程序使用 While 循環(huán)顯示從 0 到用戶指定數(shù)字的 Python 斐波那契數(shù)列。
Number = int(input("\nPlease Enter the Range : "))
# Initializing First and Second Values
i = 0
First_Value = 0
Second_Value = 1
# Find & Displaying
while(i < Number):
if(i <= 1):
Next = i
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next)
i = i + 1
Please Enter the Range : 4
0
1
1
2
python 中的斐波那契數(shù)列程序允許用戶輸入任意正整數(shù),然后將該數(shù)賦給變量 number。接下來,我們聲明了三個整數(shù)變量 I,第一個值和第二個值以及賦值。
下面的 Python While 循環(huán)確保循環(huán)從 0 開始,并且小于用戶給定的數(shù)字。在 Python 斐波那契數(shù)列程序的 While 循環(huán)中,我們使用了 If 語句。
- 如果 I 值小于或等于 1,則 Next = i
- 如果 I 值大于 1,則在 Else 塊內(nèi)執(zhí)行計算。
讓我們從迭代的角度來看 python 例子中斐波那契數(shù)列中 while 循環(huán)的工作原理。在這個斐波那契數(shù)列的例子中,用戶輸入的值:數(shù)字= 4,i = 0,第一個值= 0,第二個值= 1
Python While Loop 第一次迭代
- 而(0 < 4)為真。因此,程序在這段時間內(nèi)開始執(zhí)行語句。
- 在 while 循環(huán)中,我們有 If 語句和條件 if (0 < = 1)為真。因此,Next = 0,編譯器從 if 語句塊退出。
- 打印對帳單打印(下一步)打印值 0。
- 最后,我增加到 1。
Python 斐波那契數(shù)列邊循環(huán)邊二次迭代
- 而(1 < 4)為真。
- 不一會兒,我們有了 Python If 語句和條件 if (1 < = 1)為真。因此,Next = 1,編譯器從 if 語句塊退出。
- 打印對帳單打印(下一步)打印值 1。
- 我數(shù)到 1。
第三次迭代:While (2 < 4) is TRUE in this Fibonacci series in python. The condition if (2 <= 1) is FALSE, so statements inside the else block to start executing.
下一個=第一個 值+第二個 值 下一個= 0 + 1 = 1 第一個 值=第二個 值= 1 第二個 _ 值=下一個= 1
接下來,打印語句打印(下一步)打印值 1。最后,我增加到 1
第四次迭代:while (3 < 4) is TRUE. So, the Python 程序開始執(zhí)行 while 內(nèi)部的語句。
條件是(3 <= 1) is FALSE 下一個= 1 + 1 = 2 第一個 值=第二個 值= 1 第二個 _ 值=下一個= 2
接下來,打印語句打印(下一步)打印值 2。最后,我增加到 1
第五次迭代:While (4 < 4) is FALSE so, it exits from the while loop.
下一個值的最終輸出是:0 1 1 2
Python 中使用 For 循環(huán)的斐波那契數(shù)列
這個 Python 程序使用 For Loop 顯示從 0 到用戶指定數(shù)字的斐波那契數(shù)列。
# It will start at 0 and travel upto below value
Number = int(input("\nPlease Enter the Range : "))
# Initializing First and Second Values
First_Value = 0
Second_Value = 1
# Find & Displaying
for Num in range(0, Number):
if(Num <= 1):
Next = Num
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next)
Please Enter the Range : 10
0
1
1
2
3
5
8
13
21
34
Python 中使用遞歸的斐波那契數(shù)列
這個 Python 程序使用遞歸概念顯示了從 0 到用戶給定數(shù)字的斐波那契數(shù)列。
# Recursive Function Beginning
def Fibonacci_series(Number):
if(Number == 0):
return 0
elif(Number == 1):
return 1
else:
return (Fibonacci_series(Number - 2)+ Fibonacci_series(Number - 1))
# End of the Function
# Series will start at 0 and travel upto below value
Number = int(input("\nPlease Enter the Range Number: "))
# Find & Displaying Them
for Num in range(0, Number):
print(Fibonacci_series(Num))
在這個斐波納契數(shù)列的 python 程序中使用遞歸的例子,我們定義了一個函數(shù)。以下函數(shù)接受整數(shù)值作為參數(shù)值和返回值。
def Fibonacci_series(Number):
讓我們看看上面指定的函數(shù)里面的 Elif 語句
- if (Number == 0)檢查給定的數(shù)字是否為 0。如果為真,函數(shù)將返回值零。
- if(Number == 1)檢查給定的數(shù)字是否為 1。如果為真,函數(shù)返回值 1。
- 如果這個數(shù)字大于 1,else 塊中的語句就會被執(zhí)行。
在 Else 塊中,我們遞歸調(diào)用函數(shù)來顯示結(jié)果。
return (Fibonacci_series(Number-2)+ Fibonacci_series(Number-1))
為了演示 python 中使用遞歸的斐波那契數(shù)列程序,Number= 2
(斐波那契數(shù)列(數(shù)字-2)+斐波那契數(shù)列(數(shù)字-1))
(斐波那契數(shù)列(2–2)+斐波那契數(shù)列(2–1))
意思是,(斐波那契數(shù)列(0)+斐波那契數(shù)列(1))
return (0 + 1) = return 1
文章標題:Python程序:斐波那契數(shù)列
鏈接分享:http://www.dlmjj.cn/article/cddpejp.html


咨詢
建站咨詢
