如何使用 While 循環、For 循環和遞歸編寫 Python 斐波那契數列程序?。根據數學,斐波那契數或數列是 0,1,1,2,3,5,8,13,21,34 …
使用 While 循環的 Python 斐波那契數列程序
這個 Python 程序允許用戶輸入任何正整數。接下來,這個程序使用 While 循環顯示從 0 到用戶指定數字的 Python 斐波那契數列。
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 中的斐波那契數列程序允許用戶輸入任意正整數,然後將該數賦給變量 number。接下來,我們聲明了三個整數變量 I,第一個值和第二個值以及賦值。
下面的 Python While 循環確保循環從 0 開始,並且小於用戶給定的數字。在 Python 斐波那契數列程序的 While 循環中,我們使用了 If 語句。
- 如果 I 值小於或等於 1,則 Next = i
- 如果 I 值大於 1,則在 Else 塊內執行計算。
讓我們從迭代的角度來看 python 例子中斐波那契數列中 while 循環的工作原理。在這個斐波那契數列的例子中,用戶輸入的值:數字= 4,i = 0,第一個值= 0,第二個值= 1
Python While Loop 第一次迭代
- 而(0 < 4)為真。因此,程序在這段時間內開始執行語句。
- 在 while 循環中,我們有 If 語句和條件 if (0 < = 1)為真。因此,Next = 0,編譯器從 if 語句塊退出。
- 打印對帳單打印(下一步)打印值 0。
- 最後,我增加到 1。
Python 斐波那契數列邊循環邊二次迭代
- 而(1 < 4)為真。
- 不一會兒,我們有了 Python If 語句和條件 if (1 < = 1)為真。因此,Next = 1,編譯器從 if 語句塊退出。
- 打印對帳單打印(下一步)打印值 1。
- 我數到 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 程序開始執行 while 內部的語句。
條件是(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 循環的斐波那契數列
這個 Python 程序使用 For Loop 顯示從 0 到用戶指定數字的斐波那契數列。
# 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 中使用遞歸的斐波那契數列
這個 Python 程序使用遞歸概念顯示了從 0 到用戶給定數字的斐波那契數列。
# 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))
在這個斐波納契數列的 python 程序中使用遞歸的例子,我們定義了一個函數。以下函數接受整數值作為參數值和返回值。
def Fibonacci_series(Number):
讓我們看看上面指定的函數裡面的 Elif 語句
- if (Number == 0)檢查給定的數字是否為 0。如果為真,函數將返回值零。
- if(Number == 1)檢查給定的數字是否為 1。如果為真,函數返回值 1。
- 如果這個數字大於 1,else 塊中的語句就會被執行。
在 Else 塊中,我們遞歸調用函數來顯示結果。
return (Fibonacci_series(Number-2)+ Fibonacci_series(Number-1))
為了演示 python 中使用遞歸的斐波那契數列程序,Number= 2
(斐波那契數列(數字-2)+斐波那契數列(數字-1))
(斐波那契數列(2–2)+斐波那契數列(2–1))
意思是,(斐波那契數列(0)+斐波那契數列(1))
return (0 + 1) = return 1
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/228817.html