Approximating Pi

Approximating Pi using Monte Carlo method for Pi Day 2022

Approximating Pi

As by tradition I programmed a wacky way of calculating Pi on Pi-day (March 14th).

Method

Imagine a circle inside a square. The circle has a radius of 𝑟 and the square surrounding it has side length's of 2𝑟.
We know that the ratio of the shape's area's is π/4. However we do not know Pi, so to approximate Pi we uniformly scatter points on our square, then from knowing how many of those points have landed inside our circle we can calculate an approximate value of Pi.


In this example we are only using one quarter of our circle and square, this just makes things run a bit smoother and since the ratio between the areas stays the same, the math's don't change.

The Math's

If we take a look at the area's of our shape's we have 𝑟2 for our square and π𝑟2/4 for our quarter circle.
From this we know that the the ratio between these two area's is π/4.


By now by uniformly scattering points inside our square and keeping track of how many of those points are inside the quarter circle we can approximate Pi.
Calculating if a point is inside our quadrant is easy. We just need to calculate its distance to the origin like this d = x2 + y2, if the distance is smaller or equal to 𝑟 then the point is inside.

Knowing the ratio between our two area's (π/4) we can conclude that π ≈ 4 * ( pointsInsideQuadrant / pointsTotal )

Timeline

started

March 14, 2022

published

March 14, 2022

last changed

March 15, 2022

Repository

mymakerofficial/pi-day-2022