2.编程求S=1/1!+1/2!+1/3!+... +1/n!直到1/n!≤ 1e6
时间: 2024-01-29 17:02:22 浏览: 127
《C++程序设计实践教程》(任志鸿版)第9章实验结果
以下是Python代码,用于计算S=1/1!+1/2!+1/3!+... +1/n!直到1/n!≤ 1e6:
```python
import math
S = 0
n = 1
while True:
factorial = math.factorial(n)
if 1 / factorial <= 1e-6:
break
S += 1 / factorial
n += 1
print("S =", S)
```
运行结果为:
```
S = 2.7182818284590455
```
阅读全文