from functools import reduce
def pi(N):
jishu = itertools.count(1,2)
jslist=itertools.takewhile(lambda x: x <= N*2 - 1 , jishu)
return reduce(lambda x, y: x + (-1)**((y % 4 - 1) / 2) / y, jslist) * 4
'''
reduce的作用是最后的加和,观察正负号,注意到1,3,5,7对应1,-1,1,-1,而1=(-1)**0,-1=(-1)**1,
而1,3,5,7对4求余分别是1,3,1,3,所以可以用(余数-1)/2得到0,1,0,1,从而符号正确
Sign in to make a reply
bzny虫
from functools import reduce
def pi(N):
jishu = itertools.count(1,2)
jslist=itertools.takewhile(lambda x: x <= N*2 - 1 , jishu)
return reduce(lambda x, y: x + (-1)**((y % 4 - 1) / 2) / y, jslist) * 4
'''
reduce的作用是最后的加和,观察正负号,注意到1,3,5,7对应1,-1,1,-1,而1=(-1)**0,-1=(-1)**1,
而1,3,5,7对4求余分别是1,3,1,3,所以可以用(余数-1)/2得到0,1,0,1,从而符号正确
'''