1

How to calculate Euler's Number to nth digit in JavaScript?

e   =   1/0! + 1/1! + 1/2! + ...    
    =   2.7182818284590...to nth digit  (base 10)

Below code returns only number to 16 decimal point and accuracy is depend upon to the number of iterations/or number passed to function.

Code

function calcE(n) {
  let euler, itr;
  euler = 0
  itr = Number(n)
    for (var i = 0; i < itr+1; i++) {
      let fact = factorial(i)
      euler += 1/fact
    }
  return euler;
}

Demo: miniwebtool

Sumit Ridhal
  • 1,359
  • 3
  • 14
  • 30
  • 2
    The closing votes are very strange - the question is not broad at all. It is simple, that is roght, but there is no any simplicity limit set in the rules of SO. And what is simple for one person, is not such for another one. Upvoting for compensation of unfairness. – Gangnus Jul 03 '17 at 13:48
  • @Gangnus, although I can't see the close votes, I agree that too broad is certainly not correct in this case, it seems that too broad is the go to 'I don't know what to flag this as' reason. Its also worth noting that the edits vastly improve the question. +1 – Nick is tired Jul 03 '17 at 14:00
  • *"...returns only number to 16 decimal point..."* : JavaScript uses IEEE 754 floating point representations, which limits the precision to about 16 decimal digits. So you'll not be able to get more out of it. See https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – trincot Jul 04 '17 at 07:45

2 Answers2

0

You should find the first factorial that is greater than 2*10^n. If it is m!, you should count the e number up to 1/m!.

For the estimation of the m! you can use Stirling's approximation. Don't be afraid of e presence there - you can count it all very roughly, really you need only the order. That way allows you to determine the number of addings beforehand. It can be counted by different algorithm and faster.

If you needn't have it beforehand, you can calculate each 1/m! additions and simply compare it to 0.5/10^n. The first addition that is lesser than 1/10^n is the last that you should use.

    limit = 0.5/10^n
    add = 1
    i = 1
    euler = 1
    while(add>limit) {
      add /= i
      euler += add
      i++
    }
    return euler
Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • 3
    This is subject to a subtle trap. The algorithm you describe computes `e` with the 10^{-n} _precision_, which doesn't guarantee the the `n`'th _digit_ is correct. One must estimate the remaining error (via Taylor theorem e.g.), and keep adding until it becomes small enough to stabilize `n`'th digit. – user58697 Jul 03 '17 at 21:39
  • @user58697 Yes, I have forgotten to multiply by 0.5. Edited. But the existing practice, when talking about n'th digit, means 0.5 precision. Nobody wants to try if the next addition will change the digits that stands in elder positions. Yes, that can mean that sometimes you will have the tail of 100000 instead of 099999 and vice versa, but that does not matter. The target of that practice is the sure future use of 0.5*lastDigit as the upper limit of the possible error, and it works. – Gangnus Jul 04 '17 at 07:42
  • @user58697 If you are interested in more precise error evaluation that 0.5 of last digit, calculate mistakes evaluations together with the main value. – Gangnus Jul 04 '17 at 07:42
0

Found a solution by using npm decimal.js package to handle big decimal.

Euler

function euler(n) {
  let zero = new Decimal(0);
  let one = new Decimal(1);
  let rval;

  for (let i = 0; i <= n; i++) {
    let fval = factorial(i);
    let invert = one.dividedBy(fval)
    zero = zero.plus(invert)
  }
  rval = zero.toFixed(Number(n))
  return rval;
}

function factorial(n) {
  var i = 2,
    r = new Decimal(1);
  for (; i <= n; r = r.times(i++));
  return r;
}

Still not able to compute very large values. An Suggestions?

Sumit Ridhal
  • 1,359
  • 3
  • 14
  • 30
  • You'll have to use the decimal library also for `factorial(i);`, since also that number quickly runs out of the necessary precision. NB: You should keep an incremental value of the factorial to avoid repetitive re-multiplication of something you already did in the previous iteration. – trincot Jul 04 '17 at 13:10