The last-digit sieve: survival, window crossing-out, and standing numbers
DefinitionPrimeEndings_Defs- : , , and no prime with divides .
- : with prime, , , , , .
- : , , and for every prime and with .
import Mathlib
/-!
# Definitions for the last-digit sieve on the 40% (primes ending in 1, 3, 7, 9)
The sieve keeps only numbers above 5 ending in 1, 3, 7 or 9 (40% of all numbers) and crosses out
multiples of primes. Source: mfala22ss, *prime-endings*, https://github.com/mfala22ss/prime-endings
(Lean file `lean/Solution.lean`, 2026).
-/
namespace PrimeEndings
/-- The survival rule of the sieve on the 40%: `n > 5` ends in 1, 3, 7 or 9, and no prime `p`
with `p * p ≤ n` divides it. -/
def Survives (n : ℕ) : Prop :=
5 < n ∧ (n % 10 = 1 ∨ n % 10 = 3 ∨ n % 10 = 7 ∨ n % 10 = 9) ∧
∀ p < n + 1, p * p ≤ n → p ∣ n → ¬ p.Prime
instance (n : ℕ) : Decidable (Survives n) := by unfold Survives; infer_instance
/-- `n` is crossed out when sieving the window `[a, b]`: `n = p * k` for a sieving prime
`p ∉ {2, 5}` with `p * p ≤ b` and a multiplier `k` coprime to 10 with
`max p ⌈a / p⌉ ≤ k ≤ ⌊b / p⌋`. -/
def CrossedOut (a b n : ℕ) : Prop :=
∃ p k, p.Prime ∧ 3 ≤ p ∧ p ≠ 5 ∧ p * p ≤ b ∧ Nat.Coprime k 10 ∧
max p ((a + p - 1) / p) ≤ k ∧ k ≤ b / p ∧ p * k = n
/-- `n` is still standing after the primes up to `P` were picked up: `n > 5`, `n` is coprime to
10, and `n` is not `q * k` for a picked-up prime `q ∉ {2, 5}` and a multiplier `k ≥ q` coprime
to 10. -/
def Alive (P n : ℕ) : Prop :=
5 < n ∧ Nat.Coprime n 10 ∧
∀ q k, q.Prime → 3 ≤ q → q ≠ 5 → q ≤ P → Nat.Coprime k 10 → q ≤ k → q * k ≠ n
end PrimeEndings
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
This file introduces three predicates on natural numbers , all in the namespace PrimeEndings.
1. for holds exactly when all three of the following hold:
- ;
- the last decimal digit of is , , or , i.e. ;
- for every natural number with (i.e. ), if and , then is not prime.
So the third clause says: no prime with divides . The bound is redundant given (for ). The quantifier also ranges over and ; divides only when (excluded by ) and is not prime, so these cases are harmless. The predicate is declared decidable.
2. for holds exactly when there exist natural numbers and such that
- is prime, and (so );
- ;
- (i.e. is odd and not divisible by );
- ;
- (natural-number floor division);
- .
Since , the subtraction never truncates, and . Thus the lower bound reads and (equivalently and ); the upper bound is equivalent to . In words: is a product of a prime with and a cofactor coprime to , with . Note the lower bound on is in terms of via a ceiling and the upper bound in terms of via a floor.
3. for holds exactly when
- ;
- ;
- for all natural numbers : if is prime, , , , and , then .
That is, is coprime to and is not of the form with a prime in and coprime to . There is no upper bound on and no requirement relating to ; need not be prime. If the last clause is vacuous and reduces to .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.