import Mathlib.Tacticnamespace HindmanBands/-- Index-subset sums, including the empty sum. -/def sums : List ℕ → Finset ℕ| [] => {0}| a :: xs => sums xs ∪ (sums xs).image (a + ·)theorem total_mem (xs : List ℕ) : xs.sum ∈ sums xs := byinduction xs with| nil => simp [sums]| cons a xs ih =>exact Finset.mem_union_right _ (Finset.mem_image.mpr ⟨xs.sum, ih, rfl⟩)theorem no_cross (xs : List ℕ) (B : ℕ) (hB : 0 < B)(small : ∀ a ∈ xs, a < B)(gap : ∀ n ∈ sums xs, B ≤ n → 2 * B ≤ n) : xs.sum < B := byinduction xs with| nil => simpa using hB| cons a xs ih =>have ht := ih (fun b hb => small b (List.mem_cons_of_mem _ hb))(fun n hn => gap n (Finset.mem_union_left _ hn))have ha := small a (List.mem_cons_self ..)have hg := gap (a :: xs).sum (total_mem _)simp only [List.sum_cons] at hg ⊢omegatheorem next_band (x y e : ℕ) (hx : 2 ^ e ≤ x) (hy : 2 ^ e ≤ y)(hx' : x < 2 ^ (e + 1)) (hy' : y < 2 ^ (e + 1)) :2 ^ (e+1) ≤ x+y ∧ x+y < 2 ^ (e+2) := byhave h1 : 2^(e+1) = 2^e * 2 := pow_succ _ _have h2 : 2^(e+2) = 2^(e+1) * 2 := pow_succ _ _omegatheorem squares_le_total_sq (xs : List ℕ) :(xs.map fun x => x^2).sum ≤ xs.sum^2 := byinduction xs with| nil => simp| cons a xs ih =>simp only [List.map_cons, List.sum_cons]nlinarith [Nat.zero_le (a * xs.sum)]theorem sharp_variance_bound (xs : List ℕ) (m d : ℕ) (h : d * xs.sum < m) :(d^2 + 1)*m^2 > d^2*(m^2 + (xs.map fun x => x^2).sum) := byhave hs := Nat.mul_le_mul_left (d^2) (squares_le_total_sq xs)have ht : (d * xs.sum)^2 < m^2 := Nat.pow_lt_pow_left h (by decide)nlinarithend HindmanBands