/-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, Claude (AI assistant)-/module public import Mathlib.Combinatorics.Additive.Dissociationpublic import Mathlib.Data.Fintype.BigOperatorspublic import Mathlib.Data.Fintype.EquivFinpublic import Mathlib.Data.Nat.Logpublic import Mathlib.Order.Lattice.Natpublic import Mathlib.Algebra.CharZero.Infinitepublic import Mathlib.Basic.Real.Basicpublic import Mathlib.Tactic.Linarithpublic import Mathlib.Tactic.NormNum /-!# Dissociated subsets of real sets: the greedy bound (ErdΕs Problem 963) `f n` is the largest `k` such that every set of `n` reals has a dissociated subset with at least `k`elements (definition copied verbatim from formal-conjectures). The greedy algorithm gives`Nat.log 3 n β€ f n`: the elements that spoil a dissociated set `B` lie in its `{-1, 0, 1}`-span, ofsize at most `3 ^ #B`. ## Main results * `Erdos963.erdos_963.variants.greedy`: Nat.log 3 n β€ f n (ErdΕs 1965; formal-conjectures erdos_963.variants.greedy)* `Erdos963.card_span_le`: the {-1,0,1}-span of B has at most 3^#B elements* `Erdos963.addDissociated_insert`: B dissociated and x β span B β insert x B dissociated* `Erdos963.exists_dissociated`: 3^j β€ #A β A has a dissociated subset with j elements ## References * P. ErdΕs, Β«Extremal problems in number theoryΒ», Proc. Sympos. Pure Math. VIII (1965) 181β189, p. 188.* erdosproblems.com/963; statement and definition copied verbatim from google-deepmind/formal-conjectures, FormalConjectures/ErdosProblems/963.lean.-/ @[expose] public section namespace Erdos963 /-- $f(n)$ is the largest $k$ such that every set $A\subset\mathbb{R}$ with $n$ elements has adissociated subset with at least $k$ elements. The set of such $k$ contains $0$ (take the emptysubset) and is bounded by $n$, so `sSup` is a true maximum. The set $A$ may contain $0$, whichcan never belong to a dissociated set. For $n = 0$ the value is $0$. -/noncomputable def f (n : β) : β := sSup {k : β | β A : Finset β, A.card = n β β B β A, k β€ B.card β§ AddDissociated (B : Set β)} /-- If every set of `n` reals has a dissociated subset with at least `k` elements, then$k \leq f(n)$. -/theorem le_f {n k : β} (h : β A : Finset β, A.card = n β β B β A, k β€ B.card β§ AddDissociated (B : Set β)) : k β€ f n := by refine le_csSup β¨n, fun m hm β¦ ?_β© h obtain β¨A, hAβ© := Infinite.exists_subset_card_eq β n obtain β¨B, hBA, hmB, -β© := hm A hA exact hmB.trans ((Finset.card_le_card hBA).trans hA.le) /-- The `{-1, 0, 1}`-span of a finset of reals. -/noncomputable def span (B : Finset β) : Finset β := (Fintype.piFinset fun _ : B β¦ ({-1, 0, 1} : Finset β€)).image fun Ξ΅ β¦ β b : B, (Ξ΅ b : β) * b lemma card_span_le (B : Finset β) : (span B).card β€ 3 ^ B.card := by refine (Finset.card_image_le).trans ?_ rw [Fintype.card_piFinset, Finset.prod_const, Finset.card_univ, Fintype.card_coe] have : ({-1, 0, 1} : Finset β€).card = 3 := by decide rw [this] lemma mem_span_of {B : Finset β} (Ξ΅ : β β β€) (hΞ΅ : β b, Ξ΅ b = -1 β¨ Ξ΅ b = 0 β¨ Ξ΅ b = 1) : β b β B, (Ξ΅ b : β) * b β span B := by refine Finset.mem_image.2 β¨fun b β¦ Ξ΅ b, Fintype.mem_piFinset.2 fun b β¦ ?_, ?_β© Β· rcases hΞ΅ b with h | h | h <;> simp [h] Β· exact Finset.sum_coe_sort B (fun b β¦ (Ξ΅ b : β) * b) lemma subset_span (B : Finset β) : B β span B := by classical intro a ha have := mem_span_of (B := B) (fun b β¦ if b = a then 1 else 0) (fun b β¦ by by_cases h : b = a <;> simp [h]) simpa [ha] using this /-- Difference of two subset sums lies in the span. -/lemma sub_mem_span {B t u : Finset β} (ht : t β B) (hu : u β B) : β a β u, a - β a β t, a β span B := by classical have h := mem_span_of (B := B) (fun b β¦ (if b β u then 1 else 0) - (if b β t then 1 else 0)) (fun b β¦ by by_cases h1 : b β u <;> by_cases h2 : b β t <;> simp [h1, h2]) have e : β b β B, (((if b β u then 1 else 0) - (if b β t then 1 else 0) : β€) : β) * b = β a β u, a - β a β t, a := by simp only [Int.cast_sub, Int.cast_ite, Int.cast_one, Int.cast_zero, sub_mul, ite_mul, one_mul, zero_mul, Finset.sum_sub_distrib] rw [β Finset.sum_filter, β Finset.sum_filter, Finset.filter_mem_eq_inter, Finset.filter_mem_eq_inter, Finset.inter_eq_right.2 hu, Finset.inter_eq_right.2 ht] rwa [e] at h /-- Key step: adding an element outside the span keeps a set dissociated. -/lemma addDissociated_insert {B : Finset β} {x : β} (hB : AddDissociated (B : Set β)) (hx : x β span B) : AddDissociated ((insert x B : Finset β) : Set β) := by classical have hxB : x β B := fun h β¦ hx (subset_span B h) -- one-sided case: `x β t`, `x β u` have key : β t u : Finset β, t β insert x B β u β insert x B β x β t β x β u β β a β t, a = β a β u, a β False := by intro t u ht hu hxt hxu hs have hu' : u β B := fun a ha β¦ by rcases Finset.mem_insert.1 (hu ha) with rfl | h Β· exact absurd ha hxu Β· exact h have ht' : t.erase x β B := fun a ha β¦ by rcases Finset.mem_insert.1 (ht (Finset.mem_of_mem_erase ha)) with rfl | h Β· exact absurd rfl (Finset.ne_of_mem_erase ha) Β· exact h have hsum : β a β t, a = x + β a β t.erase x, a := (Finset.add_sum_erase t id hxt).symm apply hx have : x = β a β u, a - β a β t.erase x, a := by linarith rw [this] exact sub_mem_span ht' hu' by_contra hnd obtain β¨t, u, ht, hu, -, htu, hsβ© := not_addDissociated_iff_exists_disjoint.1 hnd have ht0 : t β insert x B := by exact_mod_cast ht have hu0 : u β insert x B := by exact_mod_cast hu by_cases hxt : x β t <;> by_cases hxu : x β u Β· -- both contain `x`: erase it and use dissociation of `B` have ht' : t.erase x β B := fun a ha β¦ by rcases Finset.mem_insert.1 (ht0 (Finset.mem_of_mem_erase ha)) with rfl | h Β· exact absurd rfl (Finset.ne_of_mem_erase ha) Β· exact h have hu' : u.erase x β B := fun a ha β¦ by rcases Finset.mem_insert.1 (hu0 (Finset.mem_of_mem_erase ha)) with rfl | h Β· exact absurd rfl (Finset.ne_of_mem_erase ha) Β· exact h have h1 := Finset.add_sum_erase t id hxt have h2 := Finset.add_sum_erase u id hxu have heq : t.erase x = u.erase x := by refine hB (by exact_mod_cast ht') (by exact_mod_cast hu') ?_ simp only [id] at h1 h2 show β a β t.erase x, a = β a β u.erase x, a linarith apply htu rw [β Finset.insert_erase hxt, β Finset.insert_erase hxu, heq] Β· exact key t u ht0 hu0 hxt hxu hs Β· exact key u t hu0 ht0 hxu hxt hs.symm Β· have ht' : t β B := fun a ha β¦ by rcases Finset.mem_insert.1 (ht0 ha) with rfl | h Β· exact absurd ha hxt Β· exact h have hu' : u β B := fun a ha β¦ by rcases Finset.mem_insert.1 (hu0 ha) with rfl | h Β· exact absurd ha hxu Β· exact h exact htu (hB (by exact_mod_cast ht') (by exact_mod_cast hu') hs) /-- Greedy construction: if `3 ^ j β€ #A` there is a dissociated `B β A` with `#B = j`. -/lemma exists_dissociated (A : Finset β) : β j : β, 3 ^ j β€ A.card β β B β A, B.card = j β§ AddDissociated (B : Set β) := by classical intro j induction j with | zero => intro _; exact β¨β , Finset.empty_subset _, rfl, by simpβ© | succ j ih => intro hj obtain β¨B, hBA, hBc, hBβ© := ih (le_trans (Nat.pow_le_pow_right (by norm_num) j.le_succ) hj) have hlt : (span B).card < A.card := by have := card_span_le B rw [hBc] at this have : 3 ^ j < 3 ^ (j + 1) := Nat.pow_lt_pow_right (by norm_num) j.lt_succ_self omega obtain β¨x, hxA, hxsβ© := Finset.exists_mem_notMem_of_card_lt_card hlt have hxB : x β B := fun h β¦ hxs (subset_span B h) refine β¨insert x B, Finset.insert_subset hxA hBA, ?_, addDissociated_insert hB hxsβ© rw [Finset.card_insert_of_notMem hxB, hBc] /--ErdΕs noted that the greedy algorithm showed $f(n)\geq \lfloor \log_3 n\rfloor$[Er65, p. 188].-/theorem erdos_963.variants.greedy (n : β) : Nat.log 3 n β€ f n := by rcases Nat.eq_zero_or_pos n with rfl | hn Β· simp apply le_f intro A hA obtain β¨B, hBA, hBc, hBβ© := exists_dissociated A (Nat.log 3 n) (hA.symm βΈ Nat.pow_log_le_self 3 hn.ne') exact β¨B, hBA, hBc.ge, hBβ© end Erdos963 end