\section{The three machines}\label{sec:machines} We now assemble the machines. Theorems~A and~B are non-adaptive batches of counting queries on a linearly padded input (Theorem~\ref{thm:batch} and Corollary~\ref{cor:padding}); Theorem~C makes adaptive calls to the machine of Theorem~A (Theorem~\ref{thm:adaptivecalls}). \medskip\noindent\emph{Words and values.} For a word $w=w_0w_1\cdots$ over $\{0,1\}$ let $\mathrm{val}(w)=\sum_iw_i2^i$ (\lean{ofWord}), and for $N\in\N$ let $\mathrm{bin}(N)$ be its binary expansion, least significant bit first (\lean{Nat.bits}). Thus $\abs{\mathrm{bin}(N)}=b(N)$, $\mathrm{val}(\mathrm{bin}(N))=N$, $\mathrm{val}(w)<2^{\abs w}$ for every word $w$, and appending zeros to a word, at its most significant end, does not change its value. The padding of Section~\ref{sec:batches} is of this kind: $\mathrm{pad}(w)=w\,0^{2047(\abs w+1)}$, so that $\mathrm{val}(\mathrm{pad}(w))=\mathrm{val}(w)$ and $\abs{\mathrm{pad}(w)}+1=2048(\abs w+1)$ (\lean{LinearPadding.\allowbreak pad}, \lean{pad\_value}, \lean{pad\_length}). The machines of Theorems~A and~B accept every word $w$, not only binary expansions, and compute the answer for $\mathrm{val}(w)$ within a time bound in $\abs w$; Theorem~C uses this generality. \subsection{Theorem A}\label{mach:A} \emph{Precision.} For a word $u$ put \[ \beta_u=b(\mathrm{val}(u))+1 . \] Then (i) $\mathrm{val}(u)<2^{b(\mathrm{val}(u))}\le2^{\beta_u}$; (ii) $\beta_u\le\abs u+1$, because $\mathrm{val}(u)<2^{\abs u}$ gives $b(\mathrm{val}(u))\le\abs u$; (iii) $\beta_u$ depends only on $\mathrm{val}(u)$, so $\beta_{\mathrm{pad}(w)}=\beta_w$; and (iv) $\beta_u$ is computable from $u$ in polynomial time and, by (ii), can be written in unary in polynomial time. For $N=\mathrm{val}(u)\ge1$ this is $\beta(N)+1$, one more than the precision of Section~\ref{sec:rounding} (and $\beta_u=\beta(0)$ for $N=0$). This is harmless: Sections~\ref{sec:rounding}--\ref{sec:network} use the precision only through $N\le2^\beta$ (Theorem~\ref{thm:rounding}) and, for the padding, $\beta\le\abs w+1$, and Proposition~\ref{prop:queries} and the bit bounds of Section~\ref{sec:bitbounds} hold with the same proofs for every such $\beta$. Lean: \lean{PrimeMachineInput.\allowbreak precision}, \lean{precision\_le}, \lean{value\_bound}, \lean{precision\_pad}; the statements of Sections~\ref{sec:rounding}--\ref{sec:network} are formalized for every $\beta$ with $N\le2^\beta$ (\lean{IntegerPrimeQueryFinal.\allowbreak queryResult\_eq\_primeSum}, \lean{PrimeQueryBounds.\allowbreak concrete\_package\_exists}). \medskip\noindent\emph{The batch.} We use the counting package of Section~\ref{sec:bitbounds}: its constant is $2^{22}\cdot132$, $R(u)=\abs u$, $\mathrm{Ok}$ is always true, and $q_b=2^{13}\cdot132+1$. Fix a word $u$, put $N=\mathrm{val}(u)$ and $\beta=\beta_u$, and take the certified tables at precision $\beta$ produced by the algorithm of Theorem~\ref{thm:generator}. Let $\mathbf q_1,\dots,\mathbf q_K$ be the query indices of Section~\ref{sec:network} at precision $\beta$, each listed once, in an order computed from $\beta$; here $K=(\beta+1)^2\cdot2\cdot(2M_\beta)^4\cdot16$. The \emph{item list} of $u$ consists of the pairs $(\pi(\mathbf q_j),c(\mathbf q_j))$, with the query word $\pi(\mathbf q_j)$ and the integer weight $c(\mathbf q_j)=-\re\gamma(\mathbf q_j)$ of Section~\ref{sec:network}, for those $j$ with $\abs{\pi(\mathbf q_j)}\le\abs u$, in this order. The batch $\mathcal B_A$ (Definition~\ref{def:batch}) is as follows. \begin{itemize} \item $r(u)$ is the length of the item list; $\sigma_0(u)$ is the empty list; the query $\pi_i$ of round $i$ is the word of the $i$-th item; and $\sigma_{i+1}$ is $\sigma_i$ followed by $Q(u,\pi_i)$. Thus $\sigma_i$ is the list of the first $i$ counts. \item $\mathrm{fin}(u,\sigma)=\mathrm{bin}(\max(\mathcal R,0))$, where, with $c_i$ the weight of the $i$-th item, $\sigma(i)$ the $i$-th entry of $\sigma$ and $s=s_\beta=M_\beta2^{p_\beta+t_\beta}$, \[ \mathcal R=\Bigl\lfloor\frac{2\sum_ic_i\,\sigma(i)+s^4}{2s^4} \Bigr\rfloor . \] \end{itemize} Definition~\ref{def:batch} asks that every query of the run be legal for every input word $u$, padded or not. The length condition in the item list guarantees this; on padded words it removes nothing, by the next lemma. \begin{lemma}\label{mach:correctA} For every word $w$, the answer of $\mathcal B_A$ on $\mathrm{pad}(w)$ is $\mathrm{bin}(S(\mathrm{val}(w)))$. \end{lemma} \begin{proof} Let $u=\mathrm{pad}(w)$ and $N=\mathrm{val}(w)=\mathrm{val}(u)$. By (i)--(iii), $\beta=\beta_u=\beta_w$ satisfies $N\le2^\beta$ and $\beta\le\abs w+1$, and the tables are certified by Theorem~\ref{thm:generator}. By Section~\ref{sec:bitbounds}, every query word $\pi(\mathbf q)$ at precision $\beta$ has length at most $\abs{\mathrm{pad}(w)}$, and $Q(\mathrm{pad}(w),\pi(\mathbf q)) =\#(\mathbf q)$. Hence the length condition keeps all $K$ items, the batch asks the $K$ queries in order, and its final state is $(\#(\mathbf q_1),\dots,\#(\mathbf q_K))$. Since every query index occurs exactly once, Proposition~\ref{prop:queries} gives \[ \sum_{j=1}^Kc(\mathbf q_j)\,\#(\mathbf q_j) =-\re\sum_{\mathbf q}\gamma(\mathbf q)\,\#(\mathbf q)=\re Z_N , \] and \eqref{eq:integerround}, that is, Theorem~\ref{thm:rounding} with $N\le2^\beta$, gives $\mathcal R=S(N)\ge0$. So $\mathrm{fin}$ returns $\mathrm{bin}(S(N))$. \end{proof} No case distinction is needed for small inputs. For $N\le1$ no index satisfies $2\le n\le N$, so every count $\#(\mathbf q)$ vanishes, $Z_N=0$, and $\mathcal R=\floor{s^4/(2s^4)}=0=S(N)$; the output is the empty word. Lean: \lean{PrimeSumMachine.\allowbreak answer\_pad}, \lean{items\_pad}, \lean{BatchCollect.\allowbreak answer\_eq}, \lean{PrimeQueryEnumeration.\allowbreak items\_package\_exists}, \lean{sum\_items\_count}, \lean{PrimeQueryFinish.\allowbreak finish\_queries\_eq\_primeSum}, \lean{CertifiedGenerator.\allowbreak generate\_certified}. \begin{lemma}\label{mach:boundA} The maps $r,\sigma_0,\mathrm{qry},\mathrm{upd},\mathrm{fin}$ of $\mathcal B_A$ are computable in polynomial time, and $\mathcal B_A$ is bounded by some constant $a$ in the sense of Definition~\ref{def:batch}. \end{lemma} \begin{proof} Let $u$ have length $n'$. By (iv), $\beta$ is available in unary in time polynomial in $n'$, and $\beta\le n'+1$. The list of query indices has $K=32(\beta+1)^2(70\beta+370)^4\le32\cdot370^4(\beta+2)^6$ entries and is enumerated in time polynomial in $\beta$. For each index, the query word $\pi(\mathbf q)$ and the weight $c(\mathbf q)$ are computed in polynomial time from $\beta$, $N$ and four poles and four residues of the tables, which Theorem~\ref{thm:generator} produces in time polynomial in $\beta$; the length test is trivial. So the item list is computable in polynomial time, and so are $r$ (its length), $\sigma_0$, $\mathrm{qry}$ (a lookup in the list) and $\mathrm{upd}$ (appending a number). The final map computes $s$, which has $O(\beta)$ bits, a sum of at most $K$ products of integers of polynomial size, one integer division and a binary expansion; it is polynomial-time. For the bounds, $r(u)\le K$ is polynomial in $n'$. Every query of the run is legal: $\mathrm{Ok}$ always holds, and $\abs{\pi_i}\le\abs u=R(u)$ by the length condition. Hence every count is below $2^{q_b(n'+2)}$ by Definition~\ref{def:package}(1), and each state $\sigma_i$, a list of at most $K$ such counts, has polynomial size. The answer is computed in polynomial time from $u$ and $\sigma_{r(u)}$, so it has polynomial size too. A constant $a$ dominating these polynomials bounds the batch. \end{proof} Lean: \lean{PrimeQueryEnumeration.\allowbreak enumerate\_length}, \lean{enumerate\_polyT}, \lean{wordQuery\_polyT}, \lean{integerWeight\_polyT}, \lean{items\_polyT}; \lean{PrimeSumMachine.\allowbreak items\_polyT}, \lean{finish\_polyT}, \lean{valid}, \lean{bounds}; \lean{BatchCollect.\allowbreak rounds\_polyT}, \lean{initial\_polyT}, \lean{query\_polyT}, \lean{update\_polyT}, \lean{bounds}; \lean{PrimeQueryFinish.\allowbreak finish\_polyT}. \begin{proof}[Proof of Theorem~A] The map $\mathrm{pad}$ is computable in polynomial time, and $\abs w\le\abs{\mathrm{pad}(w)}$ and $\abs{\mathrm{pad}(w)}+2\le2049(\abs w+2)$ for every word $w$. By Lemma~\ref{mach:boundA} the batch $\mathcal B_A$ satisfies the hypotheses of Theorem~\ref{thm:batch}, and Corollary~\ref{cor:padding} with $L=2049$ gives a \lean{TM2} machine $M_A$ with finite alphabets and a constant $C$ such that for every word $w$, $M_A$ started on $w$ outputs the answer of $\mathcal B_A$ on $\mathrm{pad}(w)$ within $\floor{C\cdot2^{C\abs w/\sqrt{\log_2(\abs w+2)}}}$ steps. By Lemma~\ref{mach:correctA} this answer is $\mathrm{bin}(S(\mathrm{val}(w)))$. For $w=\mathrm{bin}(N)$ we have $\abs w=b(N)$ and $\mathrm{val}(w)=N$, so $M_A$ outputs $S(N)$ within $T_C(N)$ steps, for every $N\in\N$. The bound is in the length $b(N)$ of the original input, although the networks see the padded word of length $2048(b(N)+1)-1$; and it includes the generation of the tables, the enumeration of the queries and the final rounding, which are part of the batch. \end{proof} Lean: \lean{NatBatchQuery.\allowbreak machine\_TM\_linear\_input}, \lean{LinearPadding.\allowbreak pad\_length\_bounds}, \lean{PrimeSumMachine.\allowbreak TM\_of\_package}, \lean{primeSum\_TM} (every word $w$), \lean{primeSum\_TM\_all\_inputs} (every $N$), and \lean{PrimeIndependent.\allowbreak primeSum\_time}. For Theorem~C we need the machine of Theorem~A before compilation. \begin{corollary}\label{mach:steppedA} There are a stepped program $\mathcal P_A$ with polynomial-time steps and a constant $A$ such that for every word $x$ there are $T,S$ with $\mathcal P_A\colon x\Downarrow_{T,S}\mathrm{bin}(S(\mathrm{val}(x)))$ and $T,S,\abs x\le2^{X_A(\abs x)}$. \end{corollary} \begin{proof} Let $\mathcal P_A$ be the program of the proof of Theorem~\ref{thm:batch} for $\mathcal B_A$, with its input first mapped through $\mathrm{pad}$; this is the program to which Lemma~\ref{lem:compile} is applied in the proof of Corollary~\ref{cor:padding}. If $A_0$ is the constant of that proof for $\mathcal B_A$, then on $\mathrm{pad}(x)$ the time, the space and $\abs{\mathrm{pad}(x)}$ are at most $2^{X_{A_0}(\abs{\mathrm{pad}(x)})}\le 2^{X_{2049A_0}(\abs x)}$, and $\abs x\le\abs{\mathrm{pad}(x)}$. Take $A=2049A_0$; the output is correct by Lemma~\ref{mach:correctA}. \end{proof} Lean: \lean{PrimeSumOracle.\allowbreak stepped\_of\_package}, \lean{PrimeSumOracle.\allowbreak primeSum\_stepped}. \subsection{Theorem B}\label{mach:B} By Lemma~\ref{lem:indicator} and the proof of Proposition~\ref{prop:four}, \[ \pi(N)=-\sum_{n=2}^{N}\ \sum_{x\in\Units(n)}\ \sum_{d\mid n}\ \sum_{y\in\Units(d)} A(n,d,x,y), \] that is, \eqref{eq:phasesum} with the weight $n$ replaced by $1$. Write $\Sigma'_N(B)$ for the right-hand side with $A$ replaced by a function $B$ on indices. There are at most $N^3$ indices of level $N$ (Lemma~\ref{lem:count}), so the proof of Proposition~\ref{prop:stability} gives $\abs{\Sigma'_N(B)-\pi(N)}\le N^3\varepsilon\le N^4\varepsilon$ when $\abs{B-A}\le\varepsilon$. Hence the error budget of Theorem~\ref{thm:rounding} applies unchanged: for every $\beta$ with $N\le2^\beta$, every choice of certified tables at precision $\beta$ and $B$ as in Theorem~\ref{thm:rounding}, $\floor{\re\Sigma'_N(B)+\frac12}=\pi(N)$. As in \eqref{eq:integerround}, $\Sigma'_N(B)=Z'_N/s^4$ with $Z'_N\in\Z[i]$, and \[ \pi(N)=\Bigl\lfloor\frac{2\re Z'_N+s^4}{2s^4}\Bigr\rfloor . \] Lean: \lean{PrimeCountFormula.\allowbreak primeCounting\_phase}, \lean{phaseCount\_error}, \lean{primeCounting\_round}, and \lean{PrimeCountInteger.\allowbreak integerResult\_eq\_primeCounting}. The reindexing and Lemmas~\ref{lem:components} and~\ref{lem:partition}, the latter with $\omega\equiv1$, give $Z'_N=-\sum_{\mathbf q}\gamma(\mathbf q)\,\#'(\mathbf q)$ with the same query indices and coefficients as in Proposition~\ref{prop:queries} and \[ \#'(\mathbf q)=\sum_v\prod_{k<4}\kappa^{\iota_k}_{k,\ell_k}(v), \] the sum over the same bases $v$ as for $\#(\mathbf q)$. The description of $\mathbf q$ is that of Section~\ref{sec:network} with one comparison changed: the weight variable $x_5$ is bounded by $x_5<1$ instead of $x_5