# Thread: looking for quick method to calculate pi

1. ## looking for quick method to calculate pi

I'm looking for a FAST way to calculate X digits of pi in hexadecimal without using any external libraries (i.e. APFloat, etc.).

Currently I've found code for extracting any specific digit of pi in hexadecimal (using the BBP formula, I believe), but using this code to compute even 1000 digits of pi is SLOW.

2. I wonder why.

3. You're going to have to keep wondering, because it's not homework. (I assume that's what you're thinking, anyway.)

4. > using this code to compute even 1000 digits of pi is SLOW.
Well pi is an extremely long real number that has no documented repetitions for about 50 billion places, if I remember correctly. Few of us are mathematicians; I would be happy with what is already possible through the work of someone else.

5. As Citizen said.

There was this mathematician that took 15 years to calculate manually the first 1000 digits, if I recall correctly. He pusblished the results and died peacefully, and an acomplished man, in his bed (for his own sake, I hope not alone), only to be found later on he made a mistake somewhere halfway. And all numbers that followed were wrong.

Anyways... You can use the Machin's Formula

6. You could also go to one of the countless sites that show PI at a high precision, copy that into a file, and read the file.

7. would it be faster or easier to calculate the square root of 2 to X hexadecimal digits?

8. A close approximation of pi is the quotient of 22 / 7. You may have an easier time converting that to hexidecimal.

9. Originally Posted by citizen
A close approximation of pi is the quotient of 22 / 7. You may have an easier time converting that to hexidecimal.
No,
pi = 3.141592...
22/7 = 3.142857...

As you can see the approximation is not very close, although in practice this kind of precision would be enough in most cases.

I know there's a method to calculate the n-th hex digit of pi (I think even not knowing about the other heximal places) and you could google for that. Probably, you could correctly calculate pi to any digit in a loop.

It was too advanced mathematics for me to understand...

10. Here's how I calculate pi:
Code:
`double x = 4 * atan( 1.0 );`

11. Originally Posted by Micko
Here's how I calculate pi:
Code:
`double x = 4 * atan( 1.0 );`
A fine technique, as long as you understand and accept the limitations of atan().

12. The problem is that want the *digits* of pi (or the sqrt of 2), not the value.

13. Once you have the value you can get the digits, but yes, I think reading pre-calculated values from file may be easier.

14. Enjoy: pi digits
Seems correct to me.