7

I've been searching for hours trying to find an algorithm to get the nth digit of pi for JavaScript.

I know I can use 2*Math.acos(0) or Math.PI to get PI but I need to find the nth digit.

How would one get the nth digit of PI without a hard-coded number already in it?

Spedwards
  • 4,167
  • 16
  • 49
  • 106

1 Answers1

6

Here is a rather simple way assuming some first year calculus.

You can approximate functions by derivating them over and over and understanding their slope - and then building a polynomial around them such that the polynomial approximates their behavior well enough. If you keep doing this for as long as you can you get something called their taylor sequence. If a function is "well behaved" enough - such as the sine function, you can approximate it rather easily.

Here is the expansion of the sine function, taken from Wikipedia (CC wikipedia)

enter image description here

You can come up with this by derivating sin(x) n times and approximating it. Read more on the subject here.

One helpful analysis it and come up with the inverse tangent function Math.atan:

enter image description here

This is useful since putting x = 1 we know Math.atan(1) = Pi/4.

So, let's write our getPi:

function getPi(){
    var sum = 0;
    for(var n = 0; n < 100000000; n++){
        var mult = (n%2 === 0) ? 1 : -1; // -1^n
        sum += mult * (1 / (2*n+1));
    }
    return sum * 4; // to get pi
}
getPi(); // 3.141592643589326

The more iterations you perform, the better the accuracy you'll get. There are faster ways to calculate Pi, this is just an example that requires some - but not a huge amount of math. As mentioned - it works by approximating the atan function with a polynomial.

Note : we have bigger issues with larger numbers since JavaScript double precision numbers are bounded. We ignore that in this answer.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I realize there is a lot of hand waving here - if you have any specific questions about how approximating a function with a polynomial works or about this method - feel free to ask and I'll do my best to answer. Since you did not say what your Math level is - I assumed some stuff. Let me know. – Benjamin Gruenbaum Jun 28 '14 at 15:14
  • If you're up to it - you _will_ have better luck with [this](http://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula). It's a lot more erm, practical. – Benjamin Gruenbaum Jun 28 '14 at 15:22
  • 8
    The question was about how to get the _nth digit_ **not** _PI itself_. :) – try-catch-finally Jun 28 '14 at 15:25
  • 1
    @try-catch-finally I believe getting the `n`th digit of a number when you have that number is a rather simple task :) I did link in a comment to a better algorithm that calculates the `n`th digit of PI _without_ calculating the previous digits first but I'm afraid it requires more Math (as in - it's usually taught 2 courses later). If you're genuinely interested I can write a post about it, but the formula is a _lot_ harder to justify from the basics - here is a short proof I found http://crd-legacy.lbl.gov/~dhbailey/dhbpapers/pi-quest.pdf . – Benjamin Gruenbaum Jun 28 '14 at 15:30