Home > Backend Development > Python Tutorial > How to use python to calculate pi (code example)

How to use python to calculate pi (code example)

不言
Release: 2019-03-18 09:22:19
forward
24428 people have browsed it

The content of this article is about how to use python to calculate pi (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Introduction to π

Introduction to π

Pi is represented by the Greek letter π (pronounced pài), which is a constant (approximately equal to 3.141592654), which represents the ratio of the circumference and diameter of a circle. It is an infinite non-repeating decimal. In daily life, 3.14 is usually used to represent pi for approximate calculations.

The solution process of π

In 1965, the British mathematician John Wallis published a mathematical monograph in which he derived a formula and found that pi is equal to infinity The product of fractions multiplied together.

In 2015, scientists at the University of Rochester discovered the same formula for pi in quantum mechanical calculations of the energy levels of hydrogen atoms.

On March 14, 2019, Google announced that pi has now reached 31.4 trillion decimal places.

Here I use a formula that I feel is 'good' to solve. It is good because the calculation results are relatively accurate, but the calculation process takes a long time. Let's learn together~~

2. Approximate calculation of π

1. Calculation formula

2. Method explanation

The numerators on the right side of the equation are all 1, and the denominator It is an increasing sequence, starting from the first item, the sign of the odd-numbered terms is positive, and the sign of the even-numbered terms is negative. The larger and smaller the denominator on the right side of the equation is, the more accurate the calculated value of pi is. To put it another way, the more terms on the right side of the equation, the more accurate the calculated value.

3. Code implementation (python)

from math import fabs           #导入数学模块
from time import perf_counter   #导入时间模块

def Bar(i):         #动态文本条
    N = pow(10,level)
    a = int((i/N)*50)
    b = 50 - a
    Y , N = '*' * a , '.' * b
    print("\r计算中:{:3.0f}% [{}->{}] {:.2f}s"
          .format(2*a,Y,N,perf_counter()),end='')
    
level = eval(input('计算Pi精确到小数点后几位数:'))
print('\n{:=^70}'.format('计算开始'))
a,b,pi,tmp = 1,1,0,1
i = 0
'''
a 分子  |  b 分母  |  pi 圆周率
tmp 存储a/b的值    |  i  执行进度
'''
perf_counter()      #开始计时
while (fabs(tmp) >= pow(10,-level)): #计算Pi
    pi += tmp
    b += 2
    a = -a
    tmp = a/b
    i += 2
    Bar(i)          #调用函数,实时显示计算进度

print('\n{:=^70}'.format('计算完成'))
print('\nPi的计算值为:{}'.format(round(pi*4,level))) #输出计算结果
Copy after login

4. Picture example

##As can be seen from the above three pictures, it only takes 14.07 seconds to be accurate to 4 decimal places, 124.61 seconds to be accurate to 6 decimal places, and 850 / 8% = 10625 to be accurate to 8 decimal places. seconds, which is approximately 177 minutes, which is 2.95 hours. This method is good, but it still takes a long time to calculate.

"Pi" is a wonderful and beautiful existence. It is an infinite non-repeating decimal, just like a kind of imperfect beauty, as long as you have the eyes to discover beauty!

In 2011, the International Mathematical Association officially announced that March 14th of each year will be set as the International Mathematics Day. The source is the pi of the ancient Chinese mathematician Zu Chongzhi.

The above is the detailed content of How to use python to calculate pi (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template