Python implements an example of how to calculate the value of pi to any bit

Source: Internet
Author: User
Tags value of pi
This article mainly introduces Python to calculate the value of Pi Pi to any bit of the method, simple analysis of the principle of pi, and combined with examples of the Python calculation of pi-related operation skills, the need for friends can refer to the next

The example in this paper describes the method of Python to calculate the value of pi to any bit. Share to everyone for your reference, as follows:

First, demand analysis

Enter the number of digits you want to calculate to the decimal point and calculate the value of pi.

Second, the algorithm: MCA Youth formula

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

The formula was discovered in 1706 by the British astronomical Professor John Mathews Qing. He used the formula to calculate the 100-bit pi. The MCA youth formula can get a decimal precision of 1.4 bits per calculation. Because the multiplier and dividend are not raised here integers in the calculation process, it is easy to program on the computer.

Third, the Python language to write a pi to any bit of the program as follows:

#-*-Coding:utf-8-*-from __future__ import pision################### #导入时间模块import time############## #计算当前时间time1 = Time.time () ############### #算法根据马青公式计算圆周率 ################### #number = Int (The Raw_input (' Enter the number of digits you want to calculate to the decimal point N: ') # # 10 bit more compute, Prevent the effect of the mantissa trade-offs Number1 = number+10# after the decimal point number1 B = 10**number1# for the first item with 4/5 x1 = b*4//5# for the first item containing 1/239 x2 = b// -239# Seeking the first major item he = X1+X2 #设置下面循环的终点, that is, a total of n items number *= The initial value of the cycle = 3, the last 2n, step =2for i in Xrange (3,number,2):  # For each containing 1/5 items and symbols  x1//= -25  # for each containing 1 /239 items and symbols  x2//= -57121  # for two items and  x = (x1+x2)//I # to  sum  he + + x# Find Πpai = he*4# out after 10 pai//= 10**10##### ####### Output pi pi value paistring=str (PAI) result=paistring[0]+str ('. ') +paistring[1:len (paistring)]print resulttime2=time.time () print U ' total time: ' + str (time2-time1) + ' s '

Operation Result:

Please enter the number of digits you want to calculate to the decimal point n:20
3.14159265358979323846
Total time: 9.77699995041s

Please enter the number of digits you want to calculate to the decimal point n:50
3.14159265358979323846264338327950288419716939937510
Total time: 2.30099987984s

Run as follows:

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.