基于Python计算圆周率pi代码实例

作者:Jessie- 时间:2021-03-06 05:13:17 

一 计算公式:

基于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("\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))

来源:https://www.cnblogs.com/linjiaxin59/p/12562697.html

标签:Python,计算,圆周率
0
投稿

猜你喜欢

  • python MultipartEncoder传输zip文件实例

    2022-04-20 07:22:57
  • 基于Oracle的高性能动态SQL程序开发

    2010-07-20 13:01:00
  • ASP XMLDom在服务器端操作XML文件的主要方法和实现

    2011-03-09 11:09:00
  • 你是一个职业的页面重构工作者吗?

    2008-09-29 12:07:00
  • Python中使用 Selenium 实现网页截图实例

    2022-07-04 06:28:16
  • php逐行读取txt文件写入数组的方法 <font color=red>原创</font>

    2023-11-14 17:22:07
  • swfupload上传使用代码说明ASP版

    2011-11-27 09:34:32
  • 三种数据库利用SQL语句进行高效果分页

    2008-11-28 14:52:00
  • 详解Laravel模型事件和模型事件在Trait中的使用

    2023-06-17 17:19:28
  • ASP技术访问WEB数据库

    2023-07-11 22:50:09
  • 设计师和美工

    2008-10-27 13:43:00
  • 用XMLHTTPRequest对象进行客户端验证

    2008-03-03 12:38:00
  • AJAX中文乱码解决

    2009-07-03 13:43:00
  • PHP设计模式(八)装饰器模式Decorator实例详解【结构型】

    2023-11-24 05:59:31
  • 介绍讲解MySQL安装下载登录

    2010-10-25 20:34:00
  • 使用CSS3和RGBa创建超酷的按钮

    2009-06-02 12:41:00
  • Python第三方库face_recognition在windows上的安装过程

    2023-07-27 02:51:29
  • python实现会员管理系统

    2023-11-13 19:44:46
  • Javascript调用XML制作连动下拉框

    2007-09-17 12:55:00
  • 常用java正则表达式的工具类

    2023-06-14 09:16:46
  • asp之家 网络编程 m.aspxhome.com