1. 使用`math`模块中的`e`常量:
```python
import math
e = math.e
print(e)
2. 使用级数展开法计算e:
```python
import math
def calculate_e(iterations=30):
e = 0
for i in range(iterations):
e += 1 / math.factorial(i)
return e
e_approx = calculate_e()
print(e_approx)
3. 使用泰勒级数展开法计算e:
```python
from decimal import Decimal, getcontext
Precision = 20000
getcontext().prec = Precision
e = Decimal(0)
nFrac = Decimal(1)
for N in range(1, Precision + 1):
e += nFrac / N
nFrac /= N
print(e)
4. 使用递归函数计算e:
```python
from math import factorial
def compute_e(n):
if n == 0:
return 1
else:
return compute_e(n-1) + 1/factorial(n)
e_approx = compute_e(100) 增加迭代次数以提高精度
print(e_approx)
以上代码展示了使用不同方法计算e的近似值。您可以根据需要选择适合的方法和精度