圆周率π的计算与进度条

先来看几张图片

   ...............................

由于十六边形太难找了,所以留给读者自己想象。正四边形,正八边形,正十六边形,正三十二边形.......这些图形的顶点到中学的距离都为1,如此当多边形的边的数目到一定大,我们可以把它看做一个圆,多边形的面积就是圆周率,下面是用Python计算π的值,附加进度条

import math
import time 
scale=14
s,m,=1,2
print("执行开始".center(scale//2, "-"))
start = time.perf_counter()
for i in range(scale+1): 
    s=math.sqrt((1-math.sqrt(1-pow(s,2)))/2)
    m=m*2
    a = '*' * i 
    b = '.' * (scale - i)
    c = (i/scale)*100 
    dur = time.perf_counter() - start 
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur))
    time.sleep(0.1)
Pi=s*m
print("Pi值是{}".format(Pi))
print("\n"+"执行结束".center(scale//2,'-'))

运行结果如下

猜你喜欢

转载自www.cnblogs.com/guyuanlin/p/10536470.html