There is no need to introduce Pi! Pi is one of the most interesting mathematical constant. Originally, Pi has been defined as the ratio of a circle’s circumference to its diameter. However, Pi is more than that: it unexpectedly shows up in almost all areas of mathematics and physics.
So, why not use it to explore a bit more of computer programming?
One thing that you must know about Pi: Pi is irrational, meaning that you cannot write it as a finite (nor cyclic) suite of digits. Actually Pi is even transcendental, but it has nothing to do with this post! So, Pi is an infinite and fascinating suite of digits that starts like this 3.1415926535897932384626433…
Super computers have already computed thousands, millions, billions of Pi digits, but this is nothing compare to the Pi digits suite length.
So here is our challenge:
Let’s see what we can do!
A Naive algorithm to compute Pi
Let’s build a very simple HTML & CSS User Interface:
As you can see, Pi is hard coded as 3.14 for now through computePi() function. We are going to iterate over this function in the next paragraphs.
First improvement: compute Pi for real using math functions. According to this web page, there is a simple expression of Pi based on atan() function:
π = 4·arctan(1).
Click on ‘Compute me’. Here is the output:
That’s a good start! We now have a first approximation of Pi with 17 correct digits!
Run it again:
Coding our own atan() Function
In our case, we have the following expression:
arctan(x) = x − x3/3 + x5/5 − x7/7 + x9/9 − x11/11 + . . .
You should get same output. However, for performances reasons, we are going to use a slightly different expression of Pi:
π = 4·(4·arctan(1/5) – atan(1/239))
This formula converges way faster using our custom atan function. Just have a look our Taylor Series using x = 1 and x = 1/5 and look at term sizes.
Our latest Pi algorithm now looks like this:
Still getting correct Pi digits:
Using toPrecision() tricks:
Not better. We managed to replace this obscure atan function by something we can control. However, we did not manage to compute more Pi digits from now on. We need to go one step further!
We need a way to keep float precisions when adding, multiplying, dividing floats together. In order to do that, we are going to use the great big.js Library. This library provides an API to run operations between numbers with an arbitrary precision.
const bigOne = new Big(1) let preciseX = bigOne.div(5) let x = 1/5
We can now come with a new version of our atan() function:
Latest algorithm sources are available in this JSFiddle if you don’t want to update previous one!
Note: he arithmetic methods always return the exact result
Press ‘Compute me’ button… and let magic happens:
Comparing to Pi’s first digits:
π = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159…
Great! We have achieved our goal: code an algorithm to compute Pi’s 300 first digits!
You can go as far as you want, depending on how fast is your CPU. Take time to try different values of precisions. You can also try to implement different Pi expression in your computePi() function.