Every prime above 5 ends in the digit 1, 3, 7 or 9. So to find primes you only need to look at 40% of all numbers. This mission formalizes a sieve of Eratosthenes that works only on that 40% (the residues prime to 10), together with the classical facts it rests on. It follows the paper A window form of the sieve of Eratosthenes on the residues prime to ten, with a machine-checked proof (mfala22ss, 2026) in the public repository prime-endings.
Status, stated plainly: every result here is classical; the source itself says so. The last-digit restriction and wheel sieves are old: Pritchard's wheel sieve is from 1981, and Dirichlet proved that every reduced residue class contains infinitely many primes in 1837. What this mission adds is a complete, kernel-checked Lean development of the sieve's correctness:
on all numbers,
on an arbitrary window [a,b],
as a "pick up the next prime" loop,
with exact prime counts computed by the kernel.
Setting
All numbers are natural numbers.
A number n>5survives (Survives n) if it ends in 1,3,7,9 and no prime p with p2≤n divides it.
In the window sieve on [a,b], a number n is crossed out (CrossedOut a b n) if n=p⋅k where:
p is a prime other than 2 and 5, with p2≤b;
k is coprime to 10, with max(p,⌈a/p⌉)≤k≤⌊b/p⌋.
In Lean the ceiling is written (a + p - 1) / p.
In the pick-up loop, n is still standing after the primes up to P (Alive P n) if:
n>5 and n is coprime to 10;
n is not q⋅k for any prime q∈/{2,5} with q≤P and any multiplier k≥q coprime to 10.
Target
Goal (sieve correctness). A number survives the sieve on the 40% exactly when it is a prime above 5:
Survivesn⟺n prime∧5<n.
Milestones.
Endings and wheels:
every prime p>5 has pmod10∈{1,3,7,9};
every prime p>5 has pmod30∈{1,7,11,13,17,19,23,29};
every prime p>7 is coprime to 210=2⋅3⋅5⋅7;
no n>5 ending in 0,2,4,5,6,8 is prime.
Infinitely many in each ending:
for every n there is a prime above n ending in 1,3,7 or 9 (elementary, via 10(n+5)!+1);
for each d∈{1,3,7,9} there are infinitely many primes ending in d (Dirichlet, from Mathlib).
Why x is enough:
a composite n has smallest prime factor at most n;
an n≤x with no prime factor p, p2≤x, is prime;
on the 40%, testing 3 and 7 alone leaves exactly the primes up to 120, and first fails at 121=112.
Counting:
#{n≤x:Survivesn}+3=π(x) for x≥5;
the kernel evaluates π(100)=25 and π(1000)=168 by the sieve.
Window and bootstrap:
the window sieve keeps exactly the primes in [max(a,6),b];
the primes up to Y decide every n≤Y2;
no finite list of primes suffices forever.
Pick-up loop:
the smallest number still standing above the last prime picked up is the next prime (only 5 is skipped);
once p2>x, everything still standing up to x is prime.
Significance
The result. These statements justify the last-digit sieve as an algorithm: it never loses a prime and never keeps a composite, on the whole line, on a window, and step by step.
Formalizing it. Proofs of all items exist and compile against this environment (standard axioms only); the mission records them as a reusable, elementary sieve library. The mod-210 wheel item is new relative to the source.
Difficulty
There is no deep obstacle: the contribution is precision. The window form needs exact ceiling and floor bookkeeping, max p ⌈a/p⌉ ≤ k ≤ ⌊b/p⌋, so that every composite in the window is hit exactly once by a multiplier coprime to 10. The pick-up loop needs the right invariant to show the next survivor is prime.
Formalization scope
Naturals throughout, with truncated subtraction and floor division.
"Ends in d" is n % 10 = d; "coprime to 10" is Nat.Coprime n 10.
π is Mathlib's Nat.primeCounting.
The two counts are evaluated by decide. No native_decide and no custom axioms are used.
mfala22ss, A window form of the sieve of Eratosthenes on the residues prime to ten, with a machine-checked proof (2026), https://github.com/mfala22ss/prime-endings (papers/sieve-window, Lean file lean/Solution.lean).
P. Pritchard, A sublinear additive sieve for finding prime numbers, Commun. ACM 24 (1981) 18-23 (wheel sieve) [UNVERIFIED].
P. G. L. Dirichlet, 1837 (primes in arithmetic progressions; formalized in Mathlib).