求阶乘和
1.循环法
n = input("输入最大值:")
total_result = 0
for i in range(1,int(n)+1):
result = 1
for j in range(1,i+1):
result = result * j
total_result += result
print(total_result)
当然循环法还可以进行优化
n = input("输入最大值:")
total_result = 0
result = 1
for i in range(1,int(n)+1):
result *= i
total_result += result
print(total_result)
2.递归法
n = input("输入最大值:")
def inner_fun(result, j):
if j == 1:
return 1
result = j * inner_fun(result, j - 1)
return result
def out_fun(n):
if n == 1:
return 1
result = inner_fun(1, n)
total_result = result + out_fun(n - 1)
return total_result
print(out_fun(int(n)))