10

I have to calculate value of Pi using Gregory-Leibniz series:

pi = 4 * ((1/1 - 1/3) + (1/5 - 1/7) + (1/9 - 1/11) + ...)

I want to write a function in JavaScript that would take the number of digits that needs to be displayed as an argument. But I'm not sure if my way of thinking is fine here.

This is what I got so far:

function pi(n) {
  var pi = 0;
  for (i=1; i <= n; i+2) {
    pi = 4 * ((1/i) + (1/(i+2)))
  }
  return pi;
}

How do I write the pi calculation so it calculates values till n?

Omiod
  • 11,285
  • 11
  • 53
  • 59
Joanna
  • 289
  • 1
  • 6
  • 16
  • 5
    The number of digits does not correspond to the number of series expansions. – Bergi Sep 19 '16 at 13:59
  • 4
    Also notice that JavaScript uses [floating point numbers](http://floating-point-gui.de/), which do not support arbitrary precision, so you can only compute a very limited number of digits (unless you use advanced tricks) – Bergi Sep 19 '16 at 14:01

2 Answers2

7

You could use an increment of 4 and multiply at the end of the function with 4.

n is not the number of digits, but the counter of the value of the series.

function pi(n) {
    var v = 0;
    for (i = 1; i <= n; i += 4) {  // increment by 4
        v +=  1 / i - 1 / (i + 2); // add the value of the series
    }
    return 4 * v;                  // apply the factor at last
}

console.log(pi(1000000000));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You may also do as follows; The function will iterate 10M times and will return you PI with n significant digits after the decimal point.

function getPI(n){
  var i = 1,
      p = 0;
  while (i < 50000000){
   p += 1/i - 1/(i+2);
   i += 4;
  }
  return +(4*p).toFixed(n);
}

var myPI = getPI(10);
console.log("myPI @n:100M:", myPI);
console.log("Math.PI     :", Math.PI);
console.log("The Diff    :", Math.PI-myPI);
Redu
  • 25,060
  • 6
  • 56
  • 76
  • You're casually using 10M (or 50M? or 100M?) as a "surely this is more than you'll ever need" number, but the number of steps needed in the algorithm seems to grow exponentially with the desired precision, so that your posted algorithm is wrong if you increase the precision to even 8 digits. The fact that you revised the number at least 3 times should have been a hint that this is not a good approach. – JounceCracklePop Sep 19 '16 at 23:01
  • @Carl Leth Wow when you mentioned i noticed that by coincidence n = 7 and 10^7 iterations. They are not correlated. I had just picked them randomly. `n` is the number of digits the user wants after the decimal point and hasn't got anything with the precision whatsoever. Having said that you are right, the series is interesting but terribly painful to get close to PI. Anyways, who would calculate PI :) – Redu Sep 19 '16 at 23:30
  • And that's why I downvoted you: you randomly picked a large number and just assumed it would be good enough without actually checking how the required iterations grew as the desired precision increased. In your algorithm, `n` is not the number of digits after the decimal in Pi, it's the number of digits in a particular constant that has the same first 7 digits as Pi. – JounceCracklePop Sep 19 '16 at 23:47
  • @Carl Leth You are welcome to down vote no problem. If you refer to the `myPI @n:100M` line at the results that is a residue from the early version of the code in which i was doing 100M iterations which was provided by `n`. Then after reading the question better i fixed the iterations to 10M and changed `n` to reflect the required digits after decimal. I unintentionally put 7 and it turned out to be as you say the number of digits same with the constant. Honestly i just implemented the given series with fixed 10M iterations regardless of with what `n` value you call it. I will change it to 10. – Redu Sep 20 '16 at 00:04