You’re being irrational… No, wait… That’s π and e…

So, in my last post I said I was going to tackle pushing the limits of JavaScript and create non-native functions that can derive the values of two irrational numbers: π and e. I also said I was going to use similar methods to derive these values. I plan to follow through on this promise…

So let’s take a look at what I reckon would be the harder of the two: the derivation of π.

π – the number… not the tasty baked good.

So, when it came to doing this, I did a little research into the various formulae that have been used to calculate π. I came across this little tidbit from Madhava of Sangamagrama:

This is an infinite series that rapidly converges to the value of π, and Madhava was from the 14th century… And using the square root of 12… Who would have thought of this back then..?

If you recall from two posts ago I had created a square root function, so this part is easy… I’ve also dealt with cumulative sums before in my previous post with the derivations of the non-native sine and cosine functions, and I’ve also created an integer power function as well. So technically, we have all the ingredients to make some delicious “π” (pun intended)…

So let’s take a look at building this up. Let’s grab the two main functions we need to make this happen, the square root function and the power functions:

p=(x,y)=>y–?x*p(x,y):1;

r=(x,y=k=x)=>(k/=2)?r(x,y/2+x/2/y):y;

These two functions were discussed in previous posts. The integer power function p(x,y) raises a positive integer, x, to the power of another positive integer, y. Whereas the square root function r(x), takes a number, x, and runs Newton’s Method of Approximation for square roots until zero underflow is reached in order to provide the square root.

Using these two functions we’ll put together a pi() function that uses these two and one other function (the cumulative sum function) that we’ll create right now:

P=k=>~k?P(~-k)+1/p(-3,k)/(k-~k):0;

Those who have followed my blog well enough will have recognized that this is a recursive function. This loops through from k down to zero and with each iteration added the equivalent of (-3-k)/(2k+1). Because the integer power function works only with positive powers of y, a simple reciprocal equates to the negative power of y. As this P() function converges quite quickly, you only need 100 iterations before JavaScript’s zero underflow kicks in, P(99), which will give us 100 iterations (0-99) should give us that without breaking a sweat. After that it’s simply a case of multiplying the result with the square root of 12:

pi=()=>r(12)*P(99)

The results will surprise you:

pi() gives 3.141592653589794

Math.PI gives 3.141592653589793

Which is more accurate? According to WolframAlpha, the sequence after the 97 goes 93238… And while JavaScript’s native value wins this round, you have to admit, that’s pretty reasonable accuracy just using recursive functions and standard operators… Now let’s turn out focus to the other guy…

e – the number… I’ve got nothing to compare this against, so let’s just get on with it…

Okay, this is far simpler… e is defined as the cumulative sum of the reciprocals of all natural integer factorials from zero and above… In other words:

It’s interesting to note that as 0! and 1! are both equal to 1, the first two terms of this series make up about 73.5% of the total value of e… Just thought I’d randomly share that bit of trivia there…

Anyhow… If we create a similar cumulative function called e() which adds the reciprocal of reach natural integer factorial, borrowing our factorial function from my post a few weeks ago:

e=k=>~k?e(~-k)+1/f(k):0;

…we then have our function… 100 iterations would once again be sufficient to get to our value within acceptable accuracy… Put to the test:

e(99) gives 2.7182818284590455

Math.E() gives 2.718281828459045

WolframAlpha shows e as continuing after the 2845 with 904523, so apart from that last digit in the return value from our function, it’s practically identical and with a surprising amount of accuracy!

So there we have it… We’ve succeeded in deriving two irrational numbers using a few fundamental functions through basic operators, recursion and fat arrow functions.

My next post will focus on something we touched a little earlier on – JavaScript obfuscation; in this case a challenge set by a fellow code golfer to test me… Goodness knows why… But we’ll take a look in my next post. Until then, stay quirky!