A while ago I wrote a post on estimating Pi using a variety of methods. As a sort of follow-up I will now write a post on estimating *e*, or Euler's Number, in JavaScript.

I won't try to summarize what *e* is or what it is used for as Wikipedia has a comprehensive article on the subject. However, I will include a portrait of Leonhard Euler just after he had washed his hair and changed into his pyjamas.

*Swiss mathematician Leonhard Euler15 April 1707 to 18 September 1783*

This project consists of a JavaScript file called estimatinge.js as well as an HTML page and a few ancilliary files. You can download the source code from the Downloads page or clone/download the Github repository.

Source Code Links

If you open estimatinge.htm in your browser you'll see this, the results of four different methods of estimating e.

The numbers in white are the correct values to 15dp (the most that JavaScript can handle with accuracy) and the values below them are the estimates with correct digits shown in green and incorrect digits shown in red.

This is the first part of the source code.

estimatinge.js (part 1)

window.onload = function()

{

firstExpansion();

secondExpansion();

NewtonSeries();

BrothersFormula();

}

function printAsText(e)

{

let e_correct = "2.718281828459045";

const e_string = e.toString().substring(0, 17);

writeToConsole("Definitive: " + e_correct + "<br />", "console");

writeToConsole("Estimated: ", "console");

for(let i = 0, l = e_string.length; i < l; i++)

{

if(e_string[i] == e_correct[i])

{

writeToConsole("<span class='green'>" + e_string[i] + "</span>", "console")

}

else

{

writeToConsole("<span class='red'>" + e_string[i] + "</span>", "console")

}

}

writeToConsole("<br /><br />", "console");

}

function factorial(n)

{

return (n < 2) ? 1 : factorial(n - 1) * n;

}

## window.onload

This function simply calls the four functions implementing various methods of calculating *e*.

## printAsText

Here we take a value of *e* as an argument and print it below the correct value, showing correct/incorrect digits in green/red respectively as shown in the screenshot.

## factorial

It is shocking that there is no Math.factorial function so I had to write my own.

Now let's look at the four *e*-estimating functions.

estimatinge.js (part 2)

function firstExpansion()

{

writeToConsole("First expansion: n = 100000<br />---------------------------<br />", "console");

const n = 100000;

let e = (1 + 1/n)**n;

printAsText(e);

}

function secondExpansion()

{

writeToConsole("Second expansion: n = 0.000000001<br />---------------------------------<br />", "console");

const n = 0.000000001;

let e = (1 + n)**(1/n);

printAsText(e);

}

function NewtonSeries()

{

writeToConsole("Newton's Series: n = 0 to 12<br />---------------------------<br />", "console");

let e = 0;

for(let n = 0; n <= 12; n++)

{

e += (1 / factorial(n));

}

printAsText(e);

}

function BrothersFormula()

{

writeToConsole("Brothers Formula: n = 0 to 8<br />---------------------------<br />", "console");

let e = 0;

for(let n = 0; n <= 8; n++)

{

e += (2*n + 2) / (factorial(2 * n + 1));

}

printAsText(e);

}

## First Expansion

The formula implemented here is...

e = (1 + 1/n)^{n}

...with which I have used a value of n = 100000. This gives us 4dp of accuracy; you might like to experiment with different values for n.

## Second Expansion

The next formula is...

e = (1 + n)^{1/n}

## Newton's Series

Now let's move on to the third method, Newton's Series, for which the formula is:

This means we start off with n = 0, set *e* to 1/n!, increment n and then keep adding 1/n! to *e* for as long as we want.

(*n! means the product of all integers up to and including n, for example 4! = 4 x 3 x 2 x 1 = 24. Incidentally 0! = 1*.)

Obviously we cannot keep going on to ∞ but the 12 incrementations I have used give us 9dp of accuracy.

## Brothers Formula

As with π people are still working on ways to calculate *e* and I'll finish off with this impressive recently discovered method, Brothers Formula, devised as recently as 2004.

As with Newton's Series this is the sum of a infinite series although rather more complex.

The extra complexity pays off though: we only need to count to 8 to get 15dp (possibly more) of accuracy.

Follow @codedrome