python求圆周率Π
蒙特卡洛方法求Π
import random
count = 0
for i in range(1000000):
x, y = random.random(), random.random()
dist = pow(x ** 2 + y ** 2, 0.5)
if dist < 1:
count += 1
print((count / 1000000)* 4)
pi = 0
k = 0
while True:
pi += (1/(16**k))* \
(4/(8*k+1) - 2/(8*k+4) - 1/(8*k+5) - 1/(8*k+6))
print(pi)
k += 1