title: Calculating the nth digit of pi using the Bailey–Borwein–Plouffe (BBP) formula
body: <p>How do I calculate the <em>n</em>th binary (or hexadecimal) digit of pi using the Bailey–Borwein–Plouffe formula? I have been thoroughly searching the Internet and this site for an answer, but I've still yet to find an actual implementation for the algorithm.</p>
  
  <p>The <a href="http://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula">Bailey–Borwein–Plouffe</a> page on Wikipedia tells me that "The method calculates the <em>n</em>th digit without calculating the first <code>n − 1</code> digits, and can use small, efficient data types". The problem is, the page only explains the formula in this form:</p>
  
  <pre><code>Pi = SUM k=0 to infinity ...
  </code></pre>
  
  <p>But I have no clue how to use this information to actually find the <em>n</em>th digit of pi. (You know, I don't want to find the value of Pi itself, nor do I want to count a sum to infinity...) I just need to see an example in any programming language or pseudo-code on how to do this in practice.</p>
  
  <pre><code>int nthDigitOfPi(long n) {
      // calculate and return the n-th binary digit of pi
      // ...
  }
  </code></pre>
  
  <p>Thanks in advance. Your help will be greatly appreciated.</p>