python1、计算前n个自然数的阶乘之和1!+2!+3!+...+n!的值
时间: 2023-11-24 12:51:54 浏览: 94
python计算阶乘和的方法(1!+2!+3!+...+n!)
5星 · 资源好评率100%
以下是三种Python计算前n个自然数的阶乘之和的方法:
方法一:
```python
n = int(input())
b = 1
sum = 0
for i in range(1, n+1):
b = b * i
sum += b
print(sum)
```
方法二:
```python
i = int(input())
sum = 0
if i < 1:
exit()
else:
while i > 0:
b = 2
c = 1
while b <= i:
c = b * c
b = b + 1
sum += c
i = i - 1
print(sum)
```
方法三:
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n = int(input())
sum = 0
for i in range(1, n+1):
sum += factorial(i)
print(sum)
```
阅读全文