描述
用python计算圆周率PI
1.要求能算到小数点后面越多越好(5分)
2.并用进度条提示算的进度,,能给出多种进度条越好(5分)
3.要求给出算圆周率Pi具体公式或者算法说明。
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()))
tqdm库
from math import * from tqdm import tqdm from time import * total,s,n,t=0.0,1,1.0,1.0 clock() while(fabs(t)>=1e-6): total+=t n+=2 s=-s t=s/n k=total*4 print("π值是{:.10f} 运行时间为{:.4f}秒".format(k,clock())) for i in tqdm(range(101)): print("\r{:3}%".format(i),end="") sleep((clock())/100) #用执行程序的总时间来算出进度条间隔的时间