基于基于Python计算圆周率计算圆周率pi代码实例代码实例
主要介绍了基于Python计算圆周率pi代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有
一定的参考学习价值,需要的朋友可以参考下
一 计算公式:
二 实现代码
(1)
import math
from tqdm import tqdm
import time
total,s,n,t=0.0,1,1.0,1.0
while(math.fabs(t)>=1e-6):
total+=t
n+=2
s=-s
t=s/n
k=total*4
print("π值是{:.10f} 运行时间为{:.4f}秒".format(k,time.clock()))
for i in tqdm(range(101)):
print("{:3}%".format(i),end="")
time.sleep(0.1)
(2)
import time
import math
class Index(object):
def __init__(self, number=50, decimal=2):
self.decimal = decimal
self.number = number
self.a = 100/number
def __call__(self, now, total):
percentage = self.percentage_number(now, total)
well_num = int(percentage / self.a)
progress_bar_num = self.progress_bar(well_num)
result = "%s %s" % (progress_bar_num, percentage)
return result
def percentage_number(self, now, total):
return round(now / total * 100, self.decimal)
def progress_bar(self, num):
well_num = "#" * num
space_num = " " * (self.number - num)
return '[%s%s]' % (well_num, space_num)
index = Index()
total,s,n,t=0.0,1,1.0,1.0
while(math.fabs(t)>=1e-6):
total+=t
n+=2
s=-s
t=s/n
k=total*4
start = 371
for i in range(start + 1):
print(index(i, start), end='')
time.sleep(0.01)
print(" π值是{:.10f}".format(k))
(3)
import time
import math
total,s,n,t=0.0,1,1.0,1.0
while(math.fabs(t)>=1e-6):
total+=t
n+=2
评论0