标签:pow int time rand 利用 过程 圆周率 class form
目录
圆周率的近似计算公式
\[ \pi = \sum_{k=0}^\infty[\frac{1}{16^k}(\frac{4}{8k+1}-\frac{2}{8k+4}-\frac{1}{8k+5}-\frac{1}{8k+6})] \]
圆周率的近似计算公式
\[ \pi = \sum_{k=0}^\infty[\frac{1}{16^k}(\frac{4}{8k+1}-\frac{2}{8k+4}-\frac{1}{8k+5}-\frac{1}{8k+6})] \]
# CalPiV1.py
pi = 0
N = 10
for k in range(N):
pi += 1 / pow(16, k) * (4 / (8 * k + 1) - 2 / (8 * k + 4) - 1 /
(8 * k + 5) - 1 / (8 * k + 6))
print("圆周率值是: {}".format(pi))
圆周率值是: 3.1333333333333333
圆周率值是: 3.1414224664224664
圆周率值是: 3.1415873903465816
圆周率值是: 3.1415924575674357
圆周率值是: 3.1415926454603365
圆周率值是: 3.141592653228088
圆周率值是: 3.141592653572881
圆周率值是: 3.141592653588973
圆周率值是: 3.1415926535897523
圆周率值是: 3.1415926535897913
# CalPiV2.py
from random import random
from time import perf_counter
DARTS = 1000 * 1000
hits = 0.0
start = perf_counter()
for i in range(1, DARTS + 1):
x, y = random(), random()
dist = pow(x**2 + y**2, 0.5)
if dist <= 1.0:
hits = hits + 1
pi = 4 * (hits / DARTS)
print("圆周率值是: {}".format(pi))
print("运行时间是: {:.5f}s".format(perf_counter() - start))
圆周率值是: 3.141148
运行时间是: 0.77535s
标签:pow int time rand 利用 过程 圆周率 class form
原文地址:https://www.cnblogs.com/nickchen121/p/11185034.html