基于Python計算圓周率pi代碼實(shí)例
一 計算公式:
二 實(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)行時間為{:.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))
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 利用XMLSerializer將對象串行化到XML2. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)3. html5手機(jī)觸屏touch事件介紹4. Docker 安裝 Jenkins 并解決初始安裝插件失敗問題5. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件上傳(一)6. HTML-Canvas的優(yōu)越性能以及實(shí)際應(yīng)用7. Ajax對緩存的處理方法實(shí)例分析8. ASP.NET MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值9. ASP.NET Core 依賴注入生命周期示例詳解10. 詳解CSS開發(fā)過程中的20個快速提升技巧
