1)python斐波那切数列1,1,2,3,5,8,13,21,…,编程求第n个斐波那契数列的数:
def fbql(x):
if x == 1 or x ==2:
return 1
else:
return fbql(x-1)+fbql(x-2)
2)n的阶乘(递归实现):
def fact(n):
if n == 0:
return 1
else:
return n*fact(n-1)
number = eval(input("请输入正整数:"))
result = fact(number)
print("{}的阶乘是{}".format(number,result))
##print("%d的阶乘是%d"%(number,result))
3)n的阶乘(迭代实现):
def fact1(n):
if n == 0:
return 1
else:
result = n
for i in range(1,n):
result = n*i
return result
number = eval(input("请输入正整数:"))
result = fact(number)
print("{}的阶乘是{}".format(number,result))
4)n的阶乘和(用递归实现):
def fact(n):
if n == 0:
return 1
else:
return n*fact(n-1)
number = eval(input("请输入正整数:"))
sum = 0
for i in range (0,number+1):
sum = sum + fact(i)
print(i,fact(i),sum)
print(sum)