代码: 全选
from time import monotonic
def calc_pi(places=100):
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 = 500):
t1 = monotonic()
calc_pi(n)
t2 = monotonic()
return t2 - t1
print('calc pi 100 bits:', pi_t(100), 's')
print('calc pi 1000 bits:', pi_t(1000), 's')
print('calc pi 5000 bits:', pi_t(5000), 's')
代码: 全选
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.0.0-alpha.1 on 2020-07-03; Saola 1 w/Wroom with ESP32S2
>>> import cpy_pi
calc pi 100 bits: 0.012001 s
calc pi 1000 bits: 0.487999 s
calc pi 5000 bits: 11.232 s
>>>