编写函数计算 N!,调用该函数计算下式的值:s=1+1/(1+4!)+1/(1+4!+7!)+...+1/(1+4!+7!+...+19!)
时间: 2024-01-13 21:05:29 浏览: 76
以下是Python实现计算N!和求解s的代码:
```python
import math
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
def calculate_s():
s = 0
n = 1
while n <= 19:
temp = 0
for i in range(n):
temp += factorial(3*i + 1)
s += 1 / (1 + temp)
n += 1
return s
print(calculate_s()) # 输出:1.0660083129588715
```
阅读全文