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


Posted in Python onMarch 25, 2020

一 计算公式:

基于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))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用PyV8执行javascript代码示例分享
Dec 04 Python
Python实现方便使用的级联进度信息实例
May 05 Python
Python中返回字典键的值的values()方法使用
May 22 Python
python使用两种发邮件的方式smtp和outlook示例
Jun 02 Python
Python实现字典去除重复的方法示例
Jul 31 Python
python sys,os,time模块的使用(包括时间格式的各种转换)
Apr 27 Python
Django ImageFiled上传照片并显示的方法
Jul 28 Python
Python综合应用名片管理系统案例详解
Jan 03 Python
Python基于QQ邮箱实现SSL发送
Apr 26 Python
python异步Web框架sanic的实现
Apr 27 Python
tensorflow2.0的函数签名与图结构(推荐)
Apr 28 Python
python help函数实例用法
Dec 06 Python
Python异常原理及异常捕捉实现过程解析
Mar 25 #Python
Python导入模块包原理及相关注意事项
Mar 25 #Python
Python脚本导出为exe程序的方法
Mar 25 #Python
Python实现猜年龄游戏代码实例
Mar 25 #Python
python读取mysql数据绘制条形图
Mar 25 #Python
Python环境下安装PyGame和PyOpenGL的方法
Mar 25 #Python
python画环形图的方法
Mar 25 #Python
You might like
echo(),print(),print_r()之间的区别?
2006/11/19 PHP
自动生成文章摘要的代码[PHP 版本]
2007/03/20 PHP
php中批量替换文件名的实现代码
2011/07/20 PHP
PHP的简易冒泡法代码分享
2012/08/28 PHP
PHP实现的简单AES加密解密算法实例
2017/05/29 PHP
laravel 实现上传图片到本地和前台访问示例
2019/10/21 PHP
硬盘浏览程序,保存成网页格式便可使用
2006/12/03 Javascript
jquery Mobile入门—多页面切换示例学习
2013/01/08 Javascript
div拖拽插件——JQ.MoveBox.js(自制JQ插件)
2013/05/17 Javascript
js页面跳转的问题(跳转到父页面、最外层页面、本页面)
2013/08/14 Javascript
固定网页背景图同时保持图片比例的思路代码
2013/08/15 Javascript
js实现百度联盟中一款不错的图片切换效果完整实例
2015/03/04 Javascript
nodejs服务搭建教程 nodejs访问本地站点文件
2017/04/07 NodeJs
利用Javascript获取选择文本所在的句子详解
2017/12/03 Javascript
JavaScript重复元素处理方法分析【统计个数、计算、去重复等】
2017/12/14 Javascript
Bootstrap的aria-label和aria-labelledby属性实例详解
2018/11/02 Javascript
微信小程序实现弹出层效果
2020/05/26 Javascript
Vue实现一个图片懒加载插件
2019/03/11 Javascript
[01:08]DOTA2次级职业联赛 - Wings 战队宣传片
2014/12/01 DOTA
[01:46]新英雄登场
2019/09/10 DOTA
Python操作sqlite3快速、安全插入数据(防注入)的实例
2014/04/26 Python
解决PyCharm控制台输出乱码的问题
2019/01/16 Python
pandas DataFrame 交集并集补集的实现
2019/06/24 Python
PyTorch中topk函数的用法详解
2020/01/02 Python
Django 用户登陆访问限制实例 @login_required
2020/05/13 Python
Sneaker Studio匈牙利:购买运动鞋
2018/03/26 全球购物
化石印度尼西亚在线商店:Fossil Indonesia
2019/03/11 全球购物
澳大利亚实惠时尚女装商店:Katies
2019/06/16 全球购物
应用心理学个人求职信范文
2013/12/11 职场文书
党的群众路线教育实践活动心得体会
2014/03/03 职场文书
求职自荐信怎么写
2014/03/06 职场文书
党员公开承诺践诺书
2014/03/25 职场文书
优秀德育工作者事迹材料
2014/05/07 职场文书
辞旧迎新演讲稿
2014/09/15 职场文书
CSS 实现多彩、智能的阴影效果
2021/05/12 HTML / CSS
JavaScript继承的三种方法实例
2021/05/12 Javascript