By Robert Smith
This article introduces a new algorithm for extracting hexadecimal digits of \(\pi\) without computing the preceding digits. It strikes the standard term-by-term evaluations of Bailey–Borwein–Plouffe (BBP), Bellard, and Huvent on operation count. Even against Huvent’s eight-term formula, the funds at ample offsets are about 61% small divisions and 38% fewer full standardized exponentiations.
The key is a association among the four powers of two in this identity:
\[ \boxed{ \pi = \sum_{k=0}^{\infty} \frac{(-1)^{\lfloor k/2\rfloor}\big(2^{1-3k}+2^{-1-5k}\big) + (-1)^k\big(2^{-1-6k}+2^{-4-10k}\big)}{2k+1}. } \]
The average denominator \(M=2k+1\) method we can add four remainders and divide once. The astonishment is that we don’t have to compute those remainders independently either. As we volition see, the gaps between them remain small multiples of \(k\) plus constants, so one starting power and the stage \(2^k\bmod M\) let us reach the others by multiplication.
Even better, operating modulo \(3M\) lets the same two exponentiations serve both \(M\) and \(3M\). Eight powers of two complete two distinct denominators can portion two complete exponentiations and a sole division. For all million hexadecimal places skipped, that saves concerning a myriad complete exponentiations compared alongside Huvent, before any machine-specific optimization enters into it.
The BBP trick and equation convergence
To see anywhere the funds arrive from, recall the BBP trick. To skip \(n\) hex digits, change \(\pi\) remaining by \(d=4n\) bits and take the partial part. For a term \(2^E/N\) alongside \(E\ge 0\), we only need \((2^E\bmod N)/N\) since the difference is an entire figure which doesn’t contribute to the partial part. So equal at \(d=4{,}000{,}000\) we never have to build a four-million-bit numerator. The conditions alongside negative exponents motionless count; they decay geometrically, so we can sum their tails directly, keeping adequate defender bits to resolve the last digit we want.
Last time, I counted the activity by counting fractions. Bellard’s formula advances 10 bits for all seven fractions; Huvent’s equation advances 12 bits for all eight. After finding Huvent’s equation independently, I’ve feverishly tried to discover one alongside a improved ratio, without success. So I looked toward ways of optimizing existing series.
One clue tipped me off. There’s a choice hiding in the final stage of Bellard’s derivation. He has one sum in powers of \(1/4\) and another in powers of \(1/1024\). Since \(4^5=1024\), splitting the archetypal sum into five progressions lets him write both sums in powers of \(1/1024\). What if, alternatively of lining up the powers, we lined up the denominators?
Lining up denominators
Lining up denominators seemed difficult alongside the many-term sequence of Bellard and Huvent. One fine location I established to try this is a sequence Euler wrote downward in 1779. Craig-Wood used it to extract hexadecimal digits1. In the original notation there are six rows:
\[ \begin{aligned} \pi ={}& 2\left(1 - \frac{1}{5}\cdot\frac{1}{64} + \frac{1}{9}\cdot\frac{1}{64^2} - \frac{1}{13}\cdot\frac{1}{64^3} + \cdots\right) \\ &+ \frac{1}{2}\left(1 - \frac{1}{3}\cdot\frac{1}{64} + \frac{1}{5}\cdot\frac{1}{64^2} - \frac{1}{7}\cdot\frac{1}{64^3} + \cdots\right) \\ &+ \frac{1}{4}\left(\frac{1}{3} - \frac{1}{7}\cdot\frac{1}{64} + \frac{1}{11}\cdot\frac{1}{64^2} - \frac{1}{15}\cdot\frac{1}{64^3} + \cdots\right) \\[4pt] &+ \frac{1}{2}\left(1 - \frac{1}{5}\cdot\frac{1}{1024} + \frac{1}{9}\cdot\frac{1}{1024^2} - \frac{1}{13}\cdot\frac{1}{1024^3} + \cdots\right) \\ &+ \frac{1}{16}\left(1 - \frac{1}{3}\cdot\frac{1}{1024} + \frac{1}{5}\cdot\frac{1}{1024^2} - \frac{1}{7}\cdot\frac{1}{1024^3} + \cdots\right) \\ &+ \frac{1}{64}\left(\frac{1}{3} - \frac{1}{7}\cdot\frac{1}{1024} + \frac{1}{11}\cdot\frac{1}{1024^2} - \frac{1}{15}\cdot\frac{1}{1024^3} + \cdots\right). \end{aligned} \]
Takahashi writes this straightforwardly as two sums:
\[ \begin{aligned} \pi ={}& \frac{1}{4}\sum_{m=0}^{\infty} \left(-\frac{1}{64}\right)^m \left(\frac{8}{4m+1}+\frac{4}{4m+2}+\frac{1}{4m+3}\right) \\[4pt] &+ \frac{1}{64}\sum_{m=0}^{\infty} \left(-\frac{1}{1024}\right)^m \left(\frac{32}{4m+1}+\frac{8}{4m+2}+\frac{1}{4m+3}\right). \end{aligned} \]
Notice what we’re dividing by. Since \(4m+2=2(2m+1)\), we can halve the numerator of the center fraction and depart \(2m+1\) in the denominator. As \(m\) runs complete the nonnegative integers, \(2m+1\) gives all positive odd integer. The another two progressions, \(4m+1\) and \(4m+3\), divided that same catalog between them. So all odd denominator appears twice in all sum.
For example, regard the denominator 13. The archetypal progression hits it at \(m=3\); the center one hits it at \(m=6\), anywhere the first denominator was 26. That’s two contributions from the sum in powers of \(1/64\) and two from the sum in powers of \(1/1024\). The powers are different, but all four numerators can be added before we distinct by 13. We can collect conditions this way for all odd denominator.
To get the boxed equation directly, we can start with a multiplication of Gaussian integers: \((3-i)^2(7-i)=50(1-i)\). Taking complex arguments gives Hutton’s arctangent identity from the 18th century:
\[ 2\arctan\left(\frac13\right)+\arctan\left(\frac17\right)=\frac\pi4. \]
Bellard lists this character as equation (7). His logarithm trick plant on both angles: \(1-(1+i)/4=(3-i)/4\), and \(1-(1+i)/8=(7-i)/8\). So if we put
\[ I_u := \operatorname{Im}\!\left[-\log\!\left(1-\frac{1+i}{2^u}\right)\right] = \operatorname{Im}\sum_{j=1}^{\infty}\frac{(1+i)^j}{j\,2^{uj}}, \]
then \(I_2=\arctan(1/3)\), \(I_3=\arctan(1/7)\), and we desire \(\pi=8I_2+4I_3\). Every period \(j\) goes up by four, \((1+i)^j/2^{uj}\) is multiplied by \(-4/2^{4u}\). That’s \(-1/64\) in the archetypal sum and \(-1/1024\) in the second.
For an odd denominator \(N=2k+1\), the two contributions in all logarithm come from \(j=N\) and \(j=2N\). All additional doublings provision multiples of four, anywhere \((1+i)^j\) is genuine and contributes nothing. At \(j=N\) the imaginary part of \((1+i)^N\) is \((-1)^{\lfloor k/2\rfloor}2^k\); at \(j=2N\) we’re looking at \((2i)^N=(-1)^k 2^N i\). At \(j=N\), the archetypal logarithm multiplies the imaginary part by \(8/2^{2(2k+1)}\), giving the numerator term \((-1)^{\lfloor k/2\rfloor}2^{1-3k}\). Collect the another three contributions in the identical way and we have the boxed formula.
Four powers in two exponentiations
After the change by \(d\), that is, multiplying the sum by \(2^d\), the four exponents, in the command we wrote them, are
\[ (E_1,E_2,E_3,E_4)=(d+1-3k,\ d-1-5k,\ d-1-6k,\ d-4-10k). \]
Call a power active if its exponent is nonnegative. The slowest-decaying exponent is \(E_1\), which stays nonnegative up to \(k=\lfloor(d+1)/3\rfloor\). That’s concerning \(d/3\) odd denominators, and we distinct fair formerly for all one. Even the largest modulus is lone \(2d/3+O(1)\), compared alongside concerning \(d\) for Bellard or Huvent, and \(2d\) for the first BBP formula.
As for the remainders \(r_j=2^{E_j}\bmod N\), notice that the center two exponents differ by \(k\). If we already have \(r_3\) and \(x=2^k\bmod N\), afterward \(r_2\) is just \(xr_3\bmod N\). In fact, we can stage from the smallest exponent up to the largest:
\[ r_4 \;\xrightarrow{\;\times\,8x^4\;}\; r_3 \;\xrightarrow{\;\times\,x\;}\; r_2 \;\xrightarrow{\;\times\,4x^2\;}\; r_1 \pmod N. \]
Start at the smallest energetic power. We pay for one complete exponentiation to get it and one to get \(x\); the remainder are a fistful of multiplications. Past \(k\approx d/5\) lone \(E_1\) is active, so don’t annoy computing \(x\).2 That’s two exponentiations for the archetypal \(d/5\) denominators and one for the remaining \(d/3-d/5\), or concerning \(8d/15\) altogether.
Two denominators for the cost of one
So far we’ve paid for one division by 13 and another by 39. Call the signed sums of their energetic powers \(A\) and \(B\). Since 39 is three times 13, their blended contribution is
\[ \frac{A}{13}+\frac{B}{39}=\frac{3A+B}{39}. \]
We lone desire the partial part, so we can compute the two numerators modulo 39, obtain the remainder of \(3A+B\), and distinct by 39 fair once.
Now regard the stage powers. For 13 the indicator is 6; for 39 it’s 19. Since \(19=3\cdot6+1\), formerly we have \(x=2^6\bmod39\), we get the step for 39 by cubing and doubling: \(2^{19}\equiv2x^3\pmod{39}\).
In general, tripling a denominator \(M=2k+1\) changes its indicator from \(k\) to \(3k+1\). Substitute that indicator into the four exponents complete to get the partner’s exponents. Now peruse the energetic exponents from both numerators in expanding order. The average \(d\) cancels in all gap, leaving a small multiple of \(k\) affirmative a constant. We need fair two full exponentiations modulo \(3M\): one for the archetypal power in the catalog and one for the stage \(2^k\). Multiplication takes us up the remainder of the list.
We never need to compute the stage fair for \(3M\). Its largest exponent, \(d-2-9k\), is smaller than the second exponent for \(M\), \(d-1-5k\). If \(3M\) has equal one energetic power, \(M\) already has at smallest two.
We don’t desire to brace 39 alongside 13 and afterward brace it again alongside 117. So we start pairs lone at odd \(M\) not divisible by three. If \(3M\) has an active power, we obtain it alongside \(M\); alternatively we procedure \(M\) alone. Odd multiples of nine, akin 117, remain unpaired.
The denominators that no longer need their own rotate are exactly the odd multiples of three that aren’t multiples of nine. That’s two out of every nine odd denominators, the two anywhere we need one exponentiation and where we need two. Multiplying the counts complete by \(7/9\) gives concerning \(7d/27\) divisions and \(56d/135\) complete exponentiations, compared to \(2d/3\) of all for Huvent.
Here is how the formulas difference in conditions of how far into \(\pi\) we get per costly arithmetic operation, expressed as “bits per operation”:
| Formula | Bits per complete mod. exp. | Bits per division |
|---|---|---|
| BBP, term by term | 4/4 = 1.00 | 4/4 = 1.00 |
| Euler, term by term | 5/4 = 1.25 | 5/4 = 1.25 |
| Bellard, term by term | 10/7 ≈ 1.43 | 10/7 ≈ 1.43 |
| Huvent, term by term | 12/8 = 1.50 | 12/8 = 1.50 |
| This post | 135/56 ≈ 2.41 | 27/7 ≈ 3.86 |
Notes on a high-performance implementation
I implemented the two the algorithm of this article and Huvent using AVX-512 IFMA. Here’s how lengthy all took to extract 12 hex digits on my computer.
| Hex digits skipped | Threads | Huvent (ms) | This article (ms) | % faster |
|---|---|---|---|---|
| 1,000,000 | 1 | 8.09 | 4.88 | +65.7% |
| 100,000,000 | 1 | 962 | 584 | +64.7% |
| 1,000,000 | 32 | 4.23 | 3.82 | +10.9% |
| 100,000,000 | 32 | 64.4 | 42.4 | +51.8% |
| 1,000,000,000 | 32 | 655 | 410 | +59.9% |
| 1,000,000,000,000 | 32 | 845,685 | 520,907 | +62.3% |
Returning to the brace 13 and 39, during 39 is active, I never decrease modulo 13: changing the numerator for 13 by a multiple of 13 changes its tripled contribution by a multiple of 39. So I do all the squaring and multiplying modulo 39. Since 39 is odd, I can compute its inverse modulo a power of two once and reuse it for all Montgomery multiplication in the pair. I triple the numerator for 13, add the one for 39, and change rear from Montgomery form fair formerly for the final division.
There’s a nice trick inner all exponentiation, too. Suppose a Montgomery reduction leaves \(-14\) modulo 39. Normally I’d add 39 to get 25. But if I’m going to quadrate it next, 14 is fair as good: \(14^2\equiv25^2\pmod{39}\). Even if the current exponent bit calls for doubling first, the next square still erases the sign. So for all but the final bit, I use vector absolute values alternatively of correcting the sign. On the final bit I do the usual reduction, since the numerator needs the genuine residue fairly than its negative. That alter made the exponentiation iteration concerning 8% faster on Zen 5.
Each vector lane handles a distinct odd modulus. I lot moduli with the identical set of energetic powers so that one lane doesn’t sit idle during another steps through three additional residues. The hardware multiplies 52-bit inputs, so the largest modulus matters: ours is concerning \(2d/3\), compared alongside \(d\) for Huvent. That lets us attain concerning 50% farther into \(\pi\) before we need wider arithmetic.
I initially went looking for a sequence alongside small fractions per bit. If you measure all fraction separately, Euler’s sequence really needs more fractions per bit than Huvent’s, so it has been uncomplicated to continue over. Sharing the standardized arithmetic throughout average and paired denominators is what makes it faster, and I doubtful akin tricks use to another BBP-type series.