Pi calculate any decimal places (theory, see the specific performance of the computer, time calculation may not be accurate)
# - * - Coding: UTF-. 8 - * - from __future__ Import Division # ################### introduction time module Import Time # ######## ###### calculates the current time TIME1 = the time.time () # ############### ########### algorithm according to formula Ma Qinggong calculate pi ######### Number = int (iNPUT ( ' enter calculating a desired number of bits after the decimal point n: ' )) # affect multiple calculation 10, to prevent the choice of the mantissa number1 Number = 10 + # operator number1 to one decimal place B = 10 ** number1 # containing a first item of 4/5 X1 = B *. 4. 5 // # find the first entry containing 1/239 X2 = -239 B // # seeking the largest item he = x1 + X2 # Set the end of the following cycle, i.e., a total of n items calculated Number = 2 * # cycle the initial value = 3, the end value of 2n, step 2 = for I in Range (3, Number, 2 ): # find each containing 1 items and symbols / 5 X1 = -25 // # find each item containing 1/239 and symbols X2 = -57 121 // # seek a sum of two X = (X1 + X2) // I # for obtaining the sum he = + X # is obtained [pi] PAI of He * =. 4 # after ten give it PAI // = 10 ** 10 # ########### output value of pi [pi] paistring = STR (PAI) Result paistring = [0] + STR ( ' . ' ) + paistring [. 1 : len (paistring)] Print (Result) TIME2 = the time.time () Print (U ' Total Processed: ' + STR (TIME2 - TIME1) + ' S ' )
Only to learn reference