第一次发表交作业评论,有点紧张。
#计算圆周率可以根据公式:利用Python提供的itertools模块,我们来计算这个序列的前N项和. from functools import reduce import itertools def pi(n): natuals=itertools.count(1,2) #括号内参数代表:1为start,2为step, m=itertools.takewhile(lambda x: x<2*n, natuals) fac=itertools.cycle([1,-1]) return reduce(lambda x,y: x+y ,map(lambda x: (4/x)*next(fac),m))
Sign in to make a reply
黎生
第一次发表交作业评论,有点紧张。