πŸ›οΈ Atlasβœ’οΈFormalizations.NumberTheory.DedekindSum view on GitHub β†—28 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.Group.Finset.Basicpublic import Mathlib.Algebra.BigOperators.Ring.Finsetpublic import Mathlib.Algebra.BigOperators.Intervalspublic import Mathlib.Algebra.Order.Field.Basicpublic import Mathlib.Data.Int.GCDpublic import Mathlib.Data.Nat.ModEqpublic import Mathlib.Data.Nat.Prime.Intpublic import Mathlib.Data.Int.ModEqpublic import Mathlib.Data.ZMod.Basicpublic import Mathlib.Tactic.Ringpublic import Mathlib.Tactic.FieldSimppublic import Mathlib.Tactic.LinearCombinationpublic import Mathlib.Tactic.Positivitypublic import Mathlib.Tactic.Linarith /-!# Dedekind sums through the sawtooth function `DedekindSum.saw k n = ((n/k))` for `n : β„€`, `0 < k` (`0` when `k ∣ n`, otherwise`(n mod k)/k βˆ’ 1/2`), and `DedekindSum.ded h k = s(h, k) = βˆ‘_{r mod k} ((r/k)) ((hr/k))`. Mathlibhas neither (checked at the pinned commit). ## Main results * `DedekindSum.ded_congr`: `s(h, k)` depends only on `h mod k`.* `DedekindSum.ded_neg`: `s(βˆ’h, k) = βˆ’s(h, k)`.* `DedekindSum.ded_reciprocity`: `s(h, k) + s(k, h) = (hΒ² + kΒ² + 1)/(12hk) βˆ’ 1/4` for coprime  `h, k β‰₯ 1` (elementary proof: the permutation `r ↦ hr mod k` and a lattice-point count of  `βˆ‘ ⌊hr/kβŒ‹Β²`).* `DedekindSum.ded_inv`: `s(h⁻¹, k) = s(h, k)`.* `DedekindSum.saw_dist`: the distribution relation `βˆ‘_{j<q} (((n + jm)/(qm))) = ((n/m))`.* `DedekindSum.ded_scale`: `s(qy, qm) = s(y, m)`.* `DedekindSum.petersson_knopp_prime`: for `q` prime,  `βˆ‘_{l<q} s(x + lm, qm) = (q + 1) s(x, m) βˆ’ s(qx, m)`.* `DedekindSum.petersson_knopp_coprime_lifts`: the version for `q ∀ m`, `qy ≑ x (mod m)`. ## References * S. R. Louboutin and M. Munsch, Canad. J. Math. 75 (2023), 1711–1743, arXiv:2205.01024 (its  Conjecture 5.7 is the motivation).-/ @[expose] public section namespace DedekindSum open Finset /-- The sawtooth `((n/k))`. -/def saw (k : β„•) (n : β„€) : β„š :=  if n % (k : β„€) = 0 then 0 else ((n % (k : β„€) : β„€) : β„š) / k - 1 / 2 /-- The Dedekind sum `s(h,k) = Ξ£_{r=0}^{k-1} ((r/k)) ((hr/k))`. -/def ded (h : β„€) (k : β„•) : β„š := βˆ‘ r ∈ range k, saw k r * saw k (h * r) lemma saw_emod (k : β„•) (n : β„€) : saw k (n % k) = saw k n := by  unfold saw; rw [Int.emod_emod] lemma saw_congr {k : β„•} {n n' : β„€} (h : n ≑ n' [ZMOD k]) : saw k n = saw k n' := by  have e : n % k = n' % k := h  rw [← saw_emod k n, e, saw_emod] /-- (a) `s(h,k)` depends only on `h mod k`. -/theorem ded_congr {h h' : β„€} {k : β„•} (hh : h ≑ h' [ZMOD k]) : ded h k = ded h' k := by  unfold ded  refine sum_congr rfl fun r _ => ?_  rw [saw_congr (hh.mul_right (r : β„€))] lemma saw_neg {k : β„•} (hk : 0 < k) (n : β„€) : saw k (-n) = -saw k n := by  unfold saw  rw [Int.neg_emod]  by_cases hd : (k : β„€) ∣ n  Β· have : n % k = 0 := Int.emod_eq_zero_of_dvd hd    simp [hd, this]  Β· have h1 : n % (k : β„€) β‰  0 := fun h0 => hd (Int.dvd_of_emod_eq_zero h0)    have hk' : (0 : β„€) < k := by exact_mod_cast hk    have hlt : n % (k : β„€) < k := Int.emod_lt_of_pos n hk'    have h2 : ((k : β„€).natAbs : β„€) - n % (k : β„€) β‰  0 := by      rw [Int.natAbs_natCast]; omega    simp only [hd, ite_false, h1, h2]    rw [Int.natAbs_natCast]    have hkq : (k : β„š) β‰  0 := by exact_mod_cast hk.ne'    push_cast    field_simp    ring /-- (b) `s(βˆ’h,k) = βˆ’s(h,k)`. -/theorem ded_neg (h : β„€) (k : β„•) : ded (-h) k = -ded h k := by  rcases Nat.eq_zero_or_pos k with rfl | hk  Β· simp [ded]  unfold ded  rw [← sum_neg_distrib]  refine sum_congr rfl fun r _ => ?_  rw [neg_mul, saw_neg hk, mul_neg] /-! ### Two-term reciprocity (and the inverse symmetry of (F2)) -/ /-- Multiplication by a unit permutes the residues `0, …, k-1`. -/lemma perm_sum {h k : β„•} (hc : Nat.Coprime h k) (F : β„• β†’ β„š) :    βˆ‘ r ∈ range k, F (h * r % k) = βˆ‘ r ∈ range k, F r := by  rcases Nat.eq_zero_or_pos k with rfl | hk  Β· simp  have hinj : Set.InjOn (fun r => h * r % k) (range k) := by    intro a ha b hb hab    simp only [coe_range, Set.mem_Iio] at ha hb    have h1 : h * a ≑ h * b [MOD k] := hab    have h2 := Nat.ModEq.cancel_left_of_coprime (Nat.Coprime.symm hc) h1    unfold Nat.ModEq at h2    rwa [Nat.mod_eq_of_lt ha, Nat.mod_eq_of_lt hb] at h2  have himg : (range k).image (fun r => h * r % k) = range k := by    apply eq_of_subset_of_card_le    Β· intro x hx      simp only [mem_image, mem_range] at hx ⊒      obtain ⟨r, _, rfl⟩ := hx      exact Nat.mod_lt _ hk    Β· rw [card_image_of_injOn hinj]  calc βˆ‘ r ∈ range k, F (h * r % k) = βˆ‘ x ∈ (range k).image (fun r => h * r % k), F x :=        (sum_image hinj).symm    _ = _ := by rw [himg] /-- **(F2)** `s(h⁻¹,k) = s(h,k)`: if `h h' ≑ 1 (mod k)` then `s(h',k) = s(h,k)`. -/theorem ded_inv {h h' k : β„•} (hinv : h * h' ≑ 1 [MOD k]) : ded h' k = ded h k := by  have hc : Nat.Coprime h k := Nat.coprime_of_mul_modEq_one h' hinv  have hmod : βˆ€ n : β„•, saw k ((n % k : β„•) : β„€) = saw k (n : β„€) := fun n => by    rw [Int.natCast_mod, saw_emod]  unfold ded  rw [← perm_sum hc (fun r => saw k (r : β„€) * saw k ((h' : β„€) * (r : β„€)))]  refine sum_congr rfl fun r _ => ?_  have e1 : saw k ((h * r % k : β„•) : β„€) = saw k ((h : β„€) * r) := by rw [hmod, Nat.cast_mul]  have e2 : saw k ((h' : β„€) * ((h * r % k : β„•) : β„€)) = saw k (r : β„€) := by    apply saw_congr    rw [← Nat.cast_mul, Int.natCast_modEq_iff]    calc h' * (h * r % k) ≑ h' * (h * r) [MOD k] := Nat.ModEq.mul_left _ (Nat.mod_modEq _ _)      _ = (h * h') * r := by ring      _ ≑ 1 * r [MOD k] := Nat.ModEq.mul_right _ hinv      _ = r := one_mul r  rw [e1, e2, mul_comm] lemma sum_id (k : β„•) : βˆ‘ r ∈ range k, (r : β„š) = k * (k - 1) / 2 := by  induction k with  | zero => simp  | succ n ih => rw [sum_range_succ, ih]; push_cast; ring lemma sum_sq (k : β„•) : βˆ‘ r ∈ range k, (r : β„š) ^ 2 = (k - 1) * k * (2 * k - 1) / 6 := by  induction k with  | zero => simp  | succ n ih => rw [sum_range_succ, ih]; push_cast; ring lemma sum_odd (n : β„•) : βˆ‘ i ∈ Ico 1 (n + 1), (2 * (i : β„š) - 1) = (n : β„š) ^ 2 := by  induction n with  | zero => simp  | succ n ih => rw [sum_Ico_succ_top (by omega), ih]; push_cast; ring lemma rho_cast (h k r : β„•) : ((h * r % k : β„•) : β„š) = h * r - k * ((h * r / k : β„•) : β„š) := by  have := congrArg (Nat.cast : β„• β†’ β„š) (Nat.div_add_mod (h * r) k)  push_cast at this  linarith /-- `P h k = Ξ£ r ⌊hr/kβŒ‹`, `Q h k = Ξ£ ⌊hr/kβŒ‹Β²`, `M h k = Ξ£ ⌊hr/kβŒ‹` (r over `range k`). -/def P (h k : β„•) : β„š := βˆ‘ r ∈ range k, (r : β„š) * ((h * r / k : β„•) : β„š)def Q (h k : β„•) : β„š := βˆ‘ r ∈ range k, ((h * r / k : β„•) : β„š) ^ 2def M (h k : β„•) : β„š := βˆ‘ r ∈ range k, ((h * r / k : β„•) : β„š) /-- Ξ£ (hr mod k) = Ξ£ r. -/lemma eqn1 {h k : β„•} (hc : Nat.Coprime h k) :    (h : β„š) * (k * (k - 1) / 2) - k * M h k = k * (k - 1) / 2 := by  have := perm_sum hc (fun x => (x : β„š))  simp only [rho_cast] at this  rw [sum_sub_distrib, ← mul_sum, sum_id, ← mul_sum] at this  unfold M  linarith /-- Ξ£ (hr mod k)Β² = Ξ£ rΒ². -/lemma eqn2 {h k : β„•} (hc : Nat.Coprime h k) :    (h : β„š) ^ 2 * ((k - 1) * k * (2 * k - 1) / 6) - 2 * h * k * P h k + k ^ 2 * Q h k      = (k - 1) * k * (2 * k - 1) / 6 := by  have := perm_sum hc (fun x => (x : β„š) ^ 2)  simp only [rho_cast] at this  have e : βˆ€ r ∈ range k, ((h : β„š) * r - k * ((h * r / k : β„•) : β„š)) ^ 2      = (h : β„š) ^ 2 * (r : β„š) ^ 2 - 2 * h * k * ((r : β„š) * ((h * r / k : β„•) : β„š))        + (k : β„š) ^ 2 * ((h * r / k : β„•) : β„š) ^ 2 := fun r _ => by ring  rw [sum_congr rfl e, sum_add_distrib, sum_sub_distrib, ← mul_sum, ← mul_sum, ← mul_sum,    sum_sq] at this  unfold P Q  linarith /-- `s(h,k)` through `P` and `M`. -/lemma ded_formula {h k : β„•} (hc : Nat.Coprime h k) (hk : 0 < k) :    ded h k = (h : β„š) * ((k - 1) * k * (2 * k - 1) / 6) / k ^ 2 - P h k / k      - (k * (k - 1) / 2) / (2 * k) - h * (k * (k - 1) / 2) / (2 * k) + M h k / 2      + k / 4 - 1 / 4 := by  have hkq : (k : β„š) β‰  0 := by exact_mod_cast hk.ne'  -- termwise  have term : βˆ€ r ∈ range k, saw k (r : β„€) * saw k ((h : β„€) * (r : β„€))      = ((r : β„š) / k - 1 / 2) * (((h * r % k : β„•) : β„š) / k - 1 / 2)        - (if r = 0 then 1 / 4 else 0) := by    intro r hr    rw [mem_range] at hr    have e : ((h : β„€) * (r : β„€)) % (k : β„€) = ((h * r % k : β„•) : β„€) := by      rw [Int.natCast_mod, Nat.cast_mul]    rcases Nat.eq_zero_or_pos r with rfl | hr0    Β· simp [saw]; norm_num    Β· have er : (r : β„€) % (k : β„€) = (r : β„€) := by        rw [← Int.natCast_mod, Nat.mod_eq_of_lt hr]      have hne : h * r % k β‰  0 := by        intro h0        have hd : k ∣ h * r := Nat.dvd_of_mod_eq_zero h0        have := Nat.eq_zero_of_dvd_of_lt ((Nat.Coprime.symm hc).dvd_of_dvd_mul_left hd) hr        omega      have hr0' : r β‰  0 := by omega      unfold saw      rw [e, er]      simp only [Nat.cast_eq_zero, hr0', hne, ite_false, Int.cast_natCast]      ring  unfold ded  rw [sum_congr rfl term, sum_sub_distrib, sum_ite_eq' (range k) 0, ite_eq_left (mem_range.2 hk)]  simp only [rho_cast]  have e2 : βˆ€ r ∈ range k, ((r : β„š) / k - 1 / 2) * (((h : β„š) * r - k * ((h * r / k : β„•) : β„š)) / k - 1 / 2)      = (h : β„š) / k ^ 2 * (r : β„š) ^ 2 - 1 / k * ((r : β„š) * ((h * r / k : β„•) : β„š))        - (1 / (2 * k) + h / (2 * k)) * (r : β„š) + 1 / 2 * ((h * r / k : β„•) : β„š) + 1 / 4 := by    intro r _; field_simp; ring  rw [sum_congr rfl e2, sum_add_distrib, sum_add_distrib, sum_sub_distrib, sum_sub_distrib,    ← mul_sum, ← mul_sum, ← mul_sum, ← mul_sum, sum_id, sum_sq, sum_const, card_range]  unfold P M  simp only [nsmul_eq_mul]  field_simp  ring /-- For `r < k`: `⌊hr/kβŒ‹Β² = Ξ£_{1 ≀ i < h, ki ≀ hr} (2i βˆ’ 1)`. -/lemma sq_as_sum {h k r : β„•} (hh : 0 < h) (hk : 0 < k) (hr : r < k) :    ((h * r / k : β„•) : β„š) ^ 2 = βˆ‘ i ∈ Ico 1 h, if k * i ≀ h * r then (2 * (i : β„š) - 1) else 0 := by  rw [← sum_filter]  have hΞΌ : h * r / k < h := by    rw [Nat.div_lt_iff_lt_mul hk]    nlinarith  have hf : (Ico 1 h).filter (fun i => k * i ≀ h * r) = Ico 1 (h * r / k + 1) := by    ext i    simp only [mem_filter, mem_Ico]    have hiff : k * i ≀ h * r ↔ i ≀ h * r / k := by      rw [Nat.le_div_iff_mul_le hk, Nat.mul_comm i k]    rw [hiff]    omega  rw [hf, sum_odd] /-- For `1 ≀ i < h`: `#{r < k : hr ≀ ki} = ⌊ki/hβŒ‹ + 1`. -/lemma count_as_sum {h k i : β„•} (hh : 0 < h) (hk : 0 < k) (hi : i < h) :    ((k * i / h : β„•) : β„š) + 1 = βˆ‘ r ∈ range k, if h * r ≀ k * i then (1 : β„š) else 0 := by  rw [sum_boole]  have hΞ½ : k * i / h < k := by    rw [Nat.div_lt_iff_lt_mul hh]    nlinarith  have hf : (range k).filter (fun r => h * r ≀ k * i) = range (k * i / h + 1) := by    ext r    simp only [mem_filter, mem_range]    have hiff : h * r ≀ k * i ↔ r ≀ k * i / h := by      rw [Nat.le_div_iff_mul_le hh, Nat.mul_comm r h]    rw [hiff]    omega  rw [hf, card_range]  push_cast  ring /-- Lattice-point double count: `Q h k + 2 P k h βˆ’ M k h + (hβˆ’1)Β² = k (hβˆ’1)Β²`. -/lemma eqn3 {h k : β„•} (hc : Nat.Coprime h k) (hh : 0 < h) (hk : 0 < k) :    Q h k + 2 * P k h - M k h + ((h : β„š) - 1) ^ 2 = k * ((h : β„š) - 1) ^ 2 := by  -- left block  have hQ : Q h k = βˆ‘ r ∈ range k, βˆ‘ i ∈ Ico 1 h,      (if k * i ≀ h * r then (2 * (i : β„š) - 1) else 0) := by    unfold Q    exact sum_congr rfl fun r hr => sq_as_sum hh hk (mem_range.1 hr)  -- right block  have hR : 2 * P k h - M k h + ((h : β„š) - 1) ^ 2 = βˆ‘ i ∈ Ico 1 h, βˆ‘ r ∈ range k,      (if h * r ≀ k * i then (2 * (i : β„š) - 1) else 0) := by    have e1 : βˆ€ i ∈ Ico 1 h, βˆ‘ r ∈ range k, (if h * r ≀ k * i then (2 * (i : β„š) - 1) else 0)        = (2 * (i : β„š) - 1) * (((k * i / h : β„•) : β„š) + 1) := by      intro i hi      rw [count_as_sum hh hk (mem_Ico.1 hi).2, mul_sum]      refine sum_congr rfl fun r _ => ?_      split_ifs <;> ring    rw [sum_congr rfl e1]    have e2 : βˆ€ i ∈ Ico 1 h, (2 * (i : β„š) - 1) * (((k * i / h : β„•) : β„š) + 1)        = (2 * ((i : β„š) * ((k * i / h : β„•) : β„š)) - ((k * i / h : β„•) : β„š)) + (2 * (i : β„š) - 1) :=      fun i _ => by ring    rw [sum_congr rfl e2, sum_add_distrib]    have hodd : βˆ‘ i ∈ Ico 1 h, (2 * (i : β„š) - 1) = ((h : β„š) - 1) ^ 2 := by      obtain ⟨n, rfl⟩ : βˆƒ n, h = n + 1 := ⟨h - 1, by omega⟩      rw [sum_odd]; push_cast; ring    rw [hodd]    have hshift : βˆ‘ i ∈ range h, (2 * ((i : β„š) * ((k * i / h : β„•) : β„š)) - ((k * i / h : β„•) : β„š))        = βˆ‘ i ∈ Ico 1 h, (2 * ((i : β„š) * ((k * i / h : β„•) : β„š)) - ((k * i / h : β„•) : β„š)) := by      rw [range_eq_Ico, sum_eq_sum_Ico_succ_bot hh]      simp    rw [← hshift, sum_sub_distrib, ← mul_sum]    unfold P M    ring  -- each pair (r,i) lies in exactly one triangle  have hpair : βˆ€ r ∈ range k, βˆ€ i ∈ Ico 1 h,      (if k * i ≀ h * r then (2 * (i : β„š) - 1) else 0)        + (if h * r ≀ k * i then (2 * (i : β„š) - 1) else 0) = 2 * (i : β„š) - 1 := by    intro r hr i hi    rw [mem_range] at hr    rw [mem_Ico] at hi    have hne : k * i β‰  h * r := by      intro heq      have hd : k ∣ h * r := ⟨i, heq.symm⟩      have h0 := Nat.eq_zero_of_dvd_of_lt ((Nat.Coprime.symm hc).dvd_of_dvd_mul_left hd) hr      subst h0      have : 0 < k * i := Nat.mul_pos hk (by omega)      omega    rcases lt_or_gt_of_ne hne with hlt | hgt    Β· rw [ite_eq_left (by omega), ite_eq_right (by omega)]; ring    Β· rw [ite_eq_right (by omega), ite_eq_left (by omega)]; ring  have htot : βˆ‘ r ∈ range k, βˆ‘ i ∈ Ico 1 h, (2 * (i : β„š) - 1) = k * ((h : β„š) - 1) ^ 2 := by    obtain ⟨n, rfl⟩ : βˆƒ n, h = n + 1 := ⟨h - 1, by omega⟩    rw [sum_const, card_range, sum_odd, nsmul_eq_mul]    push_cast; ring  rw [add_sub_assoc, add_assoc, hR, hQ, sum_comm (s := Ico 1 h), ← sum_add_distrib, ← htot]  refine sum_congr rfl fun r hr => ?_  rw [← sum_add_distrib]  exact sum_congr rfl fun i hi => hpair r hr i hi /-- (c) **Dedekind reciprocity.** For coprime positive integers `h, k`,`s(h,k) + s(k,h) = (hΒ² + kΒ² + 1)/(12hk) βˆ’ 1/4`. -/theorem ded_reciprocity {h k : β„•} (hc : Nat.Coprime h k) (hh : 0 < h) (hk : 0 < k) :    ded h k + ded k h = ((h : β„š) ^ 2 + k ^ 2 + 1) / (12 * h * k) - 1 / 4 := by  have hhq : (h : β„š) β‰  0 := by exact_mod_cast hh.ne'  have hkq : (k : β„š) β‰  0 := by exact_mod_cast hk.ne'  have E1 := eqn1 hc  have E2 := eqn2 hc  have E1p := eqn1 (Nat.Coprime.symm hc)  have E3 := eqn3 hc hh hk  rw [ded_formula hc hk, ded_formula (Nat.Coprime.symm hc) hh]  linear_combination (norm := skip) (-1 / (2 * (k : β„š))) * E1    + (1 / (2 * (h : β„š) * (k : β„š) ^ 2)) * E2 + (-1 / (2 * (h : β„š))) * E3    + ((1 - (h : β„š)) / (2 * (h : β„š) ^ 2)) * E1p  field_simp  ring /-! ### (d) Distribution relation (F3) -/ /-- `((qn/(qm))) = ((n/m))`. -/lemma saw_scale {q m : β„•} (hq : 0 < q) (hm : 0 < m) (n : β„€) :    saw (q * m) ((q : β„€) * n) = saw m n := by  have hq' : (0 : β„€) < q := by exact_mod_cast hq  have hqq : (q : β„š) β‰  0 := by exact_mod_cast hq.ne'  have hmq : (m : β„š) β‰  0 := by exact_mod_cast hm.ne'  unfold saw  rw [Nat.cast_mul, Int.mul_emod_mul_of_pos _ _ hq']  by_cases h0 : n % (m : β„€) = 0  Β· simp [h0]  Β· have : (q : β„€) * (n % m) β‰  0 := mul_ne_zero hq'.ne' h0    rw [ite_eq_right this, ite_eq_right h0]    push_cast    field_simp lemma saw_of_lt {k : β„•} {n : β„€} (h0 : 0 ≀ n) (hlt : n < k) :    saw k n = (n : β„š) / k - 1 / 2 + (if n = 0 then 1 / 2 else 0) := by  unfold saw  rw [Int.emod_eq_of_lt h0 hlt]  by_cases h : n = 0  Β· subst h; simp  Β· rw [ite_eq_right h, ite_eq_right h]; simp /-- A `q`-periodic function has the same sum over any `q` consecutive integers. -/lemma shift_sum (q : β„•) (H : β„€ β†’ β„š) (hH : βˆ€ t, H (t + q) = H t) (a : β„€) :    βˆ‘ j ∈ range q, H (a + j) = βˆ‘ j ∈ range q, H j := by  have step : βˆ€ b : β„€, βˆ‘ j ∈ range q, H (b + 1 + j) = βˆ‘ j ∈ range q, H (b + j) := by    intro b    have h1 := sum_range_succ (fun j : β„• => H (b + j)) q    have h2 := sum_range_succ' (fun j : β„• => H (b + j)) q    simp only [Nat.cast_add, Nat.cast_one, Nat.cast_zero, add_zero] at h1 h2    have e : βˆ€ j : β„•, b + 1 + (j : β„€) = b + ((j : β„€) + 1) := fun j => by ring    simp only [e]    rw [hH b] at h1    linarith  induction a using Int.induction_on with  | zero => simp  | succ i ih => rw [step, ih]  | pred i ih => rw [← step (-(i : β„€) - 1), sub_add_cancel, ih] /-- **(F3)** Discrete distribution relation: `Ξ£_{j<q} (((n + jm)/(qm))) = ((n/m))`. -/theorem saw_dist {q m : β„•} (hq : 0 < q) (hm : 0 < m) (n : β„€) :    βˆ‘ j ∈ range q, saw (q * m) (n + j * m) = saw m n := by  have hm' : (0 : β„€) < m := by exact_mod_cast hm  have hqq : (q : β„š) β‰  0 := by exact_mod_cast hq.ne'  have hmq : (m : β„š) β‰  0 := by exact_mod_cast hm.ne'  have hb0 : 0 ≀ n % (m : β„€) := Int.emod_nonneg _ hm'.ne'  have hbm : n % (m : β„€) < m := Int.emod_lt_of_pos _ hm'  have hn : βˆ€ j : β„€, n + j * m = n % m + (n / m + j) * m := fun j => by    have := Int.emod_add_mul_ediv n m    linear_combination -this  let H : β„€ β†’ β„š := fun t => saw (q * m) (n % m + t * m)  have hH : βˆ€ t, H (t + q) = H t := fun t => saw_congr (by    rw [Int.modEq_iff_dvd]; push_cast; exact ⟨-1, by ring⟩)  have hterm : βˆ€ t ∈ range q, H t = (((n % m : β„€) : β„š) / (q * m) - 1 / 2) + 1 / q * (t : β„š)      + (if n % (m : β„€) = 0 ∧ t = 0 then 1 / 2 else 0) := by    intro t ht    rw [mem_range] at ht    have htq : (t : β„€) + 1 ≀ q := by exact_mod_cast ht    have ht0 : (0 : β„€) ≀ t := by positivity    simp only [H]    have hpos : 0 ≀ n % (m : β„€) + (t : β„€) * m := by nlinarith    have hlt : n % (m : β„€) + (t : β„€) * m < ((q * m : β„•) : β„€) := by push_cast; nlinarith    rw [saw_of_lt hpos hlt]    by_cases hc : n % (m : β„€) = 0 ∧ t = 0    Β· obtain ⟨h1, rfl⟩ := hc      simp [h1]    Β· have hz : n % (m : β„€) + (t : β„€) * m β‰  0 := by        intro h        apply hc        have htm : (t : β„€) * m = 0 := by nlinarith        have : (t : β„€) = 0 := by          rcases mul_eq_zero.1 htm with h' | h'          Β· exact h'          Β· omega        exact ⟨by nlinarith, by exact_mod_cast this⟩      rw [ite_eq_right hz, ite_eq_right hc]      push_cast      field_simp      ring  calc βˆ‘ j ∈ range q, saw (q * m) (n + j * m) = βˆ‘ j ∈ range q, H (n / m + j) := by        refine sum_congr rfl fun j _ => ?_        simp only [H]        rw [hn]    _ = βˆ‘ j ∈ range q, H j := shift_sum q H hH _    _ = saw m n := by        rw [sum_congr rfl hterm, sum_add_distrib, sum_add_distrib, ← mul_sum, sum_id, sum_const,          card_range, nsmul_eq_mul]        by_cases hb : n % (m : β„€) = 0        Β· simp only [hb, true_and]          rw [sum_ite_eq' (range q) 0, ite_eq_left (mem_range.2 hq)]          unfold saw          rw [ite_eq_left hb]          push_cast          field_simp          ring        Β· simp only [hb, false_and, ite_false, sum_const_zero]          unfold saw          rw [ite_eq_right hb]          field_simp          ring /-! ### (d) Lemma 1: the prime case of the Petersson–Knopp identity -/ lemma sum_range_mul (q m : β„•) (f : β„• β†’ β„š) :    βˆ‘ r ∈ range (q * m), f r = βˆ‘ j ∈ range q, βˆ‘ i ∈ range m, f (j * m + i) := by  induction q with  | zero => simp  | succ q ih => rw [add_mul, one_mul, sum_range_add, ih, sum_range_succ] /-- The inner sum over `l` in Lemma 1. -/lemma inner_value {q m : β„•} (hq : q.Prime) (hm : 0 < m) (x : β„€) (r : β„•) :    βˆ‘ l ∈ range q, saw (q * m) ((x + l * m) * r)      = saw m (x * r) + (if q ∣ r then (q : β„š) * saw m (x * (r / q : β„•)) - saw m (x * r) else 0) := by  have hq0 := hq.pos  by_cases hd : q ∣ r  Β· rw [ite_eq_left hd]    obtain ⟨r', rfl⟩ := hd    rw [Nat.mul_div_cancel_left r' hq0]    have e : βˆ€ l ∈ range q,        saw (q * m) ((x + l * m) * ((q * r' : β„•) : β„€)) = saw m (x * r') := by      intro l _      have : (x + l * m) * ((q * r' : β„•) : β„€) = (q : β„€) * (x * r' + ((l : β„€) * r') * m) := by        push_cast; ring      rw [this, saw_scale hq0 hm]      apply saw_congr      rw [Int.modEq_iff_dvd]      exact ⟨-((l : β„€) * r'), by ring⟩    rw [sum_congr rfl e, sum_const, card_range, nsmul_eq_mul]    ring  Β· rw [ite_eq_right hd, add_zero]    have hc : Nat.Coprime r q := Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd hq).2 hd)    have e : βˆ€ l ∈ range q, saw (q * m) ((x + l * m) * r)        = saw (q * m) (x * r + ((r * l % q : β„•) : β„€) * m) := by      intro l _      apply saw_congr      rw [Int.modEq_iff_dvd]      have h0 := Nat.mod_add_div (r * l) q      have h' : ((r * l % q : β„•) : β„€) = (r : β„€) * l - q * ((r * l / q : β„•) : β„€) := by        have := congrArg (Nat.cast : β„• β†’ β„€) h0        simp only [Nat.cast_add, Nat.cast_mul] at this        linarith      refine ⟨-((r * l / q : β„•) : β„€), ?_⟩      rw [h', Nat.cast_mul]      ring    rw [sum_congr rfl e, perm_sum hc (fun j => saw (q * m) (x * r + (j : β„€) * m))]    exact saw_dist hq0 hm (x * r) /-- `Ξ£_{r<qm} ((r/(qm))) ((xr/m)) = s(x,m)` (split `r = jm + i`, then (F3)). -/lemma lift_sum {q m : β„•} (hq0 : 0 < q) (hm : 0 < m) (x : β„€) :    βˆ‘ r ∈ range (q * m), saw (q * m) r * saw m (x * r)      = βˆ‘ r ∈ range m, saw m r * saw m (x * r) := by  rw [sum_range_mul, sum_comm]  refine sum_congr rfl fun i _ => ?_  have e : βˆ€ j ∈ range q, saw (q * m) ((j * m + i : β„•) : β„€) * saw m (x * ((j * m + i : β„•) : β„€))      = saw (q * m) ((i : β„€) + (j : β„€) * m) * saw m (x * i) := by    intro j _    rw [show ((j * m + i : β„•) : β„€) = (i : β„€) + (j : β„€) * m by push_cast; ring]    congr 1    apply saw_congr    rw [Int.modEq_iff_dvd]    exact ⟨-(x * j), by ring⟩  rw [sum_congr rfl e, ← sum_mul, saw_dist hq0 hm] /-- **(F4)**, the instance used in Corollary 1β€²: `s(qy, qm) = s(y, m)`. -/theorem ded_scale {q m : β„•} (hq0 : 0 < q) (hm : 0 < m) (y : β„€) :    ded (q * y) (q * m) = ded y m := by  unfold ded  rw [← lift_sum hq0 hm y]  refine sum_congr rfl fun r _ => ?_  rw [mul_assoc, saw_scale hq0 hm] /-- **Lemma 1** (prime case of Petersson–Knopp): for a prime `q`, `m β‰₯ 1`, `x ∈ β„€`,`Ξ£_{l<q} s(x + lm, qm) = (q+1) s(x,m) βˆ’ s(qx,m)`. -/theorem petersson_knopp_prime {q m : β„•} (hq : q.Prime) (hm : 0 < m) (x : β„€) :    βˆ‘ l ∈ range q, ded (x + l * m) (q * m) = (q + 1) * ded x m - ded (q * x) m := by  have hq0 := hq.pos  have h1 := lift_sum hq0 hm x  have h2 : βˆ‘ r ∈ range (q * m), saw (q * m) r        * (if q ∣ r then (q : β„š) * saw m (x * (r / q : β„•)) - saw m (x * r) else 0)      = q * βˆ‘ r ∈ range m, saw m r * saw m (x * r)        - βˆ‘ r ∈ range m, saw m r * saw m (q * x * r) := by    rw [show range (q * m) = range (m * q) by rw [Nat.mul_comm], sum_range_mul, mul_sum,      ← sum_sub_distrib]    refine sum_congr rfl fun j _ => ?_    rw [sum_eq_single 0]    Β· rw [add_zero, ite_eq_left (dvd_mul_left q j), Nat.mul_div_cancel j hq0,        show ((j * q : β„•) : β„€) = (q : β„€) * j by push_cast; ring, saw_scale hq0 hm,        show x * ((q : β„€) * j) = q * x * j by ring]      ring    Β· intro i hi hi0      have hnd : Β¬ q ∣ j * q + i := by        intro hdv        have := (Nat.dvd_add_right (dvd_mul_left q j)).1 hdv        exact hi0 (Nat.eq_zero_of_dvd_of_lt this (mem_range.1 hi))      rw [ite_eq_right hnd, mul_zero]    Β· intro h; exact absurd (mem_range.2 hq0) h  unfold ded  rw [sum_comm]  simp_rw [← mul_sum]  simp_rw [inner_value hq hm x, mul_add, sum_add_distrib]  rw [h1, h2]  ring /-- **Corollary 1β€²** (coprime lifts). For a prime `q ∀ m` and `y` with `q y ≑ x (mod m)`(i.e. `y = q⁻¹x mod m`): the sum over the `l < q` with `q ∀ x + lm` equals`(q+1) s(x,m) βˆ’ s(qx,m) βˆ’ s(q⁻¹x,m)`. -/theorem petersson_knopp_coprime_lifts {q m : β„•} (hq : q.Prime) (hm : 0 < m) (hqm : Β¬ q ∣ m)    (x y : β„€) (hy : (q : β„€) * y ≑ x [ZMOD m]) :    βˆ‘ l ∈ range q, (if (q : β„€) ∣ x + l * m then 0 else ded (x + l * m) (q * m))      = (q + 1) * ded x m - ded (q * x) m - ded y m := by  have hq0 := hq.pos  have := Fact.mk hq  have hmq : ((m : β„€) : ZMod q) β‰  0 := by    rw [Ne, ZMod.intCast_zmod_eq_zero_iff_dvd]    exact fun h => hqm (Int.natCast_dvd_natCast.1 h)  -- the unique excluded `l`  set l0 := (-(x : ZMod q) * ((m : β„€) : ZMod q)⁻¹).val with hl0def  have hl0 : l0 < q := ZMod.val_lt _  have hdl0 : (q : β„€) ∣ x + l0 * m := by    rw [← ZMod.intCast_zmod_eq_zero_iff_dvd]    push_cast    rw [hl0def, ZMod.natCast_zmod_val]    have hmq' : ((m : β„•) : ZMod q) β‰  0 := by exact_mod_cast hmq    rw [mul_assoc, Int.cast_natCast, mul_comm _ ((m : β„•) : ZMod q), ZMod.coe_mul_inv_eq_one m      (Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd hq).2 hqm))]    ring  have huniq : βˆ€ l ∈ range q, l β‰  l0 β†’ Β¬ (q : β„€) ∣ x + l * m := by    intro l hl hne hdl    have hd : (q : β„€) ∣ ((l : β„€) - l0) * m := by      have := dvd_sub hdl hdl0      convert this using 1      ring    rcases (Nat.prime_iff_prime_int.mp hq).dvd_or_dvd hd with h | h    Β· obtain ⟨c, hc⟩ := h      have hl' : l < q := mem_range.1 hl      have hcz : c = 0 := by        rcases lt_trichotomy c 0 with h' | h' | h'        Β· have : (q : β„€) * c ≀ -(q : β„€) := by nlinarith          omega        Β· exact h'        Β· have : (q : β„€) * c β‰₯ (q : β„€) := by nlinarith          omega      subst hcz      omega    Β· exact hqm (Int.natCast_dvd_natCast.1 h)  have hsplit : βˆ€ l ∈ range q, ded (x + l * m) (q * m)      = (if (q : β„€) ∣ x + l * m then 0 else ded (x + l * m) (q * m))        + (if (q : β„€) ∣ x + l * m then ded (x + l * m) (q * m) else 0) := by    intro l _    split_ifs <;> simp  have hkey : βˆ‘ l ∈ range q, (if (q : β„€) ∣ x + l * m then ded (x + l * m) (q * m) else 0)      = ded y m := by    rw [sum_eq_single l0 (fun l hl hne => ite_eq_right (huniq l hl hne))      (fun h => absurd (mem_range.2 hl0) h), ite_eq_left hdl0]    obtain ⟨z, hz⟩ := hdl0    rw [hz, ded_scale hq0 hm]    apply ded_congr    have h1 : (q : β„€) * z ≑ (q : β„€) * y [ZMOD m] := by      rw [← hz]      refine Int.ModEq.trans ?_ hy.symm      rw [Int.modEq_iff_dvd]      exact ⟨-(l0 : β„€), by ring⟩    have hcop : Nat.Coprime m q := (Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd hq).2 hqm))    have h2 := Int.ModEq.cancel_left_div_gcd (by exact_mod_cast hm) h1    rwa [Int.gcd_natCast_natCast, hcop, Nat.cast_one, Int.ediv_one] at h2  have hPK := petersson_knopp_prime hq hm x  rw [sum_congr rfl hsplit, sum_add_distrib, hkey] at hPK  linarith end DedekindSum -- axiom audit