用函数方法编程求1!+2t...…+n!
时间: 2024-06-14 15:03:21 浏览: 57
c语言求1+2+…+n的解决方法
5星 · 资源好评率100%
以下是用函数方法编程求1!+2!+...+n!的Python代码:
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def sum_factorial(n):
sum = 0
for i in range(1, n+1):
sum += factorial(i)
return sum
n = 5
print("1!+2!+...+n!的和为:", sum_factorial(n))
```
阅读全文