Life of Pi — A gopher’s tale
History of Pi — The Indian connection
Great minds over the ages, across all civilizations were fascinated with the venerable mathematical constant Pi. Indeed this constant appears in physics and mathematics in many places. Indian mathematicians too have made significant contributions to the calculation of Pi. The mathematical genius Srinivasa Ramanujam came up with many infinite series which sum to Pi. But in this post, let’s take a look at the method discovered by an ancient Indian mathematician Aryabhata in 5th century A.D.
Aryabhata’s rule
Aryabhata gave his rule for calculating Pi as a verse in sanskrit.
caturadhikam śatam
aṣṭaguṇam dvāṣaṣṭistathā sahasrāṇām
ayutadvayaviṣkambhasyāsanno vṛittapariṇāhaḥ.
This remarkable rule translates in English as the below. Add 4 to 100, Multiply by 8 and add 62000. By this rule, the perimeter of a circle with diameter 20,000 can be approached.
Now let’s write some trivial go code to compute Pi using this rule.
func calcPiAB() float64 {
return ((100.0+4.0)*8 + 62000.0) / 20000.0
}
Executing this code in go play ground, we get the value of Pi as 3.1416, accurate to five significant figures. Indeed, the value of Pi to 8 places is 3.14159265 . So, a remarkable achievement by Aryabhata. True Genius. Aryabhata uses the word “approached” (asana in sanskrit) in his rule. This had led to some speculation that Aryabhata knew about irrationality of Pi which was proved only many centuries later.
Calculating Pi — By counting darts
How did Aryabhata arrive at his rule ? We mortals can merely speculate. Let’s move on and try a simpler method — counting darts.This method is explained below.
Imagine a square dart board of unit size (side=1m) with a quarter of an unit circle inscribed in it. Let’s randomly throw many darts at the board. Some darts will land inside the circle and some outside of it as shown in the figure below.(Blue = Inside the circle, Red = outside the circle)
We can visualize this process in go code using the excellent svgo library from Anthony J starks.

The ratio of number of darts landed inside the circle (blue) to the total number of darts thrown (red + blue) is approximately equal to the ratio of area of quarter circle(of unit radius) to the area of (unit) square which is Pi/4 if the darts were thrown randomly.
So the value of Pi can be estimated as 4.0*( number of darts which landed inside the quarter circle/total number of darts thrown)
Here is the code in go playground which calculates the value of Pi using Montecarlo method.
Executing the above code in go play ground simulating 100000 darts randomly thrown, our estimate of Pi comes out as 3.1370 . Not bad for a method of counting randomly thrown darts.
Although the example used here is a silly toy problem, the method used here is anything but silly. It goes by the name of Montecarlo method and is used for some heavy lifting in engineering and science. This method is generally used to solve problems which do not have analytical mathematical solutions. This method has a rich history and had seen it’s first usage during the making of first nuclear weapons in Los Alamos national laboratory for solving neutron diffusion problems. The name montecarlo comes from the usage of randomness and montecarlo is a famous casino in monaco which was frequented by gambling addicted uncle of one of the progenitors of this method Stanislaw Ulam.
The complete code for calculating Pi using montecarlo method is available in this github respository.
