您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Python中怎么計算圓周率,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
一 計算公式:
二 實現代碼
(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("\r{: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 = "\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.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("\n π值是{:.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 s=-s t=s/n k=total*4 scale=50 print("".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))
看完上述內容,你們對Python中怎么計算圓周率有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。