如果没有更多信息,使用e的简单连分数展开可能是一个好主意™ ,如Wikipedia所示:
e = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, ...]
可以使用简单的列表推导轻松创建此序列。
为了评估简单的连分数展开式,我们可以以相反的顺序处理列表。
以下代码适用于 Python 2 或 Python 3。
#!/usr/bin/env python
''' Calculate e using its simple continued fraction expansion
See http://stackoverflow.com/q/36077810/4014959
Also see
https://en.wikipedia.org/wiki/Continued_fraction#Regular_patterns_in_continued_fractions
Written by PM 2Ring 2016.03.18
'''
from __future__ import print_function, division
import sys
def contfrac_to_frac(seq):
''' Convert the simple continued fraction in `seq`
into a fraction, num / den
'''
num, den = 1, 0
for u in reversed(seq):
num, den = den + num*u, num
return num, den
def e_cont_frac(n):
''' Build `n` terms of the simple continued fraction expansion of e
`n` must be a positive integer
'''
seq = [2 * (i+1) // 3 if i%3 == 2 else 1 for i in range(n)]
seq[0] += 1
return seq
def main():
# Get the the number of terms, less one
n = int(sys.argv[1]) if len(sys.argv) > 1 else 11
if n < 0:
print('Argument must be >= 0')
exit()
n += 1
seq = e_cont_frac(n)
num, den = contfrac_to_frac(seq)
print('Terms =', n)
print('Continued fraction:', seq)
print('Fraction: {0} / {1}'.format(num, den))
print('Float {0:0.15f}'.format(num / den))
if __name__ == '__main__':
main()
输出
Terms = 12
Continued fraction: [2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8]
Fraction: 23225 / 8544
Float 2.718281835205993
向程序传递 20 的参数以使用 Python 浮点数获得最佳近似值:2.718281828459045
正如 Rory Daulton (& Wikipedia) 提到的,我们不需要反转连分数列表。我们可以正向处理它,但我们还需要 2 个变量,因为我们需要跟踪 2 代分子和分母。这是执行此操作的版本contfrac_to_frac
。
def contfrac_to_frac(seq):
''' Convert the simple continued fraction in `seq`
into a fraction, num / den
'''
n, d, num, den = 0, 1, 1, 0
for u in seq:
n, d, num, den = num, den, num*u + n, den*u + d
return num, den