A faster algorithm for extracting hexadecimal digits of π
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
A faster algorithm for extracting hexadecimal digits of π
用于提取 π 十六进制数字的更快速算法
By Robert Smith. This post introduces a new algorithm for extracting hexadecimal digits of (\pi) without computing the preceding digits. It beats 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 savings at large offsets are about 61% fewer divisions and 38% fewer full modular exponentiations.
作者:Robert Smith。本文介绍了一种无需计算前置数字即可提取 (\pi) 十六进制数字的新算法。在运算次数上,它优于 Bailey–Borwein–Plouffe (BBP)、Bellard 和 Huvent 的标准逐项评估法。即使与 Huvent 的八项公式相比,在大偏移量下,该算法也能减少约 61% 的除法运算和 38% 的全模幂运算。
The key is a relationship 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}. } ]
其关键在于该恒等式中四个 2 的幂次之间的关系: [ \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 common denominator (M=2k+1) means we can add four remainders and divide once. The surprise is that we don’t have to compute those remainders independently either. As we will see, the gaps between them stay small multiples of (k) plus constants, so one starting power and the step (2^k\bmod M) let us reach the others by multiplication.
公分母 (M=2k+1) 意味着我们可以将四个余数相加并仅进行一次除法。令人惊讶的是,我们也不必独立计算这些余数。正如我们将看到的,它们之间的间距保持为 (k) 的小倍数加上常数,因此通过一个起始幂次和步长 (2^k\bmod M),我们可以通过乘法得到其他余数。
Even better, working modulo (3M) lets the same two exponentiations serve both (M) and (3M). Eight powers of two over two different denominators can share two full exponentiations and a single division. For every million hexadecimal places skipped, that saves about a million full exponentiations compared with Huvent, before any machine-specific optimization enters into it.
更好的是,在模 (3M) 下工作使得相同的两次幂运算可以同时服务于 (M) 和 (3M)。两个不同分母上的八个 2 的幂次可以共享两次全模幂运算和一次除法。对于每跳过一百万位十六进制数字,与 Huvent 的方法相比,在不考虑任何机器特定优化的情况下,这可以节省约一百万次全模幂运算。
The BBP trick and formula convergence. To see where the savings come from, recall the BBP trick. To skip (n) hex digits, shift (\pi) left by (d=4n) bits and take the fractional part. For a term (2^E/N) with (E\ge 0), we only need ((2^E\bmod N)/N) because the difference is an integer which doesn’t contribute to the fractional part. So even at (d=4{,}000{,}000) we never have to construct a four-million-bit numerator. The terms with negative exponents still count; they decay geometrically, so we can sum their tails directly, keeping enough guard bits to settle the last digit we want.
BBP 技巧与公式收敛性。要了解节省的来源,请回顾 BBP 技巧。要跳过 (n) 位十六进制数字,将 (\pi) 左移 (d=4n) 位并取其小数部分。对于 (E\ge 0) 的项 (2^E/N),我们只需要 ((2^E\bmod N)/N),因为其差值是一个整数,不会对小数部分产生贡献。因此,即使在 (d=4{,}000{,}000) 时,我们也无需构建一个四百万位的分子。负指数项仍然有效;它们呈几何级数衰减,因此我们可以直接对它们的尾部求和,并保留足够的保护位以确定我们想要的最后一位数字。
Last time, I counted the work by counting fractions. Bellard’s formula advances 10 bits for every seven fractions; Huvent’s formula advances 12 bits for every eight. After finding Huvent’s formula independently, I’ve feverishly tried to find one with a better ratio, without success. So I looked toward ways of optimizing existing series.
上次,我通过计算分数来统计工作量。Bellard 的公式每七个分数推进 10 位;Huvent 的公式每八个分数推进 12 位。在独立发现 Huvent 的公式后,我曾狂热地尝试寻找比例更好的公式,但没有成功。因此,我转而研究优化现有级数的方法。
One clue tipped me off. There’s a choice hiding in the last step 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 first sum into five progressions lets him write both sums in powers of (1/1024). What if, instead of lining up the powers, we lined up the denominators?
一条线索启发了我。在 Bellard 推导的最后一步中隐藏着一个选择。他有一个 (1/4) 幂次的求和,另一个是 (1/1024) 幂次的求和。由于 (4^5=1024),将第一个求和拆分为五个级数,使他能够将两个求和都写成 (1/1024) 的幂次。如果我们将分母对齐而不是对齐幂次,会怎样呢?
Lining up denominators. Lining up denominators seemed hard with the many-term series of Bellard and Huvent. One good place I found to try this is a series Euler wrote down in 1779. Craig-Wood used it to extract hexadecimal digits. In the original notation there are six rows:
对齐分母。对于 Bellard 和 Huvent 的多项级数,对齐分母似乎很困难。我发现尝试此方法的一个好地方是欧拉在 1779 年写下的一个级数。Craig-Wood 用它来提取十六进制数字。在原始符号中,它有六行:
[ \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} ]
Takahashi 将其直接写为两个求和: [ \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 middle fraction and leave (2m+1) in the denominator. As (m) runs over the nonnegative integers, (2m+1) gives every positive odd integer. The other two progressions, (4m+1) and (4m+3), split that same list between them. So each odd denominator appears twice in each sum.
注意我们的除数。由于 (4m+2=2(2m+1)),我们可以将中间分数的分子减半,并在分母中保留 (2m+1)。当 (m) 遍历非负整数时,(2m+1) 给出了每一个正奇数。另外两个级数 (4m+1) 和 (4m+3) 将同一个列表一分为二。因此,每个奇数分母在每个求和中都会出现两次。
For example, consider the denominator 13. The first progression hits it at (m=3); the middle one hits it at (m=6), where the original 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 divide by 13. We can collect terms this way for every odd denominator.
例如,考虑分母 13。第一个级数在 (m=3) 时达到它;中间的级数在 (m=6) 时达到它,此时原始分母为 26。这来自 (1/64) 幂次求和的两个贡献,以及来自 (1/1024) 幂次求和的两个贡献。幂次不同,但在除以 13 之前,所有四个分子都可以相加。我们可以通过这种方式为每个奇数分母收集项。
To derive the boxed formula 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. ]
为了直接推导出框中的公式,我们可以从高斯整数的乘法开始:((3-i)^2(7-i)=50(1-i))。取复数辐角可得到 18 世纪 Hutton 的反正切恒等式: [ 2\arctan\left(\frac13\right)+\arctan\left(\frac17\right)=\frac\pi4. ]
Bellard lists this identity as equation (7). His logarithm trick works 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 want (\pi=8I_2+4I_3). Every time (j) goes up by four, ((1+i)^j/2^{uj}) is multiplied by (-4/2^{4u}). That’s (-1/64) in the first sum and (-1/1024) in the second.
Bellard 将此恒等式列为公式 (7)。他的对数技巧适用于这两个角度:(1-(1+i)/4=(3-i)/4) 和 (1-(1+i)/8=(7-i)/8)。因此,如果我们令 [ 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}}, ] 则 (I_2=\arctan(1/3)),(I_3=\arctan(1/7)),且我们想要 (\pi=8I_2+4I_3)。每当 (j) 增加 4 时,((1+i)^j/2^{uj}) 就会乘以 (-4/2^{4u})。在第一个求和中这是 (-1/64),在第二个求和中这是 (-1/1024)。
For an odd denominator (N=2k+1), the two contributions in each logarithm come from (j=N) and (j=2N). All further doublings give multiples of four, where ((1+i)^j) is real and contributes nothing. At (j=N) the imaginary part of ((1+i)^N) is ((-1)^{
对于奇数分母 (N=2k+1),每个对数中的两个贡献来自 (j=N) 和 (j=2N)。所有进一步的倍增都会产生 4 的倍数,此时 ((1+i)^j) 为实数且无贡献。在 (j=N) 时,((1+i)^N) 的虚部为 ((-1)^{