# Calculate $\pi$ in an arbitrary base, to arbitrary precision

I need to calculate $\pi$ -- in base: 4, 12, 32, and 128 -- to an arbitrary number of digits. (It's for an artist friend).

I remember Taylor series and I've found miscellaneous "BBP" formulas, but so far, nothing that points to calculating the digits in arbitrary bases.

How can this be done in a practical manner?

-
You can go to wolframalpha.com/input/?i=N%28Pi%2C1000%29+in+base+128 (for base 128) and keep clicking on More Digits. Or if you have a friend with Mathematica it will give you as many as you want quickly. –  Ross Millikan Apr 3 '11 at 18:41
@Ross Millikan, Well I suppose buying Mathematica is more time/cost-effective, thanks. I was hoping more for an algorithm and thought surely that someone would have it handy (Google-search to the contrary). But that page was great for jump-starting the POC of the application that is behind this question. –  Brock Adams Apr 4 '11 at 1:19
I just checked, and Mathematica (at least my old version 3) will only do bases up to 36. So for 128 you would have to do binary and then combine blocks of 7 bits. brouty.fr/Maths/pi2.html has a million binary bits available for download. Depending upon how large your arbitrary number is, it will solve 4, 32, and 128 –  Ross Millikan Apr 4 '11 at 1:31
@Ross Millikan, Thanks again. –  Brock Adams Apr 4 '11 at 1:35
@Brock. How many digits of each number are you hoping to find? –  JavaMan Apr 30 '11 at 3:20

There is a celebrated formula (perhaps BBP?) that allows you to calculate an arbitrary hexadecimal digit of the expansion of $\pi$. That takes care of bases $4,32,128$.

Now any other formula that is used to calculate $\pi$ in decimal, actually calculates $\pi$ in binary, the result being converted to decimal using the simple base-conversion algorithm. So you can use any old formula, say the arc-tangent one.

Finally, there's probably somewhere on the web an expansion of $\pi$ to zillions of binary digits. Moreover, someone probably wrote a program that converts from binary to an arbitrary base. So all you need to do is find these and plug them together.

-

There are many algorithms that can calculate pi, or alternatively there are many sufficiently accurate approximates of pi (to what might as well be arbitrarily many decimal places). I think the easiest way would just be to take the known value and covert it with a method of base conversion, rather than calculate it independently in each.

-
That's essentially what we ended up doing, for the time being, thanks. But it doesn't answer the question or the project's projected need. –  Brock Adams Apr 30 '11 at 4:16
@Brock: So you want to directly calculate pi in different bases? I'm trying to understand what the exact problem with this method is to the project. –  mixedmath Apr 30 '11 at 4:44
yes. The main problem is efficiency. (1) having to store potentially billions of digits, (2) for each new base we want to test, then having to convert all umpteen million preceding digits to get to the digits of interest. Such an approach may work, given cheap processors and memory, but it is inelegant and I will save it as a last-ish resort. Finally, the question as stated is an interesting problem, IMO, and I thought for sure it would already have been solved; it doesn't seem that difficult. –  Brock Adams Apr 30 '11 at 19:19

Usually, ye calculate pi, and then convert it into the target base.

   3:16E8 E212,7796 7998,5967 5292,6847 6661,9725 5723         base 120
3.141 592 653,589 793 238,462 643 383,279 502 884,197       base 10


This is an implementation in rexx, that finds pi to so many digits in decimal, base 120, and any named base.

-
Is there supposed to be an actual Rexx program (or link) in this answer? –  Brock Adams May 16 '13 at 8:11
It's my general purpose rexx calculator, woven from its documentation. Pi itself is not just Pi(), but PI(A, B), which gives A*pi^b, with defaults of 1, 1. –  wendy.krieger May 16 '13 at 11:34
@BrockAdams Bases 4, 32, and 128 can be derived from a hexadecimal source, since all of these are groupings of binary digits. Base 12 is different. REXX calculates in decimal, the conversion done above is to 30 places, but is a conversion of the decimal, increased by some smallish fraction to prevent .333333 turning up as .39E9 E9E9 E9E9, rather than .4000 0000 0000. –  wendy.krieger May 17 '13 at 7:02