"Python pi calculation" Python calculates the value of Pi Pi to any bit __python

Source: Internet
Author: User
Tags value of pi

I. Demand analysis
Enter the number of digits you want to calculate to the decimal point, and calculate the value of Pi Pi.

Second, the algorithm: MCA Youth formula

π/4=4arctan1/5-arctan1/239

The formula was discovered in 1706 by John Mathews, a professor of British astronomy. He used the formula to calculate the 100-digit pi. The MCA youth formula can get a decimal precision of 1.4 bits per calculation. Because it's calculated by multiplying and dividend are not long integers, it can be easily programmed on a computer.

Three, the Python language to write a request for PI to any bit of the program is as follows:

#-*-Coding:utf-8-*-
__future__ Import Division

################### #导入时间模块
import Time
####### ####### #计算当前时间
time1=time.time ()
############### #算法根据马青公式计算圆周率 #################### number

= Int (raw_ Input (' Please enter the number of digits you want to calculate to the decimal point N: ')

# 10 more digits to prevent the effect of the mantissa trade-off
number1 = number+10

# After the decimal point number1
b = 10**number1

# 4/5 First
x1 = B*4//5
# to find 1/239 of the first
x2 = b// -239

# Find the first big item He
= X1+x2
#设置下面循环的终点, That is, the total number of n items
*= 2

#循环初值 = 3, the last value 2n, step =2 for
i in Xrange (3,number,2):
    # to find each containing 1/5 items and symbols
    x1//= -25< c23/># for each item and symbol containing 1/239
    x2//= -57121
    # Find two and
    x = (x1+x2)//I
    # seek the sum of he
    + x

# to find pi
pai = He*4
#舍掉后十位
pai//= 10**10

############ output pi pi value
paistring=str (PAI)
result=paistring[0]+ Str ('. ') +paistring[1:len (paistring)]
print result

time2=time.time ()
print U ' total time consuming: ' + str (time2-time1) + ' s '

Run Result:

 "D:\Program Files\python27\python.exe" d:/pycharmprojects/learn2017/pi.py Enter the number of digits you want to compute to the decimal point n:20 3.14159265358979323846 total time consuming: 3.55100011826s Process finished with exit code 0 "D:\Program Files\pyth
On27\python.exe "d:/pycharmprojects/learn2017/pi.py Please enter the number of digits you want to calculate after the decimal point n:50 3.14159265358979323846264338327950288419716939937510 Total time consuming: 2.67100000381s Process finished with exit code 0 

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.