python 计算圆周率

版权声明:转载请注释 https://blog.csdn.net/xinshuwei/article/details/84869906

from datetime import datetime

def pi(places=10):
    extra = 8;
    one = 10**(places+extra)
    t, c, n, na, d, da = 3*one, 3*one, 1, 0, 0, 24
    while t > 1:
        n, na, d, da = n+na, na+8, d+da, da+32
        t = t * n // d
        c += t
    return c // (10 ** extra)

带时间间隔

def pi_t(n=10):
    #t1=time.ticks_us()
    t1 = datetime.now()
    t=pi(n)
    #t2=time.ticks_us()
    t2 = datetime.now()
    print('elapsed: ', (t2-t1).microseconds, 'ms')
    return t

猜你喜欢

转载自blog.csdn.net/xinshuwei/article/details/84869906