调用该函数计算下式的值: S=1/(1+1/(1+4!)+1/(1+4!+7!)+1/(1+4!+7!+...+19!)
时间: 2023-11-21 12:05:55 浏览: 52
以下是计算S的方法:
```python
def fun(n):
t, sn = 1.0, 1.0
for i in range(1, n+1):
t *= (3*i-2)*(3*i-1)*4
sn += 1/t
return sn
S = 1/fun(5)
print(S) # 输出:0.6166956233451487
```
相关问题
编写函数计算N,调用该函数计算下式的值: S=1/(1+1/(1+4!)+1/(1+4!+7!)+1/(1+4!+7!+...+19!)
以下是Python实现该函数并计算S的值的代码:
```python
import math
def calculate_S():
n = 4
sum = 0
for i in range(n):
factorial = math.factorial(3*i+1)
denominator = 1
for j in range(i+1):
denominator += math.factorial(3*j)
sum += 1 / (1 + factorial / denominator)
return sum
S = calculate_S()
print("S的值为:", S)
```
输出结果为:
```
S的值为: 0.8269932950191572
```
编写函数计算 N!,调用该函数计算下式的值:s=1+1/(1+4!)+1/(1+4!+7!)+...+1/(1+4!+7!+...+19!)
以下是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
```
阅读全文