脚本专栏 
首页 > 脚本专栏 > 浏览文章

python求pi的方法

(编辑:jimmy 日期: 2024/11/28 浏览:3 次 )

本文实例讲述了python求pi的方法,是一篇翻译自国外网站的文章,分享给大家供大家参考。

具体实现方法如下:

#_*_ coding=utf-8 *_*
## {{{ http://code.activestate.com/recipes/578130/ (r5)
def pi(places=10):
  """Computes pi to given number of decimal places
    参数places表示要返回的pi的小数点后位数  
    方法:先整体扩大10**8(10的八次方)倍,然后计算完成后再缩小10的八次方倍
  """
  
  # 3 + 3*(1/24) + 3*(1/24)*(9/80) + 3*(1/24)*(9/80)*(25/168)
  # The numerators 1, 9, 25, ... are given by (2x + 1) ^ 2
  # The denominators 24, 80, 168 are given by (16x^2 -24x + 8)
  extra = 8
  one = 10 ** (places+extra)
  t, c, n, na, d, da = 3*one, 3*one, 1, 0, 0, 24
  #这里的n 和d 分别为每一项的分子与分母 ,na 和 da 分别为分子和分分母后一项比前一项增加的数值
  #这里的//可不是C++中的注释,而是除的意思
  while t > 1: 
    n, na, d, da = n+na, na+8, d+da, da+32
    t = t * n // d
    c += t
  return c // (10 ** extra)

def picirc(radius, aspect_ratio=5):
  """Display the digit of pi in a circle of given radius
    radius:显示的半径
    aspect_ratio:调节显示的比率参数
  """
  #display_width为各行的显示长度
  display_width = int(radius * aspect_ratio + 10)
  pi_str = repr(pi(int(2 * radius ** 2 * aspect_ratio)))
  pos = 0
  #cols为每一行中要显示的数字个数
  for i in range(2 * radius):
    cols = int(0.5 + aspect_ratio * (radius**2 - (radius-(i+0.5))**2) ** 0.5)
    print(pi_str[pos:pos+cols].center(display_width)) #将产生的pi数值生成的文本列表中相应的位数取出来显示在当前行
    pos += cols

if __name__ == '__main__':
  
  picirc(16)
## end of http://code.activestate.com/recipes/578130/ }}}

显示的结果为:

                  31415926535897932384                  
              6264338327950288419716939937510582              
            0974944592307816406286208998628034825342117            
          06798214808651328230664709384460955058223172535940          
         81284811174502841027019385211055596446229489549303819644         
        288109756659334461284756482337867831652712019091456485669234        
       6034861045432664821339360726024914127372458700660631558817488152       
      09209628292540917153643678925903600113305305488204665213841469519415      
     11609433057270365759591953092186117381932611793105118548074462379962749     
    5673518857527248912279381830119491298336733624406566430860213949463952247     
    371907021798609437027705392171762931767523846748184676694051320005681271452    
   63560827785771342757789609173637178721468440901224953430146549585371050792279    
   689258923542019956112129021960864034418159813629774771309960518707211349999998   
   3729780499510597317328160963185950244594553469083026425223082533446850352619311   
   88171010003137838752886587533208381420617177669147303598253490428755468731159562   
   86388235378759375195778185778053217122680661300192787661119590921642019893809525   
   72010654858632788659361533818279682303019520353018529689957736225994138912497217   
   75283479131515574857242454150695950829533116861727855889075098381754637464939319   
   2550604009277016711390098488240128583616035637076601047101819429555961989467678   
   374494482553797747268471040475346462080466842590694912933136770289891521047521   
   62056966024058038150193511253382430035587640247496473263914199272604269922796    
    782354781636009341721641219924586315030286182974555706749838505494588586926    
    9956909272107975093029553211653449872027559602364806654991198818347977535     
     66369807426542527862551818417574672890977772793800081647060016145249192     
      17321721477235014144197356854816136115735255213347574184946843852332      
       3907394143334547762416862518983569485562099219222184272550254256       
        887671790494601653466804988627232791786085784383827967976681        
         45410095388378636095068006422512520511739298489608412848         
          86269456042419652850222106611863067442786220391949          
            4504712371378696095636437191728746776465757            
              3962413890865832645995813390478027              
                  59009946576407895126                  

希望本文所述对大家的Python程序设计有所帮助。

上一篇:python实现计算资源图标crc值的方法
下一篇:python求crc32值的方法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap