-- Eisenstein's lattice count: for distinct odd primes p = 2hp + 1 and -- q = 2hq + 1, the floor sums Σ ⌊q k / p⌋ (k = 1..hp) and Σ ⌊p j / q⌋ -- (j = 1..hq) count the lattice points of the hp × hq rectangle below and -- above the line p j = q k, which has no lattice point on it; together they -- count hp · hq. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.NumberTheory.LatticeCount import Grace.Cumulus.NumberTheory.GaussParity import Grace.Cumulus.Cardinal -- The lattice points below and above the line -- The points (k, j) of 1..hp × 1..hq with p j < q k, and those with -- q k < p j. def lowerLattice (p q hp hq : Nat) : Set := { r ∈ halfRange(hp) × halfRange(hq) | mul(p, snd(r)) < mul(q, fst(r)) } def upperLattice (p q hp hq : Nat) : Set := { r ∈ halfRange(hp) × halfRange(hq) | mul(q, fst(r)) < mul(p, snd(r)) } theorem lowerLattice.mem_iff (p q hp hq k j : Nat) shows (k, j) ∈ lowerLattice(p, q, hp, hq) ↔ k ∈ halfRange(hp) ∧ j ∈ halfRange(hq) ∧ p * j < q * k proof constructor . intro member obtain ⟨inProduct, below⟩ := lowerLattice.membership.forward with member have components := (product.pair_membership at k, j, halfRange(hp), halfRange(hq)).forward with inProduct rw [fst.pair, snd.pair] at below exact ⟨components.left, components.right, below⟩ . intro all obtain ⟨hk, hj, below⟩ := all have inProduct := product.pair with hk, hj have shaped : mul(p, snd((k, j))) < mul(q, fst((k, j))) rw [fst.pair, snd.pair] exact below exact lowerLattice.membership.backward with ⟨inProduct, shaped⟩ theorem upperLattice.mem_iff (p q hp hq k j : Nat) shows (k, j) ∈ upperLattice(p, q, hp, hq) ↔ k ∈ halfRange(hp) ∧ j ∈ halfRange(hq) ∧ q * k < p * j proof constructor . intro member obtain ⟨inProduct, below⟩ := upperLattice.membership.forward with member have components := (product.pair_membership at k, j, halfRange(hp), halfRange(hq)).forward with inProduct rw [fst.pair, snd.pair] at below exact ⟨components.left, components.right, below⟩ . intro all obtain ⟨hk, hj, below⟩ := all have inProduct := product.pair with hk, hj have shaped : mul(q, fst((k, j))) < mul(p, snd((k, j))) rw [fst.pair, snd.pair] exact below exact upperLattice.membership.backward with ⟨inProduct, shaped⟩ theorem lowerLattice.subset (p q hp hq : Nat) shows lowerLattice(p, q, hp, hq) ⊆ halfRange(hp) × halfRange(hq) proof intros r member have parts := lowerLattice.membership.forward with member exact parts.left theorem upperLattice.subset (p q hp hq : Nat) shows upperLattice(p, q, hp, hq) ⊆ halfRange(hp) × halfRange(hq) proof intros r member have parts := upperLattice.membership.forward with member exact parts.left -- The line misses the lattice, and the two sides partition it -- No lattice point of the rectangle lies on the line: p j = q k gives -- p ∣ k with 0 < k ≤ hp < p. theorem lattice.off_line (p q hp hq k j : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 requires kIn : k ∈ halfRange(hp) requires jIn : j ∈ halfRange(hq) shows p * j ≠ q * k proof intro equal obtain ⟨kPositive, kBound⟩ := (halfRange.mem_iff at hp, k).forward with kIn have divides : p ∣ q * k rw ← equal exact divides.mul_left_self at p, j have split := Prime.divides_mul with primeP, divides cases split with dividesQ | dividesK . have same := Prime.divides_prime with primeP, primeQ, dividesQ exact distinct with same . have kNonzero : k ≠ 0 by arith have le := divides.le with dividesK, kNonzero have impossible : ¬p ≤ k by arith exact impossible with le theorem lattice.partition (p q hp hq : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 shows lowerLattice(p, q, hp, hq) ∪ upperLattice(p, q, hp, hq) = halfRange(hp) × halfRange(hq) ∧ Disjoint(lowerLattice(p, q, hp, hq), upperLattice(p, q, hp, hq)) proof constructor . ext r constructor . intro member have split := union2.membership.forward with member cases split with inLower | inUpper . exact lowerLattice.subset at p, q, hp, hq with r, inLower . exact upperLattice.subset at p, q, hp, hq with r, inUpper . intro member obtain ⟨bound, k, hk, j, hj, shape⟩ := product.membership.forward with member have kNat := halfRange.member_nat with hk have jNat := halfRange.member_nat with hj have offLine := lattice.off_line with primeP, primeQ, distinct, oddP, oddQ, hk, hj have trichotomy := Less.trichotomy at p * j, q * k with * apply union2.membership.backward rw shape rcases trichotomy with below | same | above . left exact (lowerLattice.mem_iff at p, q, hp, hq, k, j).backward with ⟨hk, hj, below⟩ . exfalso exact offLine with same . right exact (upperLattice.mem_iff at p, q, hp, hq, k, j).backward with ⟨hk, hj, above⟩ . apply (Disjoint.iff at lowerLattice(p, q, hp, hq), upperLattice(p, q, hp, hq)).backward intros r inLower inUpper have inProduct := lowerLattice.subset at p, q, hp, hq with r, inLower obtain ⟨bound, k, hk, j, hj, shape⟩ := product.membership.forward with inProduct have kNat := halfRange.member_nat with hk have jNat := halfRange.member_nat with hj rw shape at inLower rw shape at inUpper have lowerAll := (lowerLattice.mem_iff at p, q, hp, hq, k, j).forward with inLower have upperAll := (upperLattice.mem_iff at p, q, hp, hq, k, j).forward with inUpper have below := lowerAll.right.right have above := upperAll.right.right have impossible : ¬q * k < p * j by arith exact impossible with above -- Floors -- For 0 < k ≤ hp the floor ⌊q k / p⌋ is at most hq: q k / p < q / 2. theorem floorWeights.le_half (p q hp hq k : Nat) requires primeP : Prime(p) requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 requires kIn : k ∈ halfRange(hp) shows quotientNat(q * k, p) ≤ hq proof have nonzero := Prime.nonzero with primeP obtain ⟨kPositive, kBound⟩ := (halfRange.mem_iff at hp, k).forward with kIn have cases := Less.or_LessEqual at hq, quotientNat(q * k, p) cases cases with above | small . exfalso have step : hq + 1 ≤ quotientNat(q * k, p) by arith have scaled := (quotientNat.le_iff at p, q * k, hq + 1 with nonzero).forward with step have bounded := LessEqual.mul_left at k, hp, q with kBound have expandP : p * (hq + 1) = 2 * (hp * hq) + 2 * hp + hq + 1 rw oddP ring have expandQ : q * hp = 2 * (hp * hq) + hp rw oddQ ring rw expandP at scaled rw expandQ at bounded arith . exact small -- The floor sum counts the lower points -- Shift the weighted pairs (k, i), i < ⌊q k / p⌋, to the lattice points -- (k, i + 1). defgraph latticeShift (p q hp hq : Nat) (r ∈ weightedPairs(halfRange(hp), floorWeights(q, p, hp))) : lowerLattice(p, q, hp, hq) := (fst(r), succ(snd(r))) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 proof have parts := weightedPairs.membership.forward with member obtain ⟨k, hk, i, hi, shape⟩ := parts.right rw [shape, fst.pair, snd.pair] have kNat := halfRange.member_nat with hk rw floorWeights.value with hk at hi have iNat := natural.member at quotientNat(q * k, p) with hi have iBelow := Less.of_membership at i, quotientNat(q * k, p) with hi have step : succ(i) ≤ quotientNat(q * k, p) by arith have nonzero := Prime.nonzero with primeP have weak := (quotientNat.le_iff at p, q * k, succ(i) with nonzero).forward with step have half := floorWeights.le_half with primeP, oddP, oddQ, hk have jPositive : 0 < succ(i) by arith have jBound : succ(i) ≤ hq by arith have hj := (halfRange.mem_iff at hq, succ(i)).backward with ⟨jPositive, jBound⟩ have offLine := lattice.off_line with primeP, primeQ, distinct, oddP, oddQ, hk, hj have strict : p * succ(i) < q * k by arith exact (lowerLattice.mem_iff at p, q, hp, hq, k, succ(i)).backward with ⟨hk, hj, strict⟩ theorem latticeShift.pair (p q hp hq k i : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 requires member : (k, i) ∈ weightedPairs(halfRange(hp), floorWeights(q, p, hp)) shows latticeShift(p, q, hp, hq)((k, i)) = (k, succ(i)) proof have value := latticeShift.value with primeP, primeQ, distinct, oddP, oddQ, member rw [value, fst.pair, snd.pair] rfl theorem latticeShift.bijective (p q hp hq : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 shows BijectiveBetween(latticeShift(p, q, hp, hq), weightedPairs(halfRange(hp), floorWeights(q, p, hp)), lowerLattice(p, q, hp, hq)) proof have nonzero := Prime.nonzero with primeP have function := latticeShift.function with primeP, primeQ, distinct, oddP, oddQ constructor . constructor . exact function . intros x hx y hy z hz hxz hyz have xValue := FunctionBetween.apply_value with function, hx, hxz have yValue := FunctionBetween.apply_value with function, hy, hyz have xParts := weightedPairs.membership.forward with hx obtain ⟨k, hk, i, hi, xShape⟩ := xParts.right have yParts := weightedPairs.membership.forward with hy obtain ⟨l, hl, m, hm, yShape⟩ := yParts.right have kNat := halfRange.member_nat with hk have lNat := halfRange.member_nat with hl rw floorWeights.value with hk at hi rw floorWeights.value with hl at hm have iNat := natural.member at quotientNat(q * k, p) with hi have mNat := natural.member at quotientNat(q * l, p) with hm rw xShape at hx rw yShape at hy have xPair := latticeShift.pair with primeP, primeQ, distinct, oddP, oddQ, hx have yPair := latticeShift.pair with primeP, primeQ, distinct, oddP, oddQ, hy rw [xShape, xPair] at xValue rw [yShape, yPair] at yValue have same : (k, succ(i)) = (l, succ(m)) rw [xValue, yValue] rfl have coordinates := orderedPair.injective with same have seconds := Naturals.injective.succ with coordinates.right rw [xShape, yShape, coordinates.left, seconds] rfl . constructor . exact function . intros z hz have inProduct := lowerLattice.subset at p, q, hp, hq with z, hz obtain ⟨bound, k, hk, j, hj, shape⟩ := product.membership.forward with inProduct rw shape at hz have kNat := halfRange.member_nat with hk have jNat := halfRange.member_nat with hj obtain ⟨hkAgain, hjAgain, below⟩ := (lowerLattice.mem_iff at p, q, hp, hq, k, j).forward with hz obtain ⟨jPositive, jBound⟩ := (halfRange.mem_iff at hq, j).forward with hj have form := natural.zero_or_successor at j cases form with zero | successor . exfalso rw zero at jPositive exact Less.irreflexive at 0 with *, jPositive . obtain ⟨i, iNat, jForm⟩ := successor have weak : p * succ(i) ≤ q * k rw ← jForm arith have characterization := quotientNat.le_iff at p, q * k, succ(i) with nonzero have step := characterization.backward with weak have iBelow : i < quotientNat(q * k, p) by arith have iIn := membership.of_Less at i, quotientNat(q * k, p) with *, *, iBelow have weight : i ∈ floorWeights(q, p, hp)(k) rw floorWeights.value with hk exact iIn have iNatural : i ∈ ℕ by exact nat.omega.forward with iNat have pairIn := product.pair with hk, iNatural have same : (k, i) = (k, i) by rfl have memberX := weightedPairs.membership.backward with ⟨pairIn, k, hk, i, weight, same⟩ have edge := FunctionBetween.relates_apply with function, memberX have value := latticeShift.pair with primeP, primeQ, distinct, oddP, oddQ, memberX rw [value, ← jForm] at edge refine ⟨(k, i), memberX, _⟩ rw shape exact edge -- j ≤ ⌊q k / p⌋ exactly when p j ≤ q k, and off the line p j < q k; so -- the weighted pairs of the floor sum are the lower lattice points, -- after shifting i < ⌊q k / p⌋ to j = i + 1. theorem lowerLattice.count (p q hp hq : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 shows natSum(halfRange(hp), floorWeights(q, p, hp)) = cardinality(lowerLattice(p, q, hp, hq)) proof have finite := weightedPairs.finite with (halfRange.finite at hp), (halfRange.cardinality at hp), (floorWeights.natural at q, p, hp) have bijective := latticeShift.bijective with primeP, primeQ, distinct, oddP, oddQ have same := cardinality.bijective with finite, bijective unfold natSum exact same -- The counts -- The upper points are the lower points of the roles exchanged, under the -- swap (k, j) ↦ (j, k). theorem upperLattice.count (p q hp hq : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 shows natSum(halfRange(hq), floorWeights(p, q, hq)) = cardinality(upperLattice(p, q, hp, hq)) proof have distinctQ : q ≠ p intro same apply distinct symm exact same have lower := lowerLattice.count at q, p, hq, hp with primeQ, primeP, distinctQ, oddQ, oddP rw lower have swapBijective := productSwap.bijective at halfRange(hq), halfRange(hp) have injective := swapBijective.left have subset := lowerLattice.subset at q, p, hq, hp have finiteProduct := Finite.product with (halfRange.finite at hq), (halfRange.finite at hp) have finite := Finite.subset with finiteProduct, subset have count := cardinality.injective_image with injective, subset, finite have image : functionImage(productSwap(halfRange(hq), halfRange(hp)), lowerLattice(q, p, hq, hp), halfRange(hp) × halfRange(hq)) = upperLattice(p, q, hp, hq) ext z constructor . intro member obtain ⟨inProduct, r, hr, value⟩ := functionImage.membership.forward with member have rProduct := subset with r, hr obtain ⟨bound, j, hj, k, hk, shape⟩ := product.membership.forward with rProduct rw shape at hr have jNat := halfRange.member_nat with hj have kNat := halfRange.member_nat with hk obtain ⟨hjAgain, hkAgain, below⟩ := (lowerLattice.mem_iff at q, p, hq, hp, j, k).forward with hr have swapped := productSwap.pair with hj, hk rw [shape, swapped] at value rw value exact (upperLattice.mem_iff at p, q, hp, hq, k, j).backward with ⟨hk, hj, below⟩ . intro member have inProduct := upperLattice.subset at p, q, hp, hq with z, member obtain ⟨bound, k, hk, j, hj, shape⟩ := product.membership.forward with inProduct rw shape at member have kNat := halfRange.member_nat with hk have jNat := halfRange.member_nat with hj obtain ⟨hkAgain, hjAgain, above⟩ := (upperLattice.mem_iff at p, q, hp, hq, k, j).forward with member have inLower := (lowerLattice.mem_iff at q, p, hq, hp, j, k).backward with ⟨hj, hk, above⟩ have swapped := productSwap.pair with hj, hk have equal : (k, j) = productSwap(halfRange(hq), halfRange(hp))((j, k)) rw swapped rfl have pairIn := product.pair with hk, hj have both : (k, j) ∈ halfRange(hp) × halfRange(hq) ∧ (∃ x ∈ lowerLattice(q, p, hq, hp), (k, j) = productSwap(halfRange(hq), halfRange(hp))(x)) constructor . exact pairIn . refine ⟨(j, k), inLower, equal⟩ rw shape exact functionImage.membership.backward with both rw image at count symm exact count theorem lattice.count (p q hp hq : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 shows add(natSum(halfRange(hp), floorWeights(q, p, hp)), natSum(halfRange(hq), floorWeights(p, q, hq))) = hp * hq proof have lower := lowerLattice.count with primeP, primeQ, distinct, oddP, oddQ have upper := upperLattice.count with primeP, primeQ, distinct, oddP, oddQ rw [lower, upper] obtain ⟨cover, disjoint⟩ := lattice.partition with primeP, primeQ, distinct, oddP, oddQ have finiteProduct := Finite.product with (halfRange.finite at hp), (halfRange.finite at hq) have lowerFinite := Finite.subset with finiteProduct, (lowerLattice.subset at p, q, hp, hq) have upperFinite := Finite.subset with finiteProduct, (upperLattice.subset at p, q, hp, hq) have union := cardinality.union_disjoint with lowerFinite, upperFinite, disjoint rw cover at union rw ← union have countP := cardinality.correct with (halfRange.finite at hp) have countQ := cardinality.correct with (halfRange.finite at hq) have product := HasCardinality.product with countP, countQ have value := cardinality.eq_of_has with product rw [value, halfRange.cardinality, halfRange.cardinality] rfl