map & reduce & itertools计算pi
Topic sourcedef pi(N):
cc = itertools.cycle([4, -4])
odds = itertools.count(1,2)
ns = list(itertools.takewhile(lambda x: x <= 2*N-1, odds))
pp = reduce(lambda x, y: x + y, map(lambda x: next(cc)/x, ns))
return pp
这种方法更快
- 1
海淀精神病院门房大爷