# Calculating decimal digits of pi, using something similar to a Bailey–Borwein–Plouffe formula

I have tried to Use the Bailey–Borwein–Plouffe formula to calculate pi to 3 digits as a test trial, and recieved the digit 4, which is technically correct, as 4 is the 3rd digit of pi in base 16. I would like to get the digit in decimal, as that is the current standard for numbering things in most places, as you don's see many things labelled D5 except in excel spreadsheets. Basically, I need to conduct a Bailey–Borwein–Plouffe formula from 0-6 (intead of 0 - infinity as i only need pi to 6 digits), but in decimal.

Here is what I had for the base 16 computation.

double pi2 = 0;
int n = 0;
while (n < 3)
{
double a = (1/(Math.pow(16, n)));
double b = (4/((8*n) + 1));
double c = (2/((8*n) + 4));
double d = (1/((8*n) + 5));
double e = (1/((8*n) + 6));
pi2 += a*( b - c - d - e );
n++;
}

• This site is about computer science, not about programming. We can help you with the algorithm, as Yuval did. The coding details would be off-topic here. But your question is fundamentally an algorithm problem, so it's ok here. If you need further help with the Java implementation, you can ask on Stack Overflow. On a side note, be nice. – Gilles 'SO- stop being evil' Sep 29 '14 at 7:53

The Bailey–Borwein–Plouffe formula only works in hexadecimal. There might be other formulas for other bases, but I'm not aware of a decimal-based formula. If you want to obtain the $N$th decimal digit, you have to compute enough hexadecimal digits, there are no shortcuts.

• I dont quite get what you mean by "there are no shortcuts." I am not looking for a shortcut, and unfortunately, teachers do not accept answers in hexadecimal. I am asking not really for a decimal based formula, but for someone to evaluate the code so far, and how to get it to work, as well as possiblly instructing me on how to us this formula to (with another algorithm) to calculate decimal digits of pi. Thank you for responding, but unfortunately, you did not help. – Anyone Sep 29 '14 at 0:21
• So this is a programming question. You need an algorithm for changing base. You can find such algorithms online. On this site we won't help you any further on your actual programming. – Yuval Filmus Sep 29 '14 at 0:24
• but this section of the site is for computer science is it not? and besides, it is technically already converting to base 16, as my integer a is multiplying things by (1/(16^n)), and thus converting it to base 16. really now, I need to add all the digits I get into a coherent thing, not simply a bunch of independent digits. – Anyone Sep 29 '14 at 1:29
• At this point your question has nothing to do with pi. All you need to do is to convert some real number from hexadecimal to decimal. This is a well-known problem with well-known algorithms, called base conversion. You can probably find pseudocode. On this site we won't help you convert the pseudocode into actual code, since this is beyond the scope of the site. – Yuval Filmus Sep 29 '14 at 1:36
• I was talking about pseudocode for the base conversion algorithm, which is what you will find on Wikipedia. Your implementation is problematic since a double has finite – and small – precision, so you wouldn't be able to get more than a handful of decimal (or hexadecimal) digits correctly. – Yuval Filmus Sep 29 '14 at 2:02

Your problem is you are trying to multiply a double with an integer value, which will result in an integer result being stored in a double. If you cast n to a double then you should get the desired result.

ie:

double b = (4 / ((8 * (double)n) + 1));
double c = (2 / ((8 * (double)n) + 4));
double d = (1 / ((8 * (double)n) + 5));
double e = (1 / ((8 * (double)n) + 6));


With your current code, c,d and e are always 0 because you're losing your double's accuracy, it will always be rounded down to 0. b only ever has a value on the first cycle because 4/1 is a nice whole number.

Hope that helps.

These two links show how you can integrate the base-changing formula into the digit calculation formula, to directly calculate the nth decimal digit of pi. They don't appear to be very simple!

http://lacim.uqam.ca/~plouffe/Simon/articlepi.html

https://bellard.org/pi/

There are other older algorithms that compute the nth decimal digit of pi, but they have to compute all the digits before the nth digit. These are the 'spigot algorithms'

https://www.cut-the-knot.org/Curriculum/Algorithms/SpigotForPi.shtml

http://www.stanleyrabinowitz.com/bibliography/spigot.pdf

https://en.wikipedia.org/wiki/Spigot_algorithm

The Rabinovitz and Wagon algorithm is roughly 20 lines of pseudo code