描述
用python计算圆周率PI
1.要求能算到小数点后面越多越好(5分)
2.并用进度条提示算的进度,,能给出多种进度条越好(5分)
3.要求给出算圆周率Pi具体公式或者算法说明
进度条设计参考:https://www.cnblogs.com/dream4567/p/9690828.html
简单直接进度条设计:https://blog.csdn.net/xiaodongxiexie/article/details/70495618
https://blog.csdn.net/qq_40666028/article/details/79335961
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 14 21:35:06 2021
用python计算圆周率PI
1.要求能算到小数点后面越多越好(5分)
2.并用进度条提示算的进度,,能给出多种进度条越好(5分)
3.要求给出算圆周率Pi具体公式或者算法说明
35号
@author: sailingXuan
"""
from random import random
from time import clock
Darts = 100000
hits = 0.0
a = 1
#开始计时
clock()
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
if i == Darts*0.01*a:
print("\r%{} [{}->{}]".format(a,'*'*a,'-'*(10-a)),end="")
a+=3
pi = 4*(hits/Darts)
print("\nPi == {:.7f}".format(pi))
print("运行时间为:{:.4f}s".format(clock()))
2次 运行结果截图: