Python 使用递归显示斐波那契数列的程序
斐波那契数列:
斐波那契数列是一个整数序列,其前两个数是0和1,其他所有数是通过将前两个数相加而得到的。
例如:0, 1, 1, 2, 3, 5, 8, 13等等…
查看此示例:
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
# take input from the user
nterms = int(input("How many terms? "))
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
输出: