\documentclass[11pt,a4paper]{article} \usepackage[margin=1in]{geometry} \usepackage{amsmath,amssymb,amsfonts,amsthm} \usepackage{booktabs} \usepackage{array} \usepackage{caption} \usepackage{enumitem} \usepackage{hyperref} \hypersetup{ colorlinks=true, linkcolor=blue, citecolor=blue, urlcolor=blue, pdftitle={Folding the Plouffe--Bellard digit-extraction scheme for pi}, pdfauthor={Mark Kim} } \setlength{\parindent}{1.25em} \setlength{\parskip}{0.25em} \emergencystretch=3em \theoremstyle{plain} \newtheorem{lemma}{Lemma} \newtheorem{theorem}{Theorem} \newtheorem{corollary}{Corollary} \newtheorem{proposition}{Proposition} \theoremstyle{remark} \newtheorem{remark}{Remark} \newcommand{\fl}[1]{\left\lfloor #1\right\rfloor} \newcommand{\ce}[1]{\left\lceil #1\right\rceil} \newcommand{\vq}{v_q} \newcommand{\Z}{\mathbb{Z}} \newcommand{\Q}{\mathbb{Q}} \newcommand{\F}{\mathbb{F}} \title{Folding the Plouffe--Bellard Digit-Extraction Scheme for $\pi$: A Legendre-Symbol Block Identity and an $O(n^{3/2})$ Variant} \author{Mark Kim} \date{November 3, 2026} \begin{document} \maketitle \begin{abstract} The algorithm of Plouffe and Bellard computes the digits of $\pi$ at positions $n+1,\dots,n+g$ in any base from the series $\pi=2\sum_{k\ge0}k!/(2k+1)!!$ in $O(n^{2})$ operations on machine words and $O(1)$ words of memory: for each of the $\pi(2N)$ primes $q<2N$ with $N\approx n\log_{2}10$ it runs a loop of length $N$. We prove that for every prime $q$ with $q^{2}>2N+q$ the sum of the contributions of all complete blocks $k\equiv j\pmod q$ collapses to the Legendre symbol $\left(\tfrac{-1}{q}\right)$, by the identity $\sum_{i=0}^{(q-1)/2}\binom{2i}{i}2^{-i}\equiv(-1)^{(q-1)/2}\pmod q$. The loop of length $N$ is replaced by a partial sum of at most $\min(L,h-L+1)$ terms, $h=(q-1)/2$, and the number of loop iterations drops from $(2+o(1))N^{2}/\ln N$ to at most $(7/24+o(1))N^{2}/\ln N$, a factor of at least $6.8$. Each remaining partial sum is a prefix or suffix sum of a hypergeometric sequence modulo $q$; Algorithm~C evaluates it by the baby-step giant-step sample shifting of Bostan, Gaudry and Schost in $O(\sqrt{L}\log L)$ operations, and the total is $O(n^{3/2})$ operations and $O(\sqrt{n})$ words of memory. The method returns the correct digits in all verification cases; on $n=2000,\dots,32000$ it is $4.2$ to $18.7$ times faster than the loop of the Plouffe--Bellard scheme on the same series, and the measured time of one tail sum grows like $\sqrt{L}$ up to $L=4.1\cdot10^{7}$. \end{abstract} \section{Introduction} Bailey, Borwein and Plouffe~\cite{bbp} gave a formula that computes a binary digit of $\pi$ without computing the preceding digits. For the decimal base no formula of this type is known. Plouffe~\cite{plouffe} described a method that extracts the $n$-th decimal digit of $\pi$ and of other constants in $O(n^{3}\log^{3}n)$ time and little memory, and Bellard~\cite{bellard} improved the time to $O(n^{2})$ with $O(1)$ memory. Both methods write a series with factorial denominators as a sum over the primes $q$ that divide the denominators and treat every prime separately; Bellard's observation is that all terms of one prime can share one modulus. In Bellard's algorithm every prime $q<2N$ costs a loop over all $N$ terms of the series. This paper shows that for the series $\pi=2\sum k!/(2k+1)!!$ most of this work is redundant. \paragraph{Results.} \begin{enumerate}[label=(\arabic*),nosep] \item Theorem~\ref{thm:digits}: an exact expression of $\{10^{n}S_{N}\}$ as a sum of fractions $R_{q}/q^{E_{q}}$ over primes, with a rigorous error bound, and Theorem~\ref{thm:check}, a criterion that certifies the digits. \item Theorem~\ref{thm:block}: for a prime $q$ with $q^{2}>2N+q$ the contribution $R_{q}$ equals an explicit expression in the Legendre symbol $\left(\tfrac{-1}{q}\right)$, the values $t_{m}$ with $m\le N/q$, and one prefix or suffix sum of $\binom{2i}{i}2^{-i}$ modulo $q$. \item Proposition~\ref{prop:count}: the number of loop iterations of Algorithm~B is at most $(7/24+o(1))N^{2}/\ln N$ against $(2+o(1))N^{2}/\ln N$ for the loop of Plouffe and Bellard. \item Theorem~\ref{thm:bgs}: Algorithm~C evaluates the remaining sums by the sample shifting of Bostan, Gaudry and Schost and uses $O(N^{3/2})=O(n^{3/2})$ operations and $O(\sqrt{n})$ words of memory; Lemmas~\ref{lem:tail}--\ref{lem:prod} prove every step, including the invertibility of all denominators. \item Section~\ref{sec:exp}: verification against independent computations, benchmarks of the loop and of Algorithm~B for $n\le2.56\cdot10^{5}$, exact iteration counts for $n\le10^{7}$, and the measured scaling of the tail sums. \end{enumerate} \section{Preliminaries}\label{sec:prelim} Throughout, $q$ denotes a prime, $v_{q}$ the $q$-adic valuation, $\Z_{(q)}$ the ring of rationals with $v_{q}\ge0$, and $\{x\}=x-\lfloor x\rfloor$. Congruences between elements of $\Z_{(q)}$ are taken in $\Z_{(q)}/q^{e}\Z_{(q)}$. Let \[ t_{k}=\frac{k!}{(2k+1)!!}=\frac{2^{k}(k!)^{2}}{(2k+1)!},\qquad S_{N}=2\sum_{k=0}^{N}t_{k}, \] where $(2k+1)!!=1\cdot3\cdots(2k+1)$. For integers $n\ge1$, $g\ge1$ set \[ N=\ce{(n+g)\log_{2}10}+1 . \] \begin{lemma}\label{lem:series} $\pi=2\sum_{k\ge0}t_{k}$ and $0<\pi-S_{N}<2^{1-N}\le10^{-(n+g)}$. \end{lemma} \begin{proof} For $|x|<1$, $\arcsin x/\sqrt{1-x^{2}}=\sum_{k\ge0}\frac{2^{2k}(k!)^{2}}{(2k+1)!}x^{2k+1}$. At $x=1/\sqrt2$ the left side is $\frac{\pi}{4}\sqrt2$ and $x^{2k+1}=2^{-k}/\sqrt2$, hence $\pi/2=\sum_{k}2^{k}(k!)^{2}/(2k+1)!=\sum_{k}t_{k}$. Since $t_{k}/t_{k-1}=k/(2k+1)<1/2$ and $t_{0}=1$, $t_{k}<2^{-k}$ for $k\ge1$, so $\pi-S_{N}=2\sum_{k>N}t_{k}<2\sum_{k>N}2^{-k}=2^{1-N}$. The choice of $N$ gives $N-1\ge(n+g)\log_{2}10$. \end{proof} \begin{lemma}\label{lem:val} For an odd prime $q$ and $k\ge0$, \[ \vq(t_{k})=\sum_{i\ge1}\Bigl(2\Bigl\lfloor\frac{k}{q^{i}}\Bigr\rfloor-\Bigl\lfloor\frac{2k+1}{q^{i}}\Bigr\rfloor\Bigr)\ \ge\ -\lfloor\log_{q}(2k+1)\rfloor, \] and $v_{2}(t_{k})\ge0$. \end{lemma} \begin{proof} Since $q$ is odd, $\vq(t_{k})=2\vq(k!)-\vq((2k+1)!)$, and Legendre's formula gives the sum. For $m=q^{i}$ write $k=am+b$, $0\le b2k+1$. For $q=2$ the denominator $(2k+1)!!$ of $t_{k}$ is odd. \end{proof} \begin{lemma}[partial fractions]\label{lem:pf} Let $Q$ be a finite set of primes, $E_{q}\ge1$ integers, and $x\in\Q$ with $\vq(x)\ge-E_{q}$ for $q\in Q$ and $v_{p}(x)\ge0$ for $p\notin Q$. Let $r_{q}\in\Z$ satisfy $r_{q}\equiv q^{E_{q}}x\pmod{q^{E_{q}}}$ in $\Z_{(q)}$. Then $x-\sum_{q\in Q}r_{q}/q^{E_{q}}\in\Z$. \end{lemma} \begin{proof} Put $y=x-\sum_{q}r_{q}/q^{E_{q}}$. For $q'\in Q$, $y=(q'^{E}x-r_{q'})/q'^{E}-\sum_{q\ne q'}r_{q}/q^{E_{q}}$ and the first term lies in $\Z_{(q')}$ by the choice of $r_{q'}$, the others lie in $\Z_{(q')}$ because $q\ne q'$. For $p\notin Q$ every term lies in $\Z_{(p)}$. A rational number in $\Z_{(p)}$ for all primes $p$ is an integer. \end{proof} \section{The Digit Formula}\label{sec:digits} Let $Q$ be the set of primes $q$ with $3\le q\le2N+1$, $q\ne5$, and $E_{q}=\lfloor\log_{q}(2N+1)\rfloor\ge1$. \begin{theorem}\label{thm:digits} Assume $5^{n}\ge2N+1$. For $q\in Q$ let $R_{q}\in[0,q^{E_{q}})$ satisfy \[ R_{q}\equiv2\cdot10^{n}q^{E_{q}}\sum_{k=0}^{N}t_{k}\pmod{q^{E_{q}}}. \] Then $10^{n}S_{N}-\sum_{q\in Q}R_{q}/q^{E_{q}}\in\Z$ and $0<10^{n}(\pi-S_{N})<10^{-g}$. In particular \[ \{10^{n}\pi\}=\Bigl\{\sum_{q\in Q}\frac{R_{q}}{q^{E_{q}}}+\varepsilon\Bigr\},\qquad0<\varepsilon<10^{-g}. \] \end{theorem} \begin{proof} Let $x_{k}=2\cdot10^{n}t_{k}$. By Lemma~\ref{lem:val}: $v_{2}(x_{k})\ge1$; $v_{5}(x_{k})\ge n-\lfloor\log_{5}(2N+1)\rfloor\ge0$ since $5^{n}\ge2N+1$; for $q\in Q$, $\vq(x_{k})\ge-E_{q}$ because $2k+1\le2N+1$ (the factor $10^{n}$ is a $q$-adic unit); and $\vq(x_{k})\ge0$ for primes $q>2N+1$. By Lemma~\ref{lem:pf}, $x_{k}-\sum_{q\in Q}r_{q,k}/q^{E_{q}}\in\Z$ with $r_{q,k}\equiv q^{E_{q}}x_{k}$. Summing over $k\le N$ and using $\sum_{k}r_{q,k}\equiv R_{q}\pmod{q^{E_{q}}}$ gives the first claim; the second is Lemma~\ref{lem:series}. \end{proof} \begin{theorem}[certified digits]\label{thm:check} Let $g'2N+q$ and \emph{small} otherwise; there are at most $\sqrt{2N}+1$ small primes. For a large prime put $h=(q-1)/2$, $\sigma=(-1)^{h}=\left(\tfrac{-1}{q}\right)$, and write $0\le k\le N$ as $k=mq+j$ with $0\le j2N+q>2k+1$. For $i=1$, $2m-\lfloor(2mq+2j+1)/q\rfloor=-\lfloor(2j+1)/q\rfloor$, which is $-1$ iff $2j+1\ge q$, i.e.\ $j\ge h$. \end{proof} \begin{lemma}[Legendre--Wilson]\label{lem:lw} Let $s=aq+b$ with $0\le b2N+1$. By Lemma~\ref{lem:lv} only $k$ with $j\ge h$ contribute to $R_{q}\equiv2\cdot10^{n}\sum_{k\le N}q\,t_{k}$. The blocks $m6.8$. \end{proposition} \begin{proof} Small primes cost at most $(\sqrt{2N}+1)N=O(N^{3/2})$ and the computation of all $t_{m}$ costs $\sum_{q}(N/q+1)=O(N\log\log N)+O(N/\ln N)$ by Mertens' theorem. For $q>N$ (so $M=0$, $J=N$, $L=q-1-N$) the cost is $\min(q-1-N,\,h-(q-1-N)+1)$, and by the prime number theorem $\sum_{NN$ is $(\frac16+o(1))N^{2}/\ln N$ by the same computation, so $I_{B}=\Theta(N^{2}/\ln N)$: Algorithm~B improves the constant of the Plouffe--Bellard loop, not its exponent. \section{Algorithm C: Sample Shifting}\label{sec:bgs} Algorithm~C is Algorithm~B in which every tail with $\ell_{q}>\theta$ is evaluated by Algorithm~P below, and every tail with $\ell_{q}\le\theta$ by the direct steps of Lemma~\ref{lem:tail}; $\theta\ge15$ is a fixed constant. Algorithm~P is the baby-step giant-step method of Bostan, Gaudry and Schost~\cite{bgs} for the product of $\ell$ polynomial matrices; the proof below is self-contained. \paragraph{Polynomial matrices.} Let $q$ be an odd prime and \[ T(x)=\begin{pmatrix}D(x)&B(x)&0\\0&N(x)&0\\0&0&D(x)\end{pmatrix}\in\mathrm{Mat}_{3}(\F_{q}[x]), \] with $D,B,N$ of degree $\le1$ as in Algorithm~B. Then $(A_{s+1},P_{s+1},Q_{s+1})^{\top}=T(s)\,(A_{s},P_{s},Q_{s})^{\top}$. For $d\ge1$ let $F_{d}(x)=T(x+d-1)\cdots T(x+1)T(x)$. Matrices of the shape $\left(\begin{smallmatrix}a&b&0\\0&c&0\\0&0&a\end{smallmatrix}\right)$ are closed under multiplication, so $F_{d}$ has this shape, with $a,b,c\in\F_{q}[x]$ of degree $\le d$, and \begin{equation}\label{eq:fd} F_{d+e}(x)=F_{e}(x+d)\,F_{d}(x). \end{equation} Then $(A_{\ell},P_{\ell},Q_{\ell})^{\top}=T(\ell-1)\cdots T(0)\,(A_{0},P_{0},Q_{0})^{\top}$, and the $\ell$ steps are the product of the $\lfloor\ell/v\rfloor$ matrices $F_{v}(iv)$ and the remaining $\ell$, and compute $v^{-1}\bmod q$. For $d\ge1$ let $\Phi_{d}$ be the table of $F_{d}(iv)$, $0\le i\le d+1$, stored as triples $(a,b,c)$ of values. For fixed $d$ the entries of $\Phi_{d}$ are the values at $i=0,\dots,d+1$ of three polynomials in $i$ of degree $\le d$. \begin{enumerate}[label=(\arabic*),nosep] \item $\Phi_{1}$ is $T(0),T(v),T(2v)$; put $d=1$. \item For each bit $\beta$ of $v$ after the leading one, from the most to the least significant: \begin{enumerate}[label=(\alph*),nosep] \item \emph{Doubling.} Put $K=d+1$, $\delta=d\,v^{-1}$. For each of the three polynomials $f$ in $\Phi_{d}$ compute $f(K+1),\dots,f(2K+1)$ by a shift with $a=K+1$, and $f(\delta+i)$, $0\le i\le2K+1$, by shifts with $a=\delta$ and $a=\delta+K+1$, all by Lemma~\ref{lem:shift}. This gives $F_{d}(iv)=f(i)$ and $F_{d}(iv+d)=f(\delta+i)$ for $0\le i\le2d+1$. By \eqref{eq:fd}, $F_{2d}(iv)=F_{d}(iv+d)F_{d}(iv)$ gives $\Phi_{2d}$. Put $d\leftarrow2d$. \item \emph{Increment} (if $\beta=1$). Put $K=d+1$ and compute $f(K+1)$ for each of the three polynomials by a shift with $a=K+1$. Then $F_{d+1}(iv)=T(iv+d)F_{d}(iv)$, $0\le i\le d+2$, gives $\Phi_{d+1}$. Put $d\leftarrow d+1$. \end{enumerate} \item Now $d=v$. Put $w=\lfloor\ell/v\rfloor$. Multiply the vector $(A_{0},P_{0},Q_{0})^{\top}$ successively by $F_{v}(0),F_{v}(v),\dots,F_{v}((w-1)v)$, then by $T(s)$ for $s=vw,\dots,\ell-1$. \end{enumerate} \begin{lemma}[tail by sample shifting]\label{lem:prod} Let $q$ be a prime, $\ell\ge16$, $v=\lfloor\sqrt{\ell}\rfloor+1$ and $q\ge v^{2}+4v$. Algorithm~P returns $(A_{\ell},P_{\ell},Q_{\ell})$ with $O(\sqrt{\ell}\log\ell+\log\ell\cdot\log q)$ operations and $O(\sqrt{\ell})$ words of memory. \end{lemma} \begin{proof} \emph{Correctness.} Step~(2) maintains $\Phi_{d}$ by \eqref{eq:fd} with $e=d$ and $e=1$ ($F_{1}=T$); the shifts apply because the entries of $\Phi_{d}$ are polynomials in $i$ of degree $\le d\le K$ and the new tables have $2d+2$ and $d+3$ entries, as required. In step~(3), $w\le\ell/v\theta\ge15$ we have $\ell=\ell_{q}\ge16$, and $\ell\le(h+1)/2$ gives $q=2h+1\ge4\ell-1$. For $v=\lfloor\sqrt{\ell}\rfloor+1\le\sqrt{\ell}+1$ we get $v^{2}+4v\le\ell+6\sqrt{\ell}+5\le\ell+\tfrac32\ell+\tfrac5{16}\ell<3\ell\le4\ell-1\le q$, using $\sqrt{\ell}\le\ell/4$ for $\ell\ge16$. Lemma~\ref{lem:prod} applies and returns $(A_{\ell},P_{\ell},Q_{\ell})$; Lemma~\ref{lem:tail} identifies $A_{\ell}/Q_{\ell}$ with the tail sum. \emph{Cost.} We add the costs of the parts, using $\pi(x)<1.26\,x/\ln x$ for $x>1$ (Rosser--Schoenfeld~\cite{rs}); thus $\pi(2N+1)=O(N/\ln N)$. \begin{enumerate}[label=(\roman*),nosep] \item The primes $\le2N+1$ are listed by a segmented sieve of Eratosthenes with $O(N\log\log N)$ operations and $O(\sqrt{N})$ words. \item The small primes satisfy $q\le\sqrt{2N}+1$ and cost $O(N)$ each by the analysis of Algorithm~A: $O(N^{3/2})$ in total. \item Per large prime, the computation of $10^{n}\bmod q$, $2^{-h}\bmod q$, $\sigma$, the $O(1)$ inversions and the conversion of $R_{q}/q$ to fixed point cost $O(\log N)$ operations: $O(N)$ in total. \item The values $t_{m}$ and $\sum_{m\theta$ costs $O(\sqrt{\ell_{q}}\log\ell_{q}+\log\ell_{q}\log q)$ by Lemma~\ref{lem:prod}; since $h\le N$, $\ell_{q}\le(N+1)/2$ and $q\le2N+1$, this is $O(\sqrt{N}\log N)$. At most $\pi(2N+1)$ large primes give \[ O\Bigl(\frac{N}{\ln N}\Bigr)\cdot O\bigl(\sqrt{N}\log N\bigr)=O(N^{3/2}). \] \item The $|Q|\le\pi(2N+1)$ fixed-point summands are added in $O(N/\ln N)$ operations. \end{enumerate} The sum is $O(N^{3/2})$. Since $N=\lceil(n+g)\log_{2}10\rceil+1$ and $g\le16$, $N=\Theta(n)$. The memory is $O(\sqrt{N})$ words by (i) and Lemma~\ref{lem:prod}. \end{proof} \begin{remark} If a polynomial multiplication costs $\mathsf M(d)$ operations with $\mathsf M(d)/d$ nondecreasing, the proof of Lemma~\ref{lem:prod} gives $O(\mathsf M(\sqrt{\ell})+\sqrt{\ell}+\log\ell\log q)$ per tail and Theorem~\ref{thm:bgs} gives $O\bigl(\tfrac{N}{\ln N}(\mathsf M(\sqrt N)+\sqrt N)+N\log^{2}N\bigr)$. For the multiplication of Cantor and Kaltofen~\cite{ck}, $\mathsf M(d)=O(d\log d\log\log d)$ ring operations, the total is $O(n^{3/2}\log\log n)$ with no assumption on the multiplication. \end{remark} \begin{remark} The program increments $v$ and restarts if a shift meets a vanishing denominator. By Lemma~\ref{lem:prod} and the proof of Theorem~\ref{thm:bgs} this does not happen for $\ell_{q}\ge16$. Computing all $n$ digits of $\pi$ by binary splitting of a Chudnovsky series takes $O(n\log^{3}n)$ time and $\Theta(n)$ memory. Algorithms A and B keep the memory at $O(1)$ words if the primes are enumerated by trial division, which adds $O(N^{3/2})$ operations; Algorithm~C uses $O(\sqrt{n})$ words. For the time alone full computation is faster. \end{remark} \section{Experiments}\label{sec:exp} The code is in Python~3.12.3 with NumPy~2.4.4 (\texttt{pi\_decimal\_digits.py}); all runs used one core of an Intel Xeon at $2.8$~GHz, with $g=14$ guard digits (output $10$ digits). The direct loops of both algorithms are vectorized across primes with 64-bit integers; this applies equally to Algorithm~A (mode \texttt{loop}) and Algorithm~B (mode \texttt{blocks}). \paragraph{Verification.} Algorithm~B (with the direct and with the forced Algorithm~C tails, threshold $\theta=40$) and Algorithm~A returned the correct digits in all $36$ cases ($n\in\{5,12,\dots,54,100,333,1000,2500\}$; Algorithm~B with $\theta=600000$ and with $\theta=40$, Algorithm~A) against $\pi$ computed with mpmath to $3200$ digits, with no mismatch. The ten digits returned by Algorithm~B at $n=16000$, $32000$, $64000$, $128000$ equal the ten digits of $\pi$ computed with mpmath at these positions. \begin{table}[htbp] \centering \caption{Algorithm A (loop of Plouffe--Bellard) against Algorithm B (blocks), wall-clock seconds.}\label{tab:ab} \begin{tabular}{rrrr} \toprule $n$ & Algorithm A & Algorithm B & ratio\\ \midrule $2000$ & $0.80$ & $0.19$ & $4.2$\\ $4000$ & $2.31$ & $0.37$ & $6.2$\\ $8000$ & $6.29$ & $0.89$ & $7.1$\\ $16000$ & $26.6$ & $2.27$ & $11.7$\\ $32000$ & $109.9$ & $5.87$ & $18.7$\\ $64000$ & --- & $17.3$ & ---\\ $128000$ & --- & $53.9$ & ---\\ $256000$ & --- & $201.8$ & ---\\ \bottomrule \end{tabular} \end{table} \paragraph{Iteration counts.} Table~\ref{tab:count} gives the exact numbers of loop iterations $I_{A}=|Q|N$ and $I_{B}$ (small primes, $\min(L,h-L+1)$ steps for active large primes, and $M+1$ values $t_{m}$ per large prime) for $g=14$. The ratio $I_{A}/I_{B}$ grows from $9.19$ at $n=10^{3}$ to $11.20$ at $n=10^{7}$, above the bound $48/7$ of Proposition~\ref{prop:count}. \begin{table}[htbp] \centering \caption{Exact iteration counts of Algorithms A and B.}\label{tab:count} \begin{tabular}{rrrrr} \toprule $n$ & $N$ & $I_{A}$ & $I_{B}$ & $I_{A}/I_{B}$\\ \midrule $10^{3}$ & $3370$ & $2.92\cdot10^{6}$ & $3.18\cdot10^{5}$ & $9.19$\\ $10^{4}$ & $33267$ & $2.21\cdot10^{8}$ & $2.09\cdot10^{7}$ & $10.55$\\ $10^{5}$ & $332241$ & $1.79\cdot10^{10}$ & $1.62\cdot10^{9}$ & $11.06$\\ $10^{6}$ & $3321976$ & $1.51\cdot10^{12}$ & $1.35\cdot10^{11}$ & $11.19$\\ $10^{7}$ & $33219329$ & $1.30\cdot10^{14}$ & $1.16\cdot10^{13}$ & $11.20$\\ \bottomrule \end{tabular} \end{table} \begin{table}[htbp] \centering \caption{Time of one tail sum of $L$ terms modulo a prime $q\approx2L$: sequential loop in pure Python against Algorithm~C.}\label{tab:tail} \begin{tabular}{rrrr} \toprule $L$ & loop (s) & Algorithm C (s) & Algorithm C $/\sqrt{L}$ (ms)\\ \midrule $10^{4}$ & $0.002$ & $0.007$ & $0.0725$\\ $4\cdot10^{4}$ & $0.008$ & $0.008$ & $0.0401$\\ $1.6\cdot10^{5}$ & $0.030$ & $0.011$ & $0.0264$\\ $6.4\cdot10^{5}$ & $0.116$ & $0.015$ & $0.0190$\\ $2.56\cdot10^{6}$ & --- & $0.022$ & $0.0140$\\ $1.024\cdot10^{7}$ & --- & $0.037$ & $0.0117$\\ $4.096\cdot10^{7}$ & --- & $0.071$ & $0.0111$\\ \bottomrule \end{tabular} \end{table} For $L\le6.4\cdot10^{5}$ both methods returned the same residue. The time of Algorithm~C divided by $\sqrt{L}$ decreases and flattens at $0.011$~ms, so the time grows like $\sqrt{L}$ (up to the logarithmic factor) while the sequential loop is linear in $L$. \paragraph{Threshold.} With the vectorized loop of Algorithm~B, tails are summed for all primes at once at about $5\cdot10^{-8}$~s per step and prime, while one call of Algorithm~C costs at least $7$~ms. At $n=256000$ the threshold $\theta=1.5\cdot10^{5}$ gave $421.5$~s against $201.8$~s for the direct tails ($\theta=\infty$). Hence the direct loop is used up to the threshold $\theta=6\cdot10^{5}$, the default of the program, and Algorithm~C is used for longer tails. Every tail satisfies $\ell_{q}\le(N+1)/2$, which is below $6\cdot10^{5}$ for $n\le2.56\cdot10^{5}$; the times of Algorithm~B in Table~\ref{tab:ab} are therefore times of direct tails, and the exponent $3/2$ of Theorem~\ref{thm:bgs} is measured on single tails in Table~\ref{tab:tail}. \section{Related Work}\label{sec:related} Plouffe~\cite{plouffe} derived the extraction of the $n$-th decimal digit by the prime decomposition used in Section~\ref{sec:digits}, and Bellard~\cite{bellard} reduced the time to $O(n^{2})$ with the common modulus. For binary digits the BBP formula~\cite{bbp} needs $O(n\log n)$ operations. The congruence of Lemma~\ref{lem:lw} is the elementary case of the results on factorials modulo prime powers in~\cite{granville}. The method of~\cite{bgs} computes factorials modulo $q$ in $O(\sqrt{q}\log q)$ operations; Algorithm~C applies it to hypergeometric partial sums. \section{Conclusion} For every prime $q$ with $q^{2}>2N+q$ the contribution of all complete blocks of the series $\pi=2\sum k!/(2k+1)!!$ equals $\left(\tfrac{-1}{q}\right)\sum_{m