import Mathlib /-! # Summing a quadratic expression by its gcd with n Source: https://oeis.org/A373561 Credit: @anmolspace Mats Granvik conjectured that grouping the values of x²+y²−z² by their gcd with n, then summing over gcds from 1 through n, recovers the polynomial defining A373561. The variables x, y and z also range from 1 through n. -/ namespace OMA.S_oeis_a373561 open Finset /-- The weight can be negative, so the subtraction takes place in the integers. -/ def weight (x y z : ℕ) : ℤ := (x : ℤ)^2 + (y : ℤ)^2 - (z : ℤ)^2 /-- Indexing by 'i+1' makes each of the three coordinates run from 1 through n. -/ def groupedSum (n : ℕ) : ℤ := ∑ k ∈ Icc 1 n, ∑ z ∈ range n, ∑ y ∈ range n, ∑ x ∈ range n, if Int.gcd (weight (x+1) (y+1) (z+1)) n = k then weight (x+1) (y+1) (z+1) else 0 /-- Granvik's conjectured polynomial identity, including the empty sum for n = 0. -/ theorem main : ∀ n : ℕ, (1/3 : ℚ) * n^5 + (1/2 : ℚ) * n^4 + (1/6 : ℚ) * n^3 = (groupedSum n : ℚ) := by sorry end OMA.S_oeis_a373561 /- Solution to OEIS A373561 (Mats Granvik's conjecture), for lefttosolve.com. Proof found by Claude (Anthropic). For n ≥ 1, gcd(f, n) always lies in [1, n], so the sum over k picks out exactly one k for every triple and the bracket disappears. What is left is Σ_{x,y,z ≤ n} (x² + y² − z²) = n² · Σ_{x ≤ n} x² = n³(n+1)(2n+1)/6. -/ namespace OMA.A373561 open OMA.S_oeis_a373561 Finset theorem gcd_mem (n : ℕ) (hn : 0 < n) (w : ℤ) : Int.gcd w (n : ℤ) ∈ Icc 1 n := by have h : Int.gcd w (n : ℤ) = Nat.gcd w.natAbs n := by simp [Int.gcd] rw [Finset.mem_Icc, h] exact ⟨Nat.gcd_pos_of_pos_right _ hn, Nat.gcd_le_right _ hn⟩ theorem collapse (n : ℕ) (hn : 0 < n) (w : ℤ) : (∑ k ∈ Icc 1 n, if Int.gcd w (n : ℤ) = k then w else 0) = w := by rw [Finset.sum_ite_eq] simp [gcd_mem n hn w] theorem grouped_eq (n : ℕ) (hn : 0 < n) : groupedSum n = ∑ z ∈ range n, ∑ y ∈ range n, ∑ x ∈ range n, weight (x+1) (y+1) (z+1) := by unfold groupedSum rw [Finset.sum_comm] refine Finset.sum_congr rfl (fun z _ => ?_) rw [Finset.sum_comm] refine Finset.sum_congr rfl (fun y _ => ?_) rw [Finset.sum_comm] refine Finset.sum_congr rfl (fun x _ => ?_) exact collapse n hn _ theorem sum_sq (n : ℕ) : 6 * (∑ x ∈ range n, ((x + 1 : ℕ) : ℤ) ^ 2) = (n : ℤ) * (n + 1) * (2 * n + 1) := by induction n with | zero => simp | succ k ih => rw [Finset.sum_range_succ, mul_add, ih] push_cast ring theorem gen (n : ℕ) (f : ℕ → ℤ) : (∑ z ∈ range n, ∑ y ∈ range n, ∑ x ∈ range n, (f x + f y - f z)) = (n : ℤ) ^ 2 * ∑ x ∈ range n, f x := by simp only [Finset.sum_add_distrib, Finset.sum_sub_distrib, Finset.sum_const, Finset.card_range, nsmul_eq_mul] simp only [← Finset.mul_sum] ring theorem triple (n : ℕ) : (∑ z ∈ range n, ∑ y ∈ range n, ∑ x ∈ range n, weight (x+1) (y+1) (z+1)) = (n : ℤ) ^ 2 * ∑ x ∈ range n, ((x + 1 : ℕ) : ℤ) ^ 2 := gen n (fun i => ((i + 1 : ℕ) : ℤ) ^ 2) end OMA.A373561 open OMA.A373561 in theorem OMA.solution : type_of% @OMA.S_oeis_a373561.main := by intro n rcases Nat.eq_zero_or_pos n with h0 | hn · subst h0 simp [OMA.S_oeis_a373561.groupedSum] · rw [grouped_eq n hn, triple n] have h6 := sum_sq n have h6q : (6 : ℚ) * ((∑ x ∈ Finset.range n, ((x + 1 : ℕ) : ℤ) ^ 2 : ℤ) : ℚ) = (n : ℚ) * (n + 1) * (2 * n + 1) := by exact_mod_cast h6 push_cast at h6q ⊢ linear_combination (-(n : ℚ) ^ 2 / 6) * h6q #print axioms OMA.solution