Folding the Plouffe-Bellard Digit-Extraction Scheme for π\pi: A Legendre-Symbol Block Identity and an O(n3/2)O(n^{3/2}) Variant

A mix of human-written and AI-generated textHuman understanding: all partsmath.NT — Number Theory

Contributed by Mark Kim ↗

Version 1 / Dec 05, 2026 / CC BY 4.0

Abstract

The algorithm of Plouffe and Bellard computes the digits of π\pi at positions n+1,…,n+gn+1, \dots, n+g in any base from the series π=2∑k≥0k!/(2k+1)!!\pi = 2 \sum_{k \ge 0} k!/(2k+1)!! in O(n2)O(n^2) operations on machine words and O(1)O(1) words of memory: for each of the π(2N)\pi(2N) primes q<2Nq < 2N with N≈nlog⁡2(10)N \approx n \log_2(10), it runs a loop of length NN. We prove that for every prime qq with q2>2N+qq^2 > 2N + q, the sum of the contributions of all complete blocks k≡j(modq)k \equiv j \pmod{q} collapses to the Legendre symbol (−1/q)(-1/q), by the identity ∑i=0(q−1)/2(2ii)2−i≡(−1)(q−1)/2(modq)\sum_{i=0}^{(q-1)/2} \binom{2i}{i} 2^{-i} \equiv (-1)^{(q-1)/2} \pmod{q}. The loop of length NN is replaced by a partial sum of at most min⁡(L,h−L+1)\min(L, h - L + 1) terms, where h=(q−1)/2h = (q - 1)/2, and the total number of loop iterations drops from (2+o(1))N2/ln⁡N(2 + o(1)) N^2 / \ln N to at most (7/24+o(1))N2/ln⁡N(7/24 + o(1)) N^2 / \ln N, a reduction factor of at least 6.86.8. Each remaining partial sum is a prefix or suffix sum of a hypergeometric sequence modulo qq; computing it with the baby-step giant-step method of Bostan, Gaudry, and Schost takes O(Llog⁡L)O(\sqrt{L} \log L) operations and gives a total complexity of O(n3/2)O(n^{3/2}) operations. The proposed framework returns the correct digits in all verification cases; on benchmarks with n=2000,…,32000n = 2000, \dots, 32000, it is 4.24.2 to 18.718.7 times faster than the standard loop of the Plouffe--Bellard scheme on the same series, and the measured execution time of a single tail sum scales strictly like L\sqrt{L} up to L=4.1×107L = 4.1 \times 10^7. The complete source code, verification test suites, and benchmarks are made available.

References

  1. D. H. Bailey, P. B. Borwein, S. Plouffe, On the rapid computation of various polylogarithmic constants, Math.\ Comp.\ 66 (1997), 903--913.
  2. S. Plouffe, On the computation of the nn-th decimal digit of various transcendental numbers, 1996, arXiv:0912.0303 [math.NT]. https://arxiv.org/abs/0912.0303arXiv
  3. F. Bellard, Computation of the nn'th digit of π\pi in any base in O(n2)O(n^{2}), 1997. https://bellard.org/pi/pi_n2/pi_n2.htmllink
  4. A. Bostan, P. Gaudry, É. Schost, Linear recurrences with polynomial coefficients and application to integer factorization and Cartier--Manin operator, SIAM J.\ Comput.\ 36 (2007), 1777--1806.
  5. A. Granville, Binomial coefficients modulo prime powers, Canad.\ Math.\ Soc.\ Conf.\ Proc.\ 20 (1997), 253--275.
  6. J. B. Rosser, L. Schoenfeld, Approximate formulas for some functions of prime numbers, Illinois J.\ Math.\ 6 (1962), 64--94.
  7. D. G. Cantor, E. Kaltofen, On fast multiplication of polynomials over arbitrary algebras, Acta Inform.\ 28 (1991), 693--701.
  8. M. Kim, source code: pi_decimal_digits.py, verify_pi_digits.py, bench_pi_digits.py. https://github.com/tankwars92/pi_decimal_digits.link

Version history

  1. v1 · 11 pagesSubmitted by Mark KimInitial depositCurrentDec 05, 2026