# Life of Pi (π): Calculating its Value

### Condensing thousands of years of mathematical progress into a few lines of JavaScript!

Hello there!

Recently, there were many headlines celebrating how we’ve managed to calculate pi to the 100 trillionth digit:

No, your eyes aren’t playing tricks on you. One…hundred...trillion…digits. That’s a lot of digits, if I do say so myself! All of this got me asking a more fundamental question. * How would we calculate pi in the first place?* Answering this took me down a fun rabbit hole, but I summarized the educational and informational bits into the following video where I describe how we can use basic JavaScript techniques to calculate pi from scratch:

If you prefer reading, **A Fun Look at How to Calculate the Value of Pi** is the article version of the above video that covers similar content.

## From Math to JavaScript

With that said, let’s get to what I want to talk with you about. Depending on how deep you went into mathematics in your career or schooling, you may have seen equations that look a bit like this:

This particular equation describes one of the earlier attempts at calculating the value of pi by adding up an infinite number of terms. When we look at a calculation in this way, it probably looks a bit scary. As my video/article highlights and the code reflects, this equation can be easily reduced and un-scarified into nothing more than a **for** loop paired with some simple mathematical expressions and JavaScript syntax blocks:

The sigma (Σ) character in our equation is our **for** loop, and the terms following the sigma character make up the body of our **for** loop.

When this code runs, what we eventually get is a value that is almost identical to what we would get if we used `Math.PI`

directly. The only difference is that we did a lot of heavy lifting to calculate pi in the first place, and this heavy lifting primarily involved putting loops, console log statements, functions, and mathematical operators in their right places. Nothing too heavy at all! 🧠

If anything, the complicated part was mentally translating from the *mathematical representation from the equation* into a *mathematical representation that works in JavaScript*.

## How about you?

Having our computers solve complicated math problems is nothing new. Number crunching was one of the earliest jobs our computers and their predecessors did. Digging into something like my exercise in calculating pi, I realized that they are still very good at crunching numbers - even if the direct work they do there is abstracted away by many layers of UX goodness where writing formulas is replaced with point/click/tap gestures.

**Let me turn this around now.** When was the last time you had to do something with your computer that falls into the realm of “math-heavy computation”? Reply to my tweet or create a thread on the forums…like the days of yore!

Of course, I always enjoy hearing from readers like you! Please feel free to ping me, and I’ll get back to you ASAP.

Cheers,

Kirupa 🙃