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)

python求圆周率Π_第1张图片

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

python求圆周率Π_第2张图片

转载于:https://www.cnblogs.com/SkyOceanchen/p/11202487.html

你可能感兴趣的:(python求圆周率Π)