Discuss / Python / 利用Python提供的itertools模块来计算pi的一个无穷级数前N项和:

利用Python提供的itertools模块来计算pi的一个无穷级数前N项和:

Topic source

灿_Clarence

#1 Created at ... [Delete] [Delete and Lock User]

def pi(N):

    odds = itertools.count(1,2)

    odds = itertools.takewhile(lambda x: x <= 2*N-1, odds)

    sum=0

    for n in odds:

        sum=sum+4/n*(-1)**(n//2)

    return sum


  • 1

Reply