πŸ›οΈ Atlasβœ’οΈFormalizations.NumberTheory.WeightedCharacterSums.DigitRecursion view on GitHub β†—15 theorems
/-Copyright (c) 2026 Carles MarΓ­n. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles MarΓ­n-/module public import Mathlib.Algebra.BigOperators.Intervalspublic import Mathlib.Algebra.BigOperators.Group.Finset.Basicpublic import Mathlib.Algebra.BigOperators.Group.Finset.Lemmaspublic import Mathlib.Algebra.BigOperators.Group.Finset.Piecewisepublic import Mathlib.Algebra.BigOperators.Ring.Finsetpublic import Mathlib.Algebra.Group.Nat.Evenpublic import Mathlib.Data.Nat.Digits.Defspublic import Mathlib.Data.List.Inductionpublic import Mathlib.Tactic.Ringpublic import Mathlib.Tactic.LinearCombination /-!# Digit recursion for weighted sums of a completely multiplicative function Let `Ο‡ : β„• β†’ β„€` be completely multiplicative, periodic modulo `q` on the residues `1, …, q - 1`,with `Ο‡ q = s` and `βˆ‘_{n=1}^{q-1} Ο‡ n = 0`, and let `S m = βˆ‘_{n=1}^{m} n Ο‡(n)`. Then `S` satisfiesa recursion along the base-`q` digits, so zeros of `S` propagate digit by digit. The file alsoproves the complete-sequence (subset-sum) lemma and a version for pairs. The declaration names are those cited by the paper and its blueprint (namespace `SegmentoII`). ## Main results * `SegmentoII.digit_recursion`: `S (q k + r) = s q S k + k (T_q + q C r) + T r` for `r < q`.* `SegmentoII.zero_step`, `SegmentoII.zeros_from_digits`: if every base-`q` digit of `m` lies in  the digit set `R = {r < q | T_q + q C r = 0 ∧ T r = 0}`, then `S m = 0`.* `SegmentoII.complete_list`, `SegmentoII.complete_range`: if each term is at most `1` plus the  sum of the earlier ones, every integer up to the total is a subset sum.* `SegmentoII.pairs_subset_sum`: the version with an odd extra term `qβ‚€` and pairs  `(u j, u j + 2 d j)`. ## References * C. MarΓ­n, *Zeros of weighted partial sums of completely multiplicative functions: Dirichlet  characters, the Euler factor at 2 and Conrey's sine series*, Zenodo (2026),  doi:10.5281/zenodo.22847374.-/ @[expose] public section open Finset namespace SegmentoII /-- `S m = βˆ‘_{n=1}^{m} n Ο‡(n)`. -/def S (Ο‡ : β„• β†’ β„€) (m : β„•) : β„€ := βˆ‘ n ∈ Icc 1 m, (n : β„€) * Ο‡ n /-- `C r = βˆ‘_{n=1}^{r} Ο‡(n)`. -/def C (Ο‡ : β„• β†’ β„€) (r : β„•) : β„€ := βˆ‘ n ∈ Icc 1 r, Ο‡ n /-- `T r = βˆ‘_{n=1}^{r} n Ο‡(n)` (same as `S`; used for `r < q`). -/def T (Ο‡ : β„• β†’ β„€) (r : β„•) : β„€ := βˆ‘ n ∈ Icc 1 r, (n : β„€) * Ο‡ n /-- `Tq = T (q-1)`. -/def Tq (Ο‡ : β„• β†’ β„€) (q : β„•) : β„€ := T Ο‡ (q - 1) lemma S_zero (Ο‡ : β„• β†’ β„€) : S Ο‡ 0 = 0 := by simp [S]lemma T_zero (Ο‡ : β„• β†’ β„€) : T Ο‡ 0 = 0 := by simp [T]lemma C_zero (Ο‡ : β„• β†’ β„€) : C Ο‡ 0 = 0 := by simp [C] lemma S_succ (Ο‡ : β„• β†’ β„€) (n : β„•) : S Ο‡ (n + 1) = S Ο‡ n + ((n : β„€) + 1) * Ο‡ (n + 1) := by  unfold S; rw [Finset.sum_Icc_succ_top (by omega)]; push_cast; ring lemma T_succ (Ο‡ : β„• β†’ β„€) (n : β„•) : T Ο‡ (n + 1) = T Ο‡ n + ((n : β„€) + 1) * Ο‡ (n + 1) :=  S_succ Ο‡ n lemma C_succ (Ο‡ : β„• β†’ β„€) (n : β„•) : C Ο‡ (n + 1) = C Ο‡ n + Ο‡ (n + 1) := by  unfold C; rw [Finset.sum_Icc_succ_top (by omega)] /-- Inner step: `r ↦ r+1` inside a block (uses periodicity). -/private lemma step_r (q : β„•) (Ο‡ : β„• β†’ β„€) (s : β„€)    (hper : βˆ€ j r, 1 ≀ r β†’ r < q β†’ Ο‡ (j * q + r) = Ο‡ r) (k r : β„•) (hr : r + 1 < q)    (ih : S Ο‡ (q * k + r) = s * q * S Ο‡ k + k * (Tq Ο‡ q + q * C Ο‡ r) + T Ο‡ r) :    S Ο‡ (q * k + (r + 1)) = s * q * S Ο‡ k + k * (Tq Ο‡ q + q * C Ο‡ (r + 1)) + T Ο‡ (r + 1) := by  have hΟ‡ : Ο‡ (q * k + r + 1) = Ο‡ (r + 1) := by    rw [mul_comm q k, add_assoc]; exact hper k (r + 1) (by omega) hr  have e : q * k + (r + 1) = (q * k + r) + 1 := by omega  rw [e, S_succ, C_succ, T_succ, hΟ‡, ih]  push_cast; ring /-- Block step: `(k, q-1) ↦ (k+1, 0)` (uses multiplicativity, `Ο‡ q = s`, `sum0`). -/private lemma step_k (q : β„•) (hq : 1 ≀ q) (Ο‡ : β„• β†’ β„€) (s : β„€)    (hmul : βˆ€ a b, Ο‡ (a * b) = Ο‡ a * Ο‡ b) (hs : Ο‡ q = s) (hsum0 : C Ο‡ (q - 1) = 0) (k : β„•)    (ih : S Ο‡ (q * k + (q - 1)) = s * q * S Ο‡ k + k * (Tq Ο‡ q + q * C Ο‡ (q - 1)) + T Ο‡ (q - 1)) :    S Ο‡ (q * (k + 1) + 0) = s * q * S Ο‡ (k + 1) + ((k + 1 : β„•) : β„€) * (Tq Ο‡ q + q * C Ο‡ 0)      + T Ο‡ 0 := by  have e : q * (k + 1) + 0 = (q * k + (q - 1)) + 1 := by    rw [Nat.mul_succ]; omega  have hΟ‡ : Ο‡ (q * k + (q - 1) + 1) = s * Ο‡ (k + 1) := by    rw [← e, add_zero, hmul, hs]  have hcast : ((q * k + (q - 1) : β„•) : β„€) + 1 = (q : β„€) * ((k : β„€) + 1) := by    have h2 : q * k + (q - 1) + 1 = q * (k + 1) := by rw [← e, add_zero]    exact_mod_cast h2  have hTq : Tq Ο‡ q = T Ο‡ (q - 1) := rfl  rw [e, S_succ, hΟ‡, ih, hcast, S_succ, C_zero, T_zero, hsum0, hTq]  push_cast; ring /-- **Theorem 1 (digit recursion).** For `r < q`:`S (q*k + r) = s*q*S k + k*(Tq + q*C r) + T r`. -/theorem digit_recursion (q : β„•) (hq : 2 ≀ q) (Ο‡ : β„• β†’ β„€) (s : β„€)    (hmul : βˆ€ a b, Ο‡ (a * b) = Ο‡ a * Ο‡ b)    (hper : βˆ€ j r, 1 ≀ r β†’ r < q β†’ Ο‡ (j * q + r) = Ο‡ r)    (hs : Ο‡ q = s) (hsum0 : C Ο‡ (q - 1) = 0) (k r : β„•) (hr : r < q) :    S Ο‡ (q * k + r) = s * q * S Ο‡ k + k * (Tq Ο‡ q + q * C Ο‡ r) + T Ο‡ r := by  -- from `P k 0` get `P k r` for every `r < q`  have fill : βˆ€ k, (S Ο‡ (q * k + 0) = s * q * S Ο‡ k + k * (Tq Ο‡ q + q * C Ο‡ 0) + T Ο‡ 0) β†’      βˆ€ r, r < q β†’ S Ο‡ (q * k + r) = s * q * S Ο‡ k + k * (Tq Ο‡ q + q * C Ο‡ r) + T Ο‡ r := by    intro k h0 r    induction r with    | zero => intro _; exact h0    | succ r ihr => intro hr; exact step_r q Ο‡ s hper k r hr (ihr (by omega))  revert r  induction k with  | zero =>    apply fill 0    simp [S_zero, T_zero, C_zero]  | succ k ih =>    apply fill (k + 1)    exact step_k q (by omega) Ο‡ s hmul hs hsum0 k (ih (q - 1) (by omega)) /-- The digit set `R = {r | r < q ∧ Tq + q*C r = 0 ∧ T r = 0}`. -/def InR (Ο‡ : β„• β†’ β„€) (q r : β„•) : Prop := r < q ∧ Tq Ο‡ q + (q : β„€) * C Ο‡ r = 0 ∧ T Ο‡ r = 0 /-- **Theorem 2 (zeros from digits), one step.** -/theorem zero_step (q : β„•) (hq : 2 ≀ q) (Ο‡ : β„• β†’ β„€) (s : β„€)    (hmul : βˆ€ a b, Ο‡ (a * b) = Ο‡ a * Ο‡ b)    (hper : βˆ€ j r, 1 ≀ r β†’ r < q β†’ Ο‡ (j * q + r) = Ο‡ r)    (hs : Ο‡ q = s) (hsum0 : C Ο‡ (q - 1) = 0) (k r : β„•)    (hk : S Ο‡ k = 0) (hR : InR Ο‡ q r) : S Ο‡ (q * k + r) = 0 := by  obtain ⟨hr, h1, h2⟩ := hR  rw [digit_recursion q hq Ο‡ s hmul hper hs hsum0 k r hr, hk, h1, h2]  ring /-- **Theorem 2 (zeros from digits), corollary.** If every base-`q` digit of `m` lies in `R`,then `S m = 0`. -/theorem zeros_from_digits (q : β„•) (hq : 2 ≀ q) (Ο‡ : β„• β†’ β„€) (s : β„€)    (hmul : βˆ€ a b, Ο‡ (a * b) = Ο‡ a * Ο‡ b)    (hper : βˆ€ j r, 1 ≀ r β†’ r < q β†’ Ο‡ (j * q + r) = Ο‡ r)    (hs : Ο‡ q = s) (hsum0 : C Ο‡ (q - 1) = 0) (m : β„•)    (hm : βˆ€ d ∈ Nat.digits q m, InR Ο‡ q d) : S Ο‡ m = 0 := by  induction m using Nat.strong_induction_on with  | _ m ih =>    rcases Nat.eq_zero_or_pos m with rfl | hpos    Β· exact S_zero Ο‡    Β· rw [Nat.digits_def' (by omega) hpos] at hm      have hlt : m / q < m := Nat.div_lt_self hpos (by omega)      have h0 := ih (m / q) hlt (fun d hd => hm d (List.mem_cons_of_mem _ hd))      have hR := hm (m % q) (List.mem_cons_self ..)      have := zero_step q hq Ο‡ s hmul hper hs hsum0 (m / q) (m % q) h0 hR      rwa [Nat.div_add_mod] at this /-- **Theorem 3 (complete sequence), list form.** If each entry is at most `1 +` the sum of theearlier entries, every `x ≀ d.sum` is the sum of a sublist of `d`.(The hypothesis "every entry β‰₯ 1" is not needed and has been dropped.) -/theorem complete_list (d : List β„•)    (hd : βˆ€ l₁ a lβ‚‚, d = l₁ ++ a :: lβ‚‚ β†’ a ≀ 1 + l₁.sum) :    βˆ€ x ≀ d.sum, βˆƒ l : List β„•, l.Sublist d ∧ l.sum = x := by  induction d using List.reverseRecOn with  | nil =>    intro x hx    exact ⟨[], List.Sublist.slnil, by simp at hx ⊒; omega⟩  | append_singleton l a ih =>    have hl : βˆ€ l₁ b lβ‚‚, l = l₁ ++ b :: lβ‚‚ β†’ b ≀ 1 + l₁.sum :=      fun l₁ b lβ‚‚ h => hd l₁ b (lβ‚‚ ++ [a]) (by rw [h]; simp)    have ha : a ≀ 1 + l.sum := hd l a [] rfl    intro x hx    simp only [List.sum_append, List.sum_cons, List.sum_nil, add_zero] at hx    by_cases hx' : x ≀ l.sum    Β· obtain ⟨t, ht, hsum⟩ := ih hl x hx'      exact ⟨t, ht.trans (List.sublist_append_left l [a]), hsum⟩    Β· obtain ⟨t, ht, hsum⟩ := ih hl (x - a) (by omega)      refine ⟨t ++ [a], ht.append (List.Sublist.refl _), ?_⟩      simp only [List.sum_append, List.sum_cons, List.sum_nil, add_zero, hsum]      omega /-- **Theorem 3 (complete sequence), indexed form** (`d j`, `j < t`, subsets of `range t`). -/theorem complete_range (d : β„• β†’ β„•) (t : β„•)    (hd : βˆ€ j < t, d j ≀ 1 + βˆ‘ i ∈ range j, d i) :    βˆ€ y ≀ βˆ‘ i ∈ range t, d i, βˆƒ A βŠ† range t, βˆ‘ i ∈ A, d i = y := by  induction t with  | zero =>    intro y hy    exact βŸ¨βˆ…, empty_subset _, by simp at hy ⊒; omega⟩  | succ t ih =>    have hd' : βˆ€ j < t, d j ≀ 1 + βˆ‘ i ∈ range j, d i := fun j hj => hd j (by omega)    have ht := hd t (by omega)    intro y hy    rw [sum_range_succ] at hy    by_cases hy' : y ≀ βˆ‘ i ∈ range t, d i    Β· obtain ⟨A, hA, hsum⟩ := ih hd' y hy'      exact ⟨A, hA.trans (range_subset_range.mpr (by omega)), hsum⟩    Β· obtain ⟨A, hA, hsum⟩ := ih hd' (y - d t) (by omega)      have hnot : t βˆ‰ A := fun h => by simpa using hA h      refine ⟨insert t A, ?_, ?_⟩      Β· intro i hi        rcases mem_insert.mp hi with rfl | hi        Β· simp        Β· exact range_subset_range.mpr (by omega) (hA hi)      Β· rw [sum_insert hnot, hsum]; omega /-- Choosing `v j` on `A` and `u j` off `A` adds `2 * βˆ‘_A d` to `U = βˆ‘ u`. -/lemma choice_sum (t : β„•) (d u v : β„• β†’ β„•) (hv : βˆ€ j, v j = u j + 2 * d j)    (A : Finset β„•) (hA : A βŠ† range t) :    βˆ‘ j ∈ range t, (if j ∈ A then v j else u j)      = βˆ‘ j ∈ range t, u j + 2 * βˆ‘ j ∈ A, d j := by  have : βˆ€ j, (if j ∈ A then v j else u j) = u j + (if j ∈ A then 2 * d j else 0) := by    intro j; split_ifs <;> simp [hv]  simp only [this, sum_add_distrib, sum_ite_mem, inter_eq_right.mpr hA, mul_sum] /-- **Theorem 3, pairs version.** Pairs `(u j, v j)` with `v j = u j + 2 d j`, `d` complete,`q0` odd, `U = βˆ‘ u`, `H = βˆ‘ d`. Every `x ∈ [U + q0 - 1, U + 2H + 1]` is a sum of a selection:optionally `q0`, plus one element from each pair (`v j` for `j ∈ A`, `u j` otherwise).(For distinct `q0, u j, v j` this is a subset sum of `{q0, u j, v j}`; the hypothesis`q0 ≀ 2H + 1` is not needed and has been dropped.) -/theorem pairs_subset_sum (t : β„•) (d u v : β„• β†’ β„•) (q0 : β„•) (hq0 : Odd q0)    (hv : βˆ€ j, v j = u j + 2 * d j)    (hd : βˆ€ j < t, d j ≀ 1 + βˆ‘ i ∈ range j, d i) (x : β„•)    (hx1 : βˆ‘ j ∈ range t, u j + q0 - 1 ≀ x)    (hx2 : x ≀ βˆ‘ j ∈ range t, u j + 2 * βˆ‘ j ∈ range t, d j + 1) :    βˆƒ b : Bool, βˆƒ A βŠ† range t,      x = (if b then q0 else 0) + βˆ‘ j ∈ range t, (if j ∈ A then v j else u j) := by  set U := βˆ‘ j ∈ range t, u j with hU  set H := βˆ‘ j ∈ range t, d j with hH  obtain ⟨c, hc⟩ := hq0  have hUx : U ≀ x := by omega  rcases Nat.even_or_odd (x - U) with ⟨y, hy⟩ | ⟨y, hy⟩  Β· -- x = U + 2y, y ≀ H    obtain ⟨A, hA, hsum⟩ := complete_range d t hd y (by omega)    refine ⟨false, A, hA, ?_⟩    rw [choice_sum t d u v hv A hA, hsum]; simp; omega  Β· -- x = U + q0 + 2y', y' ≀ H    obtain ⟨A, hA, hsum⟩ := complete_range d t hd (y - c) (by omega)    refine ⟨true, A, hA, ?_⟩    rw [choice_sum t d u v hv A hA, hsum]; simp; omega end SegmentoII