SegmentoII.lean Β· SHA-256 1284bf149ad03ed41dc15cc2bded04ba79a4a789edf17655e45f4b100d0f7a65 (of the text shown) Β· blueprint
Comments edited for publication; the code is unchanged.
| 1 | /- SegmentoII.lean β digit recursion for weighted sums of a completely multiplicative, |
| 2 | q-periodic-off-multiples function, zeros from base-q digits, and the complete-sequence |
| 3 | (subset-sum) lemma with its pairs version. |
| 4 | Author: Carles MarΓn <karlesmarin@gmail.com> |
| 5 | |
| 6 | Formalizes three results of paper II (orbit-pair, segmento II), over β€-valued Ο : β β β€ with |
| 7 | (mult) Ο (a*b) = Ο a * Ο b, (per) Ο (j*q + r) = Ο r for 1 β€ r < q, (s) Ο q = s, |
| 8 | (sum0) β_{n=1}^{q-1} Ο n = 0. |
| 9 | * `digit_recursion` : S (q*k + r) = s*q*S k + k*(Tq + q*C r) + T r (r < q) |
| 10 | * `zero_step`, `zeros_from_digits` : zeros of S propagate along base-q digits in R. |
| 11 | * `complete_list`, `complete_range`, `pairs_subset_sum` : complete-sequence lemma. |
| 12 | |
| 13 | Fast-loop build (specific imports): |
| 14 | lake env lean SegmentoII.lean (inside a Lean 4 + Mathlib project) -/ |
| 15 | import Mathlib.Algebra.BigOperators.Intervals |
| 16 | import Mathlib.Algebra.BigOperators.Group.Finset.Basic |
| 17 | import Mathlib.Algebra.BigOperators.Group.Finset.Lemmas |
| 18 | import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise |
| 19 | import Mathlib.Algebra.BigOperators.Ring.Finset |
| 20 | import Mathlib.Algebra.Group.Nat.Even |
| 21 | import Mathlib.Data.Nat.Digits.Defs |
| 22 | import Mathlib.Data.List.Induction |
| 23 | import Mathlib.Tactic.Ring |
| 24 | import Mathlib.Tactic.LinearCombination |
| 25 | |
| 26 | open Finset |
| 27 | |
| 28 | namespace SegmentoII |
| 29 | |
| 30 | /-- `S m = β_{n=1}^{m} n Ο(n)`. -/ |
| 31 | def S (Ο : β β β€) (m : β) : β€ := β n β Icc 1 m, (n : β€) * Ο n |
| 32 | |
| 33 | /-- `C r = β_{n=1}^{r} Ο(n)`. -/ |
| 34 | def C (Ο : β β β€) (r : β) : β€ := β n β Icc 1 r, Ο n |
| 35 | |
| 36 | /-- `T r = β_{n=1}^{r} n Ο(n)` (same as `S`; used for `r < q`). -/ |
| 37 | def T (Ο : β β β€) (r : β) : β€ := β n β Icc 1 r, (n : β€) * Ο n |
| 38 | |
| 39 | /-- `Tq = T (q-1)`. -/ |
| 40 | def Tq (Ο : β β β€) (q : β) : β€ := T Ο (q - 1) |
| 41 | |
| 42 | lemma S_zero (Ο : β β β€) : S Ο 0 = 0 := by simp [S] |
| 43 | lemma T_zero (Ο : β β β€) : T Ο 0 = 0 := by simp [T] |
| 44 | lemma C_zero (Ο : β β β€) : C Ο 0 = 0 := by simp [C] |
| 45 | |
| 46 | lemma S_succ (Ο : β β β€) (n : β) : S Ο (n + 1) = S Ο n + ((n : β€) + 1) * Ο (n + 1) := by |
| 47 | unfold S; rw [Finset.sum_Icc_succ_top (by omega)]; push_cast; ring |
| 48 | |
| 49 | lemma T_succ (Ο : β β β€) (n : β) : T Ο (n + 1) = T Ο n + ((n : β€) + 1) * Ο (n + 1) := |
| 50 | S_succ Ο n |
| 51 | |
| 52 | lemma C_succ (Ο : β β β€) (n : β) : C Ο (n + 1) = C Ο n + Ο (n + 1) := by |
| 53 | unfold C; rw [Finset.sum_Icc_succ_top (by omega)] |
| 54 | |
| 55 | /-- Inner step: `r β¦ r+1` inside a block (uses periodicity). -/ |
| 56 | private lemma step_r (q : β) (Ο : β β β€) (s : β€) |
| 57 | (hper : β j r, 1 β€ r β r < q β Ο (j * q + r) = Ο r) (k r : β) (hr : r + 1 < q) |
| 58 | (ih : S Ο (q * k + r) = s * q * S Ο k + k * (Tq Ο q + q * C Ο r) + T Ο r) : |
| 59 | S Ο (q * k + (r + 1)) = s * q * S Ο k + k * (Tq Ο q + q * C Ο (r + 1)) + T Ο (r + 1) := by |
| 60 | have hΟ : Ο (q * k + r + 1) = Ο (r + 1) := by |
| 61 | rw [mul_comm q k, add_assoc]; exact hper k (r + 1) (by omega) hr |
| 62 | have e : q * k + (r + 1) = (q * k + r) + 1 := by omega |
| 63 | rw [e, S_succ, C_succ, T_succ, hΟ, ih] |
| 64 | push_cast; ring |
| 65 | |
| 66 | /-- Block step: `(k, q-1) β¦ (k+1, 0)` (uses multiplicativity, `Ο q = s`, `sum0`). -/ |
| 67 | private lemma step_k (q : β) (hq : 1 β€ q) (Ο : β β β€) (s : β€) |
| 68 | (hmul : β a b, Ο (a * b) = Ο a * Ο b) (hs : Ο q = s) (hsum0 : C Ο (q - 1) = 0) (k : β) |
| 69 | (ih : S Ο (q * k + (q - 1)) = s * q * S Ο k + k * (Tq Ο q + q * C Ο (q - 1)) + T Ο (q - 1)) : |
| 70 | S Ο (q * (k + 1) + 0) = s * q * S Ο (k + 1) + ((k + 1 : β) : β€) * (Tq Ο q + q * C Ο 0) |
| 71 | + T Ο 0 := by |
| 72 | have e : q * (k + 1) + 0 = (q * k + (q - 1)) + 1 := by |
| 73 | rw [Nat.mul_succ]; omega |
| 74 | have hΟ : Ο (q * k + (q - 1) + 1) = s * Ο (k + 1) := by |
| 75 | rw [β e, add_zero, hmul, hs] |
| 76 | have hcast : ((q * k + (q - 1) : β) : β€) + 1 = (q : β€) * ((k : β€) + 1) := by |
| 77 | have h2 : q * k + (q - 1) + 1 = q * (k + 1) := by rw [β e, add_zero] |
| 78 | exact_mod_cast h2 |
| 79 | have hTq : Tq Ο q = T Ο (q - 1) := rfl |
| 80 | rw [e, S_succ, hΟ, ih, hcast, S_succ, C_zero, T_zero, hsum0, hTq] |
| 81 | push_cast; ring |
| 82 | |
| 83 | /-- **Theorem 1 (digit recursion).** For `r < q`: |
| 84 | `S (q*k + r) = s*q*S k + k*(Tq + q*C r) + T r`. -/ |
| 85 | theorem digit_recursion (q : β) (hq : 2 β€ q) (Ο : β β β€) (s : β€) |
| 86 | (hmul : β a b, Ο (a * b) = Ο a * Ο b) |
| 87 | (hper : β j r, 1 β€ r β r < q β Ο (j * q + r) = Ο r) |
| 88 | (hs : Ο q = s) (hsum0 : C Ο (q - 1) = 0) (k r : β) (hr : r < q) : |
| 89 | S Ο (q * k + r) = s * q * S Ο k + k * (Tq Ο q + q * C Ο r) + T Ο r := by |
| 90 | -- from `P k 0` get `P k r` for every `r < q` |
| 91 | have fill : β k, (S Ο (q * k + 0) = s * q * S Ο k + k * (Tq Ο q + q * C Ο 0) + T Ο 0) β |
| 92 | β r, r < q β S Ο (q * k + r) = s * q * S Ο k + k * (Tq Ο q + q * C Ο r) + T Ο r := by |
| 93 | intro k h0 r |
| 94 | induction r with |
| 95 | | zero => intro _; exact h0 |
| 96 | | succ r ihr => intro hr; exact step_r q Ο s hper k r hr (ihr (by omega)) |
| 97 | revert r |
| 98 | induction k with |
| 99 | | zero => |
| 100 | apply fill 0 |
| 101 | simp [S_zero, T_zero, C_zero] |
| 102 | | succ k ih => |
| 103 | apply fill (k + 1) |
| 104 | exact step_k q (by omega) Ο s hmul hs hsum0 k (ih (q - 1) (by omega)) |
| 105 | |
| 106 | /-- The digit set `R = {r | r < q β§ Tq + q*C r = 0 β§ T r = 0}`. -/ |
| 107 | def InR (Ο : β β β€) (q r : β) : Prop := r < q β§ Tq Ο q + (q : β€) * C Ο r = 0 β§ T Ο r = 0 |
| 108 | |
| 109 | /-- **Theorem 2 (zeros from digits), one step.** -/ |
| 110 | theorem zero_step (q : β) (hq : 2 β€ q) (Ο : β β β€) (s : β€) |
| 111 | (hmul : β a b, Ο (a * b) = Ο a * Ο b) |
| 112 | (hper : β j r, 1 β€ r β r < q β Ο (j * q + r) = Ο r) |
| 113 | (hs : Ο q = s) (hsum0 : C Ο (q - 1) = 0) (k r : β) |
| 114 | (hk : S Ο k = 0) (hR : InR Ο q r) : S Ο (q * k + r) = 0 := by |
| 115 | obtain β¨hr, h1, h2β© := hR |
| 116 | rw [digit_recursion q hq Ο s hmul hper hs hsum0 k r hr, hk, h1, h2] |
| 117 | ring |
| 118 | |
| 119 | /-- **Theorem 2 (zeros from digits), corollary.** If every base-`q` digit of `m` lies in `R`, |
| 120 | then `S m = 0`. -/ |
| 121 | theorem zeros_from_digits (q : β) (hq : 2 β€ q) (Ο : β β β€) (s : β€) |
| 122 | (hmul : β a b, Ο (a * b) = Ο a * Ο b) |
| 123 | (hper : β j r, 1 β€ r β r < q β Ο (j * q + r) = Ο r) |
| 124 | (hs : Ο q = s) (hsum0 : C Ο (q - 1) = 0) (m : β) |
| 125 | (hm : β d β Nat.digits q m, InR Ο q d) : S Ο m = 0 := by |
| 126 | induction m using Nat.strong_induction_on with |
| 127 | | _ m ih => |
| 128 | rcases Nat.eq_zero_or_pos m with rfl | hpos |
| 129 | Β· exact S_zero Ο |
| 130 | Β· rw [Nat.digits_def' (by omega) hpos] at hm |
| 131 | have hlt : m / q < m := Nat.div_lt_self hpos (by omega) |
| 132 | have h0 := ih (m / q) hlt (fun d hd => hm d (List.mem_cons_of_mem _ hd)) |
| 133 | have hR := hm (m % q) (List.mem_cons_self ..) |
| 134 | have := zero_step q hq Ο s hmul hper hs hsum0 (m / q) (m % q) h0 hR |
| 135 | rwa [Nat.div_add_mod] at this |
| 136 | |
| 137 | /-- **Theorem 3 (complete sequence), list form.** If each entry is at most `1 +` the sum of the |
| 138 | earlier entries, every `x β€ d.sum` is the sum of a sublist of `d`. |
| 139 | (The hypothesis "every entry β₯ 1" is not needed and has been dropped.) -/ |
| 140 | theorem complete_list (d : List β) |
| 141 | (hd : β lβ a lβ, d = lβ ++ a :: lβ β a β€ 1 + lβ.sum) : |
| 142 | β x β€ d.sum, β l : List β, l.Sublist d β§ l.sum = x := by |
| 143 | induction d using List.reverseRecOn with |
| 144 | | nil => |
| 145 | intro x hx |
| 146 | exact β¨[], List.Sublist.slnil, by simp at hx β’; omegaβ© |
| 147 | | append_singleton l a ih => |
| 148 | have hl : β lβ b lβ, l = lβ ++ b :: lβ β b β€ 1 + lβ.sum := |
| 149 | fun lβ b lβ h => hd lβ b (lβ ++ [a]) (by rw [h]; simp) |
| 150 | have ha : a β€ 1 + l.sum := hd l a [] rfl |
| 151 | intro x hx |
| 152 | simp only [List.sum_append, List.sum_cons, List.sum_nil, add_zero] at hx |
| 153 | by_cases hx' : x β€ l.sum |
| 154 | Β· obtain β¨t, ht, hsumβ© := ih hl x hx' |
| 155 | exact β¨t, ht.trans (List.sublist_append_left l [a]), hsumβ© |
| 156 | Β· obtain β¨t, ht, hsumβ© := ih hl (x - a) (by omega) |
| 157 | refine β¨t ++ [a], ht.append (List.Sublist.refl _), ?_β© |
| 158 | simp only [List.sum_append, List.sum_cons, List.sum_nil, add_zero, hsum] |
| 159 | omega |
| 160 | |
| 161 | /-- **Theorem 3 (complete sequence), indexed form** (`d j`, `j < t`, subsets of `range t`). -/ |
| 162 | theorem complete_range (d : β β β) (t : β) |
| 163 | (hd : β j < t, d j β€ 1 + β i β range j, d i) : |
| 164 | β y β€ β i β range t, d i, β A β range t, β i β A, d i = y := by |
| 165 | induction t with |
| 166 | | zero => |
| 167 | intro y hy |
| 168 | exact β¨β , empty_subset _, by simp at hy β’; omegaβ© |
| 169 | | succ t ih => |
| 170 | have hd' : β j < t, d j β€ 1 + β i β range j, d i := fun j hj => hd j (by omega) |
| 171 | have ht := hd t (by omega) |
| 172 | intro y hy |
| 173 | rw [sum_range_succ] at hy |
| 174 | by_cases hy' : y β€ β i β range t, d i |
| 175 | Β· obtain β¨A, hA, hsumβ© := ih hd' y hy' |
| 176 | exact β¨A, hA.trans (range_subset_range.mpr (by omega)), hsumβ© |
| 177 | Β· obtain β¨A, hA, hsumβ© := ih hd' (y - d t) (by omega) |
| 178 | have hnot : t β A := fun h => by simpa using hA h |
| 179 | refine β¨insert t A, ?_, ?_β© |
| 180 | Β· intro i hi |
| 181 | rcases mem_insert.mp hi with rfl | hi |
| 182 | Β· simp |
| 183 | Β· exact range_subset_range.mpr (by omega) (hA hi) |
| 184 | Β· rw [sum_insert hnot, hsum]; omega |
| 185 | |
| 186 | /-- Choosing `v j` on `A` and `u j` off `A` adds `2 * β_A d` to `U = β u`. -/ |
| 187 | lemma choice_sum (t : β) (d u v : β β β) (hv : β j, v j = u j + 2 * d j) |
| 188 | (A : Finset β) (hA : A β range t) : |
| 189 | β j β range t, (if j β A then v j else u j) |
| 190 | = β j β range t, u j + 2 * β j β A, d j := by |
| 191 | have : β j, (if j β A then v j else u j) = u j + (if j β A then 2 * d j else 0) := by |
| 192 | intro j; split_ifs <;> simp [hv] |
| 193 | simp only [this, sum_add_distrib, sum_ite_mem, inter_eq_right.mpr hA, mul_sum] |
| 194 | |
| 195 | /-- **Theorem 3, pairs version.** Pairs `(u j, v j)` with `v j = u j + 2 d j`, `d` complete, |
| 196 | `q0` odd, `U = β u`, `H = β d`. Every `x β [U + q0 - 1, U + 2H + 1]` is a sum of a selection: |
| 197 | optionally `q0`, plus one element from each pair (`v j` for `j β A`, `u j` otherwise). |
| 198 | (For distinct `q0, u j, v j` this is a subset sum of `{q0, u j, v j}`; the hypothesis |
| 199 | `q0 β€ 2H + 1` is not needed and has been dropped.) -/ |
| 200 | theorem pairs_subset_sum (t : β) (d u v : β β β) (q0 : β) (hq0 : Odd q0) |
| 201 | (hv : β j, v j = u j + 2 * d j) |
| 202 | (hd : β j < t, d j β€ 1 + β i β range j, d i) (x : β) |
| 203 | (hx1 : β j β range t, u j + q0 - 1 β€ x) |
| 204 | (hx2 : x β€ β j β range t, u j + 2 * β j β range t, d j + 1) : |
| 205 | β b : Bool, β A β range t, |
| 206 | x = (if b then q0 else 0) + β j β range t, (if j β A then v j else u j) := by |
| 207 | set U := β j β range t, u j with hU |
| 208 | set H := β j β range t, d j with hH |
| 209 | obtain β¨c, hcβ© := hq0 |
| 210 | have hUx : U β€ x := by omega |
| 211 | rcases Nat.even_or_odd (x - U) with β¨y, hyβ© | β¨y, hyβ© |
| 212 | Β· -- x = U + 2y, y β€ H |
| 213 | obtain β¨A, hA, hsumβ© := complete_range d t hd y (by omega) |
| 214 | refine β¨false, A, hA, ?_β© |
| 215 | rw [choice_sum t d u v hv A hA, hsum]; simp; omega |
| 216 | Β· -- x = U + q0 + 2y', y' β€ H |
| 217 | obtain β¨A, hA, hsumβ© := complete_range d t hd (y - c) (by omega) |
| 218 | refine β¨true, A, hA, ?_β© |
| 219 | rw [choice_sum t d u v hv A hA, hsum]; simp; omega |
| 220 | |
| 221 | end SegmentoII |
| 222 | |
| 223 | #print axioms SegmentoII.digit_recursion |
| 224 | #print axioms SegmentoII.zero_step |
| 225 | #print axioms SegmentoII.zeros_from_digits |
| 226 | #print axioms SegmentoII.complete_list |
| 227 | #print axioms SegmentoII.complete_range |
| 228 | #print axioms SegmentoII.pairs_subset_sum |