1. ## pi spigot program

I am attempting to use the Bailey-Borwein-Plouffe algorithm, ((4/(8n+1))-(2/(8n+4))-(1/(8n+5))-(1/(8n+6)))/16^n for the nth digit of pi.

Here is my code:
Code:
```#include <stdio.h>
int main(void){
float n, pi_digit, hex_base, pi;
for(n=0, hex_base=1; n<=20; n++) {
pi_digit = 4 /(8 * n + 1);
pi_digit -= 2 / (8 * n + 4);
pi_digit -= 1 / (8 * n + 5);
pi_digit -= 1 / (8 * n + 6);
pi_digit /= hex_base;
hex_base *= 16;
printf("%x ",pi_digit);
}
getchar ();
}```
I'm getting the wrong answer and I don't know if I'm handling the hexadecimal conversion correctly.

2. First of all, use doubles instead of floats for greater precision.

Completely wrong?

3. Why are you printing the digit as a hex value? I've never seen PI represented in hex...

Since pi_digit is a float you have to use %f or one of the other floating point conversion specifiers. %x is used for printing integers.

4. Google "Bailey-Borwein-Plouffe algorithm hexadecimal" gave me this, which gives the reason:
The kicker here though is that a single digit of the transcendent number is extracted, and as the (1/16)^n term indicates, that digit is in hexadecimal. Other algorithms must be used to convert the partial series of hexadecimal digits into decimal, or base 10.

Edit for edit: Since the digit created is hexadecimal, you don't want %f. (I don't think hexadecimal numbers can be floating-point, can they?)
[/edit]

5. 
Edit for edit: Since the digit created is hexadecimal, you don't want %f. (I don't think hexadecimal numbers can be floating-point, can they?)
[/edit]
Floating point digets are internally base two, so printing them out as base 16 should be even simpler then printing them out as base 10.

That said, There are no standard function that prints hexadecimal floating point numbers that I know of. You would have to either find a librairy, or write your own (which is not too hard).

6. This code from the Internet works, but I did not write it:

Code:
```#include <stdio.h>
int a[52514], b, c=52514, d, e, f=1e4, g, h;
main(){
for(;b=c-=14;h=printf("%04d",e+d/f))
for(e=d%=f;g=--b*2;d/=g)
d=d*b+f*(h?a[b]:f/5),a[b]=d%--g;
getchar();
}```

7. Yes, completely wrong--I don't even get the initial 3. Yes, pi can be written out in hexadecimal digits. I expected everyone here to be familiar with BPP's formula for the nth digit of pi, as it is the only one I know of that allows for individual digits to be calculated.

8. Originally Posted by eehiram
I expected everyone here to be familiar with BPP's formula for the nth digit of pi, as it is the only one I know of that allows for individual digits to be calculated.
Perhaps, but this is a C forum, not a math forum

9. looking for quick method to calculate pi

Hmmm. Looks strangely familiar eh? Either you are cross-posting and trying to be uber elite by changing your name, or you guys are in the same class.