计算1!+2!+3!+…+n!
时间: 2023-10-30 08:09:35 浏览: 61
python计算阶乘和的方法(1!+2!+3!+…+n!)
要计算1!+2!+3!+…+n!,可以使用循环来实现。具体代码如下:
```
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
def sum_of_factorials(n):
result = 0
for i in range(1, n+1):
result += factorial(i)
return result
n = 5
print(sum_of_factorials(n)) # 输出153
```
阅读全文