Calculation of pi π in Python

Table of contents

1. Formula method

2. Monte Carlo method


Pi (Pi) is the ratio of the circumference to the diameter of a circle, usually expressed in Greek letters π Representation is a mathematical constant ubiquitous in mathematics and physics. In daily life, 3.14 is usually used to represent pi for approximate calculations.

π is an irrational number, that isInfinite non-repeating decimals. Many mathematical masters in history spent their entire lives calculating their precise approximations. Around AD 480, Zu Chongzhi, a mathematician in the Southern and Northern Dynasties, used the secant method to accurately measure π to 7 decimal places, giving a deficiency approximation of 3.1415926 and a surplus approximation of 3.1415927. He also obtained two approximate fractional values, the density rate 355/113 and the reduction rate. 22/7. For the next 800 years, its calculated values ​​of π were the most accurate.

With the advent of computers, calculations of π values ​​have developed by leaps and bounds. On August 17, 2021, the American Fun Science website reported that Swiss researchers used a supercomputer to calculate π to 62.8 trillion decimal places in 108 days, setting a record for the most accurate value of this constant to date.

This article briefly introduces both methods.

1. Formula method

In 1995, three American algorithmic scientists Bailey-Borwein-Plouffe jointly proposed a formula that shocked the mathematical world, the famousBBP formula, pushing the π calculation formula to its peak:

The code is as follows:

#Pi calculation formula method

pi=0
N=eval(input())

for k in range(N):
    pi+=1/pow(16,k)*(4/(8*k+1)-2/(8*k+4)-1/(8*k+5)-1/(8*k+6))

print(pi)

When N is assigned a value of 100, the obtained π value is 3.141592653589793, which is very accurate. As the N assignment increases, it may become more and more accurate. Of course, it is accompanied by an extension of the calculation time.

2. Monte Carlo method

Monte Carlo is a place name rather than a person’s name. It is located in the casino city of Monaco and symbolizes probability. This method was proposed by the famous mathematician von Neumann and was born in the “Manhattan Project” of the United States in the 1940s. The principle is to use a large number of random samples to understand a system and then obtain the value to be calculated.

Given that the ratio of the area of ​​a square inscribed circle to the square is π/4, n points are randomly filled inside the square (these points are uniformly distributed). If their distance from the center point is not greater than the radius of the circle, then these points The points all fall inside the circle. Count the number of points in the circle, and multiply the ratio with n by 4 to get the value of π. Theoretically, the larger n is, the more accurate the calculated π value is.

Its code is:

from random import *
seed(100) #Set the seed to fix the random number

dot=0
dots=eval(input('Please enter the number of points you want to fill:')) 

for i in range(1,dots+1):
    x,y=random(),random()
    r=pow(x**2+y**2,0.5)
    if r<=1:
        dot+=1
pi=4*(dot/dots)
print('The resulting pi ratio is: {}'.format(pi))

When there are 10,000 filling points in the circle, the obtained π value is 3.1396, 3.143964 when 1,000,000, and 3.1418574 when 100,000,000. Theoretically, the more filling points there are, the more accurate the calculated π value will be, and with it, the time will be extended… If you are interested, you can also call time.perfcounter() The function looks at the time required for different filling points.

Related Posts

Detailed explanation of finding the difference between two dataframes using Pandas

Python3 crawler tutorial-basic use of aiohttp

Python Digital Analog Notes-PuLP Library (1) Introduction to Linear Programming

Anaconda is installed on the D drive, but why is the working terminal itself on the C drive?

ModelArts(1)——From quick start to remote development

Mouse operation in selenium: ActionChains class

Python installation tutorial step 2: Create a virtual environment in Windows Install Pytorch and configure the virtual environment in PyCharm

Detailed explanation of Python list list (super detailed)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*