[3]编写一函数求1!+2!+3!+…+n!的和,n为参数。python
时间: 2023-11-24 09:50:44 浏览: 67
python计算阶乘和的方法(1!+2!+3!+…+n!)
以下是求1!+2!+3!+…+n!的和的Python函数实现:
```python
def factorial_sum(n):
result = 0
factorial = 1
for i in range(1, n+1):
factorial *= i
result += factorial
return result
```
使用示例:
```python
print(factorial_sum(3)) # 输出:9,即1!+2!+3!=1+2+6=9
```
阅读全文