python计算圆周率_Python如何计算圆周率pi Python计算圆周率pi代码实例

本篇文章小编给大家分享一下Python计算圆周率pi代码实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

一 计算公式:

5e7b0374d67b2.png

二 实现代码

(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))

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值