PI Estimation
In this article, we’ll see how to approximate to the pi with basic math and computing power.
Before to start i would like to suggest you to read the theory of the behind the magic but incase i’ll mention it anyway. (https://en.wikipedia.org/wiki/Monte_Carlo_method)
The basic idea is;
1- Inscribe a circle with radius r in a square with side length of 2r
2- The area of the circle is Πr2 and the area of the square is 4r2
so, if we randomly generate N points inside the square approximately
N * Π / 4 of those points (M) should fall inside the circle.
Here’s a gif of this method in action.
Here’s a basic java code to evaluate this.
As you can see, if we increase the radius(thus number of dots) we get closer to the pi
And theoretically if we limit to infinity, we reach the pi.
Thanks for reading ❤