Home >Backend Development >Python Tutorial >Python example of how to calculate the value of pi to any digit

Python example of how to calculate the value of pi to any digit

不言
不言Original
2018-05-08 11:52:394591browse

This article mainly introduces Python's method of calculating the value of pi to any position, briefly analyzes the calculation principle of pi, and analyzes the relevant operating skills of Python's calculation of pi in the form of examples. Friends in need can refer to the following

The example in this article describes the method of calculating the value of pi to any digit in Python. Share it with everyone for your reference, the details are as follows:

1. Requirements analysis

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

2. Algorithm: Ma Qing formula

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

This formula was discovered by the British astronomy professor John Mathing in 1706. He used this formula to calculate pi to 100 digits. Ma Qing's formula can obtain 1.4 decimal digits of precision for each calculated item. Because the multiplicand and dividend during its calculation are neither larger than a long integer, it can be easily programmed on a computer.

3. Write a program in python language to find the pi to any position as follows:

# -*- coding: utf-8 -*-
from __future__ import pision
####################导入时间模块
import time
###############计算当前时间
time1=time.time()
################算法根据马青公式计算圆周率####################
number = int(raw_input('请输入想要计算到小数点后的位数n:'))
# 多计算10位,防止尾数取舍的影响
number1 = number+10
# 算到小数点后number1位
b = 10**number1
# 求含4/5的首项
x1 = b*4//5
# 求含1/239的首项
x2 = b// -239
# 求第一大项
he = x1+x2
#设置下面循环的终点,即共计算n项
number *= 2
#循环初值=3,末值2n,步长=2
for i in xrange(3,number,2):
  # 求每个含1/5的项及符号
  x1 //= -25
  # 求每个含1/239的项及符号
  x2 //= -57121
  # 求两项之和
  x = (x1+x2) // i
  # 求总和
  he += x
# 求出π
pai = he*4
#舍掉后十位
pai //= 10**10
############ 输出圆周率π的值
paistring=str(pai)
result=paistring[0]+str('.')+paistring[1:len(paistring)]
print result
time2=time.time()
print u'总共耗时:' + str(time2 - time1) + 's'

Run result:

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

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

The running screenshot is as follows:

Related recommendations:

Python implements crawler setting proxy IP Share with the method of pretending to be a browser

The above is the detailed content of Python example of how to calculate the value of pi to any digit. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn