/-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