| | @@ -0,0 +1,158 @@ |
| 1 | +import Mathlib.Tactic |
| 2 | + |
| 3 | +/-! |
| 4 | +# Erdős #588: counting the good ordered pairs modulo `n` |
| 5 | + |
| 6 | +A counting lemma with no geometry, for the sharp bound `f₃(n) ≥ (n² - 3n)/6`. In the planned |
| 7 | +configuration the points are indexed by `j < n`, and three different indices `i, j, l` are |
| 8 | +collinear exactly when `n ∣ i + j + l + 1`. |
| 9 | + |
| 10 | +For `i, j < n` there is exactly one `l < n` with `n ∣ i + j + l + 1`, namely |
| 11 | +`sylvesterThird n i j = n - 1 - (i + j) % n` (`dvd_add_iff_eq_sylvesterThird`). An ordered pair |
| 12 | +`(i, j)` is *good* when `i ≠ j` and this `l` differs from both. A pair is bad only if `i = j`, or |
| 13 | +`l = i` (then `j` is determined by `i`), or `l = j` (then `i` is determined by `j`): at most `3n` |
| 14 | +pairs. Hence at least `n² - 3n` ordered pairs are good (`sq_sub_le_card_goodPairs`, |
| 15 | +`sq_le_card_goodPairs_add`). |
| 16 | +-/ |
| 17 | + |
| 18 | +open scoped Finset |
| 19 | + |
| 20 | +namespace ErdosSwarm.Erdos588 |
| 21 | + |
| 22 | +/-- The index of the third point on the line through points `i` and `j`: the `l < n` with |
| 23 | +`n ∣ i + j + l + 1`. -/ |
| 24 | +def sylvesterThird (n i j : ℕ) : ℕ := n - 1 - (i + j) % n |
| 25 | + |
| 26 | +theorem sylvesterThird_lt {n : ℕ} (hn : 0 < n) (i j : ℕ) : sylvesterThird n i j < n := by |
| 27 | + unfold sylvesterThird |
| 28 | + omega |
| 29 | + |
| 30 | +theorem sylvesterThird_comm (n i j : ℕ) : sylvesterThird n i j = sylvesterThird n j i := by |
| 31 | + unfold sylvesterThird |
| 32 | + rw [add_comm] |
| 33 | + |
| 34 | +private theorem eq_of_dvd_of_lt_two_mul {n m : ℕ} (h : n ∣ m) (h0 : 0 < m) (h2 : m < 2 * n) : |
| 35 | + m = n := by |
| 36 | + obtain ⟨c, rfl⟩ := h |
| 37 | + have hc1 : c < 2 := by |
| 38 | + by_contra hc |
| 39 | + rw [not_lt] at hc |
| 40 | + nlinarith |
| 41 | + have hc0 : c ≠ 0 := by |
| 42 | + rintro rfl |
| 43 | + simp at h0 |
| 44 | + have : c = 1 := by omega |
| 45 | + rw [this, mul_one] |
| 46 | + |
| 47 | +/-- For `l < n`: `n ∣ i + j + l + 1` exactly when `l` is `sylvesterThird n i j`. -/ |
| 48 | +theorem dvd_add_iff_eq_sylvesterThird {n : ℕ} (i j : ℕ) {l : ℕ} (hl : l < n) : |
| 49 | + n ∣ i + j + l + 1 ↔ l = sylvesterThird n i j := by |
| 50 | + have hn : 0 < n := by omega |
| 51 | + have hmod := Nat.mod_lt (i + j) hn |
| 52 | + have hdiv := Nat.div_add_mod (i + j) n |
| 53 | + have e : i + j + l + 1 = n * ((i + j) / n) + ((i + j) % n + l + 1) := by omega |
| 54 | + rw [e, Nat.dvd_add_right (Dvd.intro _ rfl)] |
| 55 | + unfold sylvesterThird |
| 56 | + constructor |
| 57 | + · intro h |
| 58 | + have := eq_of_dvd_of_lt_two_mul h (by omega) (by omega) |
| 59 | + omega |
| 60 | + · intro h |
| 61 | + have : (i + j) % n + l + 1 = n := by omega |
| 62 | + rw [this] |
| 63 | + |
| 64 | +/-- The good ordered pairs: `i ≠ j`, both below `n`, with a third index `l < n` different from |
| 65 | +both such that `n ∣ i + j + l + 1`. -/ |
| 66 | +def goodPairs (n : ℕ) : Finset (ℕ × ℕ) := |
| 67 | + (Finset.range n ×ˢ Finset.range n).filter fun p : ℕ × ℕ => |
| 68 | + p.1 ≠ p.2 ∧ ∃ l ∈ Finset.range n, l ≠ p.1 ∧ l ≠ p.2 ∧ n ∣ p.1 + p.2 + l + 1 |
| 69 | + |
| 70 | +/-- Membership in `goodPairs`, with the third index named. -/ |
| 71 | +theorem mem_goodPairs {n : ℕ} {p : ℕ × ℕ} : |
| 72 | + p ∈ goodPairs n ↔ p.1 < n ∧ p.2 < n ∧ p.1 ≠ p.2 ∧ |
| 73 | + sylvesterThird n p.1 p.2 ≠ p.1 ∧ sylvesterThird n p.1 p.2 ≠ p.2 := by |
| 74 | + simp only [goodPairs, Finset.mem_filter, Finset.mem_product, Finset.mem_range] |
| 75 | + constructor |
| 76 | + · rintro ⟨⟨h1, h2⟩, hne, l, hl, hl1, hl2, hdvd⟩ |
| 77 | + rw [dvd_add_iff_eq_sylvesterThird p.1 p.2 hl] at hdvd |
| 78 | + exact ⟨h1, h2, hne, hdvd ▸ hl1, hdvd ▸ hl2⟩ |
| 79 | + · rintro ⟨h1, h2, hne, hl1, hl2⟩ |
| 80 | + have hl := sylvesterThird_lt (by omega : 0 < n) p.1 p.2 |
| 81 | + exact ⟨⟨h1, h2⟩, hne, _, hl, hl1, hl2, (dvd_add_iff_eq_sylvesterThird p.1 p.2 hl).2 rfl⟩ |
| 82 | + |
| 83 | +/-- At most `3n` of the `n²` ordered pairs are not good. -/ |
| 84 | +theorem sq_le_card_goodPairs_add (n : ℕ) : n ^ 2 ≤ #(goodPairs n) + 3 * n := by |
| 85 | + classical |
| 86 | + rcases Nat.eq_zero_or_pos n with rfl | hn |
| 87 | + · simp |
| 88 | + set U : Finset (ℕ × ℕ) := Finset.range n ×ˢ Finset.range n with hU |
| 89 | + set D : Finset (ℕ × ℕ) := U.filter fun x ↦ x.1 = x.2 with hD |
| 90 | + set B₁ : Finset (ℕ × ℕ) := U.filter fun x ↦ sylvesterThird n x.1 x.2 = x.1 with hB₁ |
| 91 | + set B₂ : Finset (ℕ × ℕ) := U.filter fun x ↦ sylvesterThird n x.1 x.2 = x.2 with hB₂ |
| 92 | + have hUmem : ∀ x ∈ U, x.1 < n ∧ x.2 < n := fun x hx ↦ by |
| 93 | + simpa [hU, Finset.mem_product] using hx |
| 94 | + have hcover : U ⊆ goodPairs n ∪ D ∪ B₁ ∪ B₂ := by |
| 95 | + intro x hx |
| 96 | + obtain ⟨h1, h2⟩ := hUmem x hx |
| 97 | + simp only [hD, hB₁, hB₂, Finset.mem_union, Finset.mem_filter, hx, true_and, mem_goodPairs] |
| 98 | + tauto |
| 99 | + have hDcard : #D ≤ n := by |
| 100 | + have := Finset.card_le_card_of_injOn (s := D) (t := Finset.range n) Prod.fst |
| 101 | + (fun x hx ↦ Finset.mem_range.2 (hUmem x (Finset.mem_filter.1 hx).1).1) ?_ |
| 102 | + · simpa using this |
| 103 | + · intro x hx y hy hxy |
| 104 | + have ex := (Finset.mem_filter.1 hx).2 |
| 105 | + have ey := (Finset.mem_filter.1 hy).2 |
| 106 | + exact Prod.ext hxy (by rw [← ex, ← ey]; exact hxy) |
| 107 | + have hmodeq : ∀ a b b' : ℕ, b < n → b' < n → (a + b) % n = (a + b') % n → b = b' := by |
| 108 | + intro a b b' hb hb' h |
| 109 | + exact Nat.ModEq.eq_of_lt_of_lt (Nat.ModEq.add_left_cancel' a h) hb hb' |
| 110 | + have hB₁card : #B₁ ≤ n := by |
| 111 | + have := Finset.card_le_card_of_injOn (s := B₁) (t := Finset.range n) Prod.fst |
| 112 | + (fun x hx ↦ Finset.mem_range.2 (hUmem x (Finset.mem_filter.1 hx).1).1) ?_ |
| 113 | + · simpa using this |
| 114 | + · intro x hx y hy hxy |
| 115 | + obtain ⟨hxU, ex⟩ := Finset.mem_filter.1 hx |
| 116 | + obtain ⟨hyU, ey⟩ := Finset.mem_filter.1 hy |
| 117 | + have hxy' : x.1 = y.1 := hxy |
| 118 | + refine Prod.ext hxy' (hmodeq x.1 x.2 y.2 (hUmem x hxU).2 (hUmem y hyU).2 ?_) |
| 119 | + have hx1 := (hUmem x hxU).1 |
| 120 | + have hm1 := Nat.mod_lt (x.1 + x.2) hn |
| 121 | + have hm2 := Nat.mod_lt (y.1 + y.2) hn |
| 122 | + unfold sylvesterThird at ex ey |
| 123 | + rw [← hxy'] at ey hm2 |
| 124 | + omega |
| 125 | + have hB₂card : #B₂ ≤ n := by |
| 126 | + have := Finset.card_le_card_of_injOn (s := B₂) (t := Finset.range n) Prod.snd |
| 127 | + (fun x hx ↦ Finset.mem_range.2 (hUmem x (Finset.mem_filter.1 hx).1).2) ?_ |
| 128 | + · simpa using this |
| 129 | + · intro x hx y hy hxy |
| 130 | + obtain ⟨hxU, ex⟩ := Finset.mem_filter.1 hx |
| 131 | + obtain ⟨hyU, ey⟩ := Finset.mem_filter.1 hy |
| 132 | + have hxy' : x.2 = y.2 := hxy |
| 133 | + refine Prod.ext (hmodeq x.2 x.1 y.1 (hUmem x hxU).1 (hUmem y hyU).1 ?_) hxy' |
| 134 | + have hx2 := (hUmem x hxU).2 |
| 135 | + have hm1 := Nat.mod_lt (x.1 + x.2) hn |
| 136 | + have hm2 := Nat.mod_lt (y.1 + y.2) hn |
| 137 | + unfold sylvesterThird at ex ey |
| 138 | + rw [← hxy'] at ey hm2 |
| 139 | + rw [add_comm x.2 x.1, add_comm x.2 y.1] |
| 140 | + omega |
| 141 | + have hUcard : #U = n ^ 2 := by |
| 142 | + rw [hU, Finset.card_product, Finset.card_range, sq] |
| 143 | + have := Finset.card_le_card hcover |
| 144 | + have u1 := Finset.card_union_le (goodPairs n ∪ D ∪ B₁) B₂ |
| 145 | + have u2 := Finset.card_union_le (goodPairs n ∪ D) B₁ |
| 146 | + have u3 := Finset.card_union_le (goodPairs n) D |
| 147 | + omega |
| 148 | + |
| 149 | +/-- **The count.** At least `n² - 3n` ordered pairs `(i, j)` of different indices below `n` have a |
| 150 | +third index `l < n`, different from both, with `n ∣ i + j + l + 1` (natural subtraction). -/ |
| 151 | +theorem sq_sub_le_card_goodPairs (n : ℕ) : |
| 152 | + n ^ 2 - 3 * n ≤ ((Finset.range n ×ˢ Finset.range n).filter fun p : ℕ × ℕ => |
| 153 | + p.1 ≠ p.2 ∧ ∃ l ∈ Finset.range n, l ≠ p.1 ∧ l ≠ p.2 ∧ n ∣ p.1 + p.2 + l + 1).card := by |
| 154 | + have := sq_le_card_goodPairs_add n |
| 155 | + unfold goodPairs at this |
| 156 | + omega |
| 157 | + |
| 158 | +end ErdosSwarm.Erdos588 |