算法
使用众所周知的结果:
我们在Python 3中定义:
from math import sin
from functools import reduce
from operator import mul
def integrate(f, a, b, n):
h = (b-a)/n
i = h * sum(f(a+i*h+h/2) for i in range(n))
return i
def sinc(x):
return sin(x)/x
def borwein(n):
def f(x):
g = lambda i: sinc(x/(2*i+1))
return reduce(mul, map(g, range(n)), 1)
return f
测试中
>>> for i in range(1,10):
... pi = 2 * integrate(borwein(i), 0, 1000, 1000)
... print("x[{}] = {}".format(i, pi))
x[1] = 3.140418050361841
x[2] = 3.141593078648859
x[3] = 3.1415926534611547
x[4] = 3.1415926535957164
x[5] = 3.1415926535895786
x[6] = 3.1415926535897953
x[7] = 3.1415926535897936
x[8] = 3.1415926535435896 # ???
x[9] = 3.141592616140805 # ?!!
扰流板
该Borwein积分是恶作剧的数学的想法。虽然上面的标识保持为sinc(x / 13),但下一个值实际上是: