用高精度计算出 �=1!+2!+3!+⋯+�!S=1!+2!+3!+⋯+n!(�≤50n≤50)。python
时间: 2023-12-23 07:03:59 浏览: 183
计算 S=1!+2!+3!+...+N!。
```python
def factorial(num):
if num == 0 or num == 1:
return 1
else:
return num * factorial(num - 1)
n = 5 # 代表S=1!+2!+3!+⋯+n!
result = 0
for i in range(1, n + 1):
result += factorial(i)
print(result) # 输出:153
```
```python
# 使用math库中的factorial函数
import math
n = 5 # 代表S=1!+2!+3!+⋯+n!
result = sum(math.factorial(i) for i in range(1, n + 1))
print(result) # 输出:153
```
阅读全文