• 用进度条表示求圆周率的进程


    用python算圆周率

    1.准备第三方库pip

    3.用python语言编写出求圆周率到任意位的程序如下:

    复制代码
    from math import *
    from tqdm import tqdm
     from time import *
    total,s,n,t=0.0,1,1.0,1.0 clock()
     while(fabs(t)>=1e-6):
    total+=t n+=2 s=-s t=s/n k=total*4 print("π值是{:.10f}
     运行时间为{:.4f}秒".format(k,clock())) for i in tqdm(range(101)): print("
    {:3}%".format(i),end="") sleep((clock())/100)#用执行程序的总时间来算出进度条间隔的时间
    复制代码

    4.在cmd中运行如图所示:

    在IDLE中运行执行如下代码:

    在IDLE中运行执行如下代码:

    复制代码
    # -*- coding: utf-8 -*-
    from __future__ import division
     
    ####################导入时间模块
    import time
    ###############计算当前时间
    time1=time.time()
    ################算法根据马青公式计算圆周率####################
     
    number = int(input('请输入想要计算到小数点后的位数n:'))
     
    # 多计算10位,防止尾数取舍的影响
    number1 = number+10
     
    # 算到小数点后number1位
    b = 10**number1
     
    # 求含4/5的首项
    x1 = b*4//5
    # 求含1/239的首项
    x2 = b// -239
     
    # 求第一大项
    he = x1+x2
    #设置下面循环的终点,即共计算n项
    number *= 2
     
    #循环初值=3,末值2n,步长=2
    for i in range(3,number,2):
        # 求每个含1/5的项及符号
        x1 //= -25
        # 求每个含1/239的项及符号
        x2 //= -57121
        # 求两项之和
        x = (x1+x2) // i
        # 求总和
        he += x
     
    # 求出π
    pai = he*4
    #舍掉后十位
    pai //= 10**10
     
    ############ 输出圆周率π的值
    paistring=str(pai)
    result=paistring[0]+str('.')+paistring[1:len(paistring)]
    print (result)
     
    time2=time.time()
    print (u'总共耗时:' + str(time2 - time1) + 's')
  • 相关阅读:
    Zero-shot Relation Classification as Textual Entailment (Abiola Obamuyide, Andreas Vlachos, 2018)阅读笔记:Model
    高阶Erlang:超大只的问答房间
    高阶的Parser:可变运算优先级
    Erlang练习2:火烈鸟
    Erlang实现的模拟kaboose(山寨kahoot)
    Prolog模拟社交圈
    08-bootcss
    07-jQuery
    06-字符串、表单form、input标签
    05-有名/无名函数
  • 原文地址:https://www.cnblogs.com/wxyzzj/p/10575536.html
Copyright © 2020-2023  润新知