# 提取π十六进制位算法提速61%

Original title: A faster algorithm for extracting hexadecimal digits of π

> Hacker News story 50638075 · language: zh · submitted 2026-11-25
> Web version: https://zeli.app/zh/story/50638075
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 74 · by [vinhnx](<https://news.ycombinator.com/user?id=vinhnx>) · 💬 5
[original](<https://www.stylewarning.com/posts/faster-digit-extraction/>) · [HN discussion](<https://news.ycombinator.com/item?id=50638075>)

## Summary

我提出了一种提取π十六进制位的新算法，无需计算前序数字即可直接定位。该算法在运算量上全面超越了标准的Bailey–Borwein–Plouffe、Bellard以及Huvent公式。核心突破在于通过重新排列分母，让多个项共享模运算过程。相比Huvent的八项公式，在计算大偏移量时，新算法减少了约61%的除法运算和38%的完整模指数运算。通过利用Euler级数中分母的共性，我们仅需两次模指数运算即可处理原本需要多次计算的项，甚至能同时处理模M和模3M的情况。在AVX-512 IFMA硬件加速下，实测提取十亿位十六进制数字的速度提升了近60%。

## Key quote

> 对于每跳过的百万个十六进制位，相比Huvent算法，在引入任何机器特定优化之前，我们就能节省约一百万次完整的模指数运算。

## HN discussion (5 comments)

### [prajaybasu](<https://news.ycombinator.com/item?id=50648840>)

> 我相信我和 Claude、Gemini 以及 Astra 合作，发现了一种更快的算法来提取 π 的十六进制位。
>
> https://github.com/prajaybasu/math

### [nickcw](<https://news.ycombinator.com/item?id=50640564>)

> 那个主要的方框公式等价于下面这个 Machin 风格的 π 公式：
>
> pi = 4 arctan 4/7 + 4 arctan 1/8 + 2 arctan 8/31 + 2 arctan 1/32
>
> 因为 arctan 中的每个分母都是 2 的幂，或者与 2 的幂相差 1，我们可以由此构造一个 BBP 公式，从而在不计算前几位的情况下直接计算 π 的任意十六进制位。
>
> 我在 https://scholarlycommons.pacific.edu/euleriana/vol3/iss1/3/ 写了一篇关于其工作原理的通俗易懂的文章（文章中也引用了这篇！）。
>
> 方框公式令人兴奋，因为它经过了重新排列，拥有了一个统一的公分母，因此每项只需要 1 次除法。
>
> 除法运算很慢，所以减少除法次数能加快计算速度。加上另一个技巧，这使得计算速度比目前已知的最佳 BBP 公式快 40%。
>
> \> There’s a choice hiding in the last step of Bellard’s terse derivation.
>
> 这句话让我笑了。我花了好几天才搞懂 Bellard 那简略的推导过程 :-)

### [dh2022](<https://news.ycombinator.com/item?id=50644696>)

> 计算提升百分比的公式是错的。
>
> 正确的公式应该是 100% - t_after / t_before。
>
> 文章中的公式似乎是 t_before / t_after - 100%。这是不正确的。
>
> 例如，如果 t_after 非常小（意味着计算效率极高，提升巨大），这个公式会显示提升超过 100%（这显然是不对的）

---

## Cite

- Page: https://zeli.app/zh/story/50638075
- Date: 2026-11-25
- Source: stylewarning.com (https://www.stylewarning.com/posts/faster-digit-extraction/)
- Hacker News: https://news.ycombinator.com/item?id=50638075

## Other languages

- en: https://zeli.app/story/50638075 · markdown: https://zeli.app/story/50638075.md
- ja: https://zeli.app/ja/story/50638075 · markdown: https://zeli.app/ja/story/50638075.md
- ko: https://zeli.app/ko/story/50638075 · markdown: https://zeli.app/ko/story/50638075.md
- de: https://zeli.app/de/story/50638075 · markdown: https://zeli.app/de/story/50638075.md
- es: https://zeli.app/es/story/50638075 · markdown: https://zeli.app/es/story/50638075.md
- ru: https://zeli.app/ru/story/50638075 · markdown: https://zeli.app/ru/story/50638075.md
