The method of pi in Python _python

Source: Internet
Author: User
Tags in python

This article describes the Python method of Pi, is a translation from the foreign website of the article, to share with you for your reference.

The implementation methods are as follows:

#_ *_ Coding=utf-8 *_* # {{http://code.activestate.com/recipes/578130/(R5) def Pi (places=10): "" "Computes Pi to give n Number of decimal places parameter places represents the post-decimal point of the PI to return: first expand the 10**8 (10 of eight) times, then the calculation is completed and then reduce the eight times "" "# 3 + 3* ( 1/24) + 3* (1/24) * (9/80) + 3* (1/24) * (9/80) * (25/168) # The numerators 1, 9, ... are given by (2x + 1) ^ 2 # The Deno Minators, are given by (16x^2-24x + 8) Extra = 8 one = ten * * * (Places+extra) T, c, N, na, d, da = 3*one, 
    3*one, 1, 0, 0, #这里的n and D are the numerator and denominator of each item respectively, and Na and Da are numerator and denominator, respectively. The value added to the previous item #这里的//is not the annotation in C + +, but the meaning while T > 1: N, na, d, da = N+na, na+8, D+da, da+32 t = t * N//D C + + t return c//(extra) def picirc (radius, Aspe
  ct_ratio=5): "" "Display the digit of Pi in a circle of given radius radius: radius of the display Aspect_ratio: Adjust the displayed ratio parameter" "" #display_width为各行的显示长度 display_width = Int (RADIUS * aspect_ratio +) Pi_str = repr Pi (int (2 * RADIUS * * 2 * aspect_r Atio)) pos = 0 #cols number of digits to display 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 (#) # # End of Http://code.activestate.com/recipes/578130/}}}

The results displayed are:

                  31415926535897932384 6264338327950288419716939937510582 0974944592307816406286208998628034825342117 067982148086513282306647093844609550582231725 35940 81284811174502841027019385211055596446229489549303819644 28810975665933446128475       
      6482337867831652712019091456485669234 6034861045432664821339360726024914127372458700660631558817488152 09209628292540917153643678925903600113305305488204665213841469519415 116094330572703657595919530921861     
    17381932611793105118548074462379962749     
    5673518857527248912279381830119491298336733624406566430860213949463952247    
   371907021798609437027705392171762931767523846748184676694051320005681271452    
   63560827785771342757789609173637178721468440901224953430146549585371050792279 689258923542019956112129021960864034418159813629774771309960518707211349999998 3729780499510597317328160963185950244594553469083026425223082533446850352619311   
   88171010003137838752886587533208381420617177669147303598253490428755468731159562   
   86388235378759375195778185778053217122680661300192787661119590921642019893809525   
   72010654858632788659361533818279682303019520353018529689957736225994138912497217   
   75283479131515574857242454150695950829533116861727855889075098381754637464939319   
   2550604009277016711390098488240128583616035637076601047101819429555961989467678   
   374494482553797747268471040475346462080466842590694912933136770289891521047521    
    62056966024058038150193511253382430035587640247496473263914199272604269922796    
    782354781636009341721641219924586315030286182974555706749838505494588586926 9956909272107975093029553211653449872027559602364806654991198818347977535 6636980742654252786255181841757467289 0977772793800081647060016145249192 17321721477235014144197356854816136115735255213347574184946843852332 3907394143334547762416862518983569485562099219222184272550254256 887671790494601653466804         
          988627232791786085784383827967976681 45410095388378636095068006422512520511739298489608412848 86269456042419652850222106611863067442786220391949 45047123713786960956364371917287467764657                  

 57 3962413890865832645995813390478027 59009946576407895126

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.