def factorial(n):
result = n
for i in range(1, n):
result *= i
return result
number = int(input("請輸入一個正整數(shù):"))
result = factorial(number)
print("%d的階乘是:%d" %(number, result))
-----
def factorial(n):
if n == 1:
return 1
else:
return n* factorial(n - 1)
number = int(input("請輸入一個正整數(shù):"))
result = factorial(number)
print("%d的階乘是:%d" %(number, result))
斐波那契數(shù)列
def f(n):
if n == 1:
ret = 0
elif n == 2:
ret = 1
else:
ret = f(n-1)+f(n-2)
return ret
print(f(4))
def f(n):
if n == 1:
return 0
elif n == 2:
return 1
return f(n-1)+f(n-2)
print(f(5))
當(dāng)前文章:遞歸求階乘
網(wǎng)站鏈接:
http://weahome.cn/article/jdicos.html