基于Python計(jì)算圓周率pi代碼實(shí)例
一 計(jì)算公式:
二 實(shí)現(xiàn)代碼
(1)
import mathfrom tqdm import tqdmimport timetotal,s,n,t=0.0,1,1.0,1.0while(math.fabs(t)>=1e-6): total+=t n+=2 s=-s t=s/nk=total*4print('π值是{:.10f} 運(yùn)行時(shí)間為{:.4f}秒'.format(k,time.clock()))for i in tqdm(range(101)): print('r{:3}%'.format(i),end='') time.sleep(0.1)
(2)
import timeimport 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 = 'r%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.0while(math.fabs(t)>=1e-6): total+=t n+=2 s=-s t=s/nk=total*4start = 371for i in range(start + 1): print(index(i, start), end=’’) time.sleep(0.01)print('n π值是{:.10f}'.format(k))
(3)
import timeimport mathtotal,s,n,t=0.0,1,1.0,1.0while(math.fabs(t)>=1e-6): total+=t n+=2 s=-s t=s/nk=total*4scale=50print(''.center(scale//2,'-'))start = time.perf_counter()for i in range(scale+1): a='*'*i b='.'*(scale-i) c=(i/scale)*100 d=time.perf_counter() - start print('r{:^3.0f}%[{}->{}]{:.2f}s'.format(c,a,b,d),end=’’) time.sleep(0.1)print('n π值是{:.10f}'.format(k))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)2. idea設(shè)置代碼格式化的方法步驟3. 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移4. 關(guān)聯(lián)數(shù)據(jù)入門——RDF應(yīng)用5. Python web如何在IIS發(fā)布應(yīng)用過(guò)程解析6. 一篇文章搞懂Python反斜杠的相關(guān)問(wèn)題7. PHP?redis?Sorted?Set實(shí)現(xiàn)字符串去重代碼示例8. 解決Python 進(jìn)程池Pool中一些坑9. Python scrapy爬取起點(diǎn)中文網(wǎng)小說(shuō)榜單10. XML、DataSet、DataGrid結(jié)合二
