import Mathlibopen Finsetnamespace Erdos311Residues/-- The actual set of distinct harmonic subset sums in the problem. -/noncomputable def subsetSums (N : ℕ) : Finset ℚ :=((Icc 1 N).powerset).image (fun A : Finset ℕ => ∑ j ∈ A, (j : ℚ)⁻¹)def commonDen (N : ℕ) := (Icc 1 N).lcm id/-- Exactly the independent full prime-power tests used in the paper. -/def locallyAdmissible (N u : ℕ) : Prop :=∀ p ∈ Icc 1 N, p.Prime → ∃ A ⊆ (Icc 1 N).filter (p ∣ ·),Nat.ModEq (p^(commonDen N).factorization p) u (∑ j ∈ A, commonDen N / j)/-- The full large-N statement, with the real inequalities written using rational coordinates. -/def centralMembership : Prop :=∀ ε : ℚ, 0 < ε → ∃ N₀ : ℕ, ∀ N ≥ N₀, ∀ u : ℕ,let x := (u : ℚ) / commonDen Nlet H := ∑ j ∈ Icc 1 N, (j : ℚ)⁻¹ε ≤ x → x ≤ H-ε → (x ∈ subsetSums N ↔ locallyAdmissible N u)/-- The nonzero digit test on denominators carrying the full p-power. -/def firstDigits (N u : ℕ) : Prop := ∀ p ∈ Icc 1 N, p.Prime →∃ A ⊆ (Icc 1 N).filter (p^(commonDen N).factorization p ∣ ·),Nat.ModEq p u (∑ j ∈ A, commonDen N / j)def representedModuloOne (N u : ℕ) : Prop := ∃ A ⊆ Icc 1 N,Nat.ModEq (commonDen N) u (∑ j ∈ A, commonDen N / j)def fractionalPartTheorem : Prop := ∀ N u : ℕ,representedModuloOne N u ↔ firstDigits N unoncomputable def localCount (N : ℕ) : ℕ := ∏ p ∈ (Icc 1 N).filter Nat.Prime,((((Icc 1 N).filter (p ∣ ·)).powerset).image (fun A : Finset ℕ =>(∑ j ∈ A, commonDen N/j) % p^(commonDen N).factorization p)).carddef countAsymptotic : Prop := Filter.Tendsto (fun N : ℕ =>((subsetSums N).card : ℝ) / localCount N - ∑ j ∈ Icc 1 N, (j : ℝ)⁻¹)Filter.atTop (nhds 0)/-- A projected sum is unchanged when terms invisible in that projection are removed. -/theorem filter_sum {ι : Type*} [DecidableEq ι] {R : Type*} [AddCommMonoid R](A B : Finset ι) (f : ι → R) (h : ∀ j ∈ A, j ∉ B → f j = 0) :∑ j ∈ A, f j = ∑ j ∈ A ∩ B, f j := bysymmapply Finset.sum_subset Finset.inter_subset_leftintro j hj hj'exact h j hj (fun hB => hj' (Finset.mem_inter.mpr ⟨hj,hB⟩))/-- The nonzero common denominator and its divisibility by each allowed index. -/theorem commonDen_ne_zero (N : ℕ) : commonDen N ≠ 0 := byapply Finset.lcm_ne_zero_iff.mprintro j hjhave := (Finset.mem_Icc.mp hj).1simpa only [id_eq] using (show j ≠ 0 by omega)theorem weight_sum_eq (N : ℕ) (A : Finset ℕ) (hA : A ⊆ Icc 1 N) :((∑ j ∈ A, commonDen N/j : ℕ) : ℚ) = (commonDen N : ℚ) * ∑ j ∈ A, (j : ℚ)⁻¹ := byrw [Nat.cast_sum, Finset.mul_sum]apply Finset.sum_congr rflintro d hdhave hdvd : d ∣ commonDen N := Finset.dvd_lcm (f := id) (hA hd)rw [Nat.cast_div hdvd (by have := (Finset.mem_Icc.mp (hA hd)).1; exact_mod_cast (show d ≠ 0 by omega)), div_eq_mul_inv]theorem necessary (N u : ℕ) (h : (u : ℚ)/commonDen N ∈ subsetSums N) : locallyAdmissible N u := byobtain ⟨A,hA,hval⟩ := Finset.mem_image.mp hhave hsub := Finset.mem_powerset.mp hAhave hu : u = ∑ j ∈ A, commonDen N/j := byhave hl : (commonDen N : ℚ) ≠ 0 := by exact_mod_cast commonDen_ne_zero Nhave hw := weight_sum_eq N A hsubchange (∑ j ∈ A, (j : ℚ)⁻¹) = (u : ℚ) / commonDen N at hvalrw [hval] at hwfield_simp [hl] at hwexact_mod_cast hw.symmintro p _ hplet B := (Icc 1 N).filter (p ∣ ·)refine ⟨A ∩ B, Finset.inter_subset_right, ?_⟩apply (ZMod.natCast_eq_natCast_iff ..).mprw [hu, Nat.cast_sum, Nat.cast_sum]apply filter_sumintro j hj hjBapply (ZMod.natCast_eq_zero_iff ..).mprhave hprime : ¬ p ∣ j := fun hd => hjB (Finset.mem_filter.mpr ⟨hsub hj,hd⟩)have hdvd : j ∣ commonDen N := Finset.dvd_lcm (f := id) (hsub hj)apply ((hp.coprime_iff_not_dvd.mpr hprime).pow_left _).dvd_of_dvd_mul_rightsimpa only [Nat.div_mul_cancel hdvd] using Nat.ordProj_dvd (commonDen N) p#print axioms necessary/-- At every finite order, realization modulo one passes each top-digit test. -/theorem top_digits_necessary (N u : ℕ) (h : representedModuloOne N u) : firstDigits N u := byobtain ⟨A,hA,hcongr⟩ := hintro p hpN hplet q := p^(commonDen N).factorization plet B := (Icc 1 N).filter (q ∣ ·)refine ⟨A ∩ B, Finset.inter_subset_right, ?_⟩have hpL : p ∣ commonDen N := Finset.dvd_lcm (f := id) hpNapply (hcongr.of_dvd hpL).transapply (ZMod.natCast_eq_natCast_iff ..).mprw [Nat.cast_sum,Nat.cast_sum]apply filter_sumintro j hj hnapply (ZMod.natCast_eq_zero_iff ..).mprby_contra hneghave hdvd : j ∣ commonDen N := Finset.dvd_lcm (f := id) (hA hj)have hq : q ∣ (commonDen N/j)*j := by simpa [Nat.div_mul_cancel hdvd] using Nat.ordProj_dvd (commonDen N) phave hc := (hp.coprime_iff_not_dvd.mpr hneg).pow_left ((commonDen N).factorization p)exact hn (Finset.mem_filter.mpr ⟨hA hj,hc.dvd_of_dvd_mul_left hq⟩)#print axioms top_digits_necessary/-- Local witnesses from disjoint blocks glue simultaneously under arbitrary additive projections. -/theorem glue {ι κ G : Type*} [DecidableEq ι] [DecidableEq κ] [AddCommMonoid G](I : Finset κ) (block take : κ → Finset ι) (w : ι → G) (target : G)(R : κ → Type*) [∀ i, AddCommMonoid (R i)] (project : ∀ i, G →+ R i)(disj : ∀ i ∈ I, ∀ j ∈ I, i ≠ j → Disjoint (block i) (block j))(within : ∀ i ∈ I, take i ⊆ block i)(invisible : ∀ i ∈ I, ∀ j, j ∉ block i → project i (w j) = 0)(realizes : ∀ i ∈ I, project i (∑ j ∈ take i, w j) = project i target) :∀ i ∈ I, project i (∑ j ∈ I.biUnion take, w j) = project i target := byintro i hihave hinter : I.biUnion take ∩ block i = take i := byext x; constructor· intro hobtain ⟨hU,hB⟩ := Finset.mem_inter.mp hobtain ⟨j,hj,hx⟩ := Finset.mem_biUnion.mp hUby_cases he : j=i· simpa [he] using hx· exact False.elim ((Finset.disjoint_left.mp (disj j hj i hi he)) (within j hj hx) hB)· intro hexact Finset.mem_inter.mpr ⟨Finset.mem_biUnion.mpr ⟨i,hi,h⟩,within i hi h⟩rw [map_sum, filter_sum _ (block i) _ (fun j _ hn => invisible i hi j hn), hinter,← map_sum, realizes i hi]/-- Once a bounded smooth remainder can be represented, three disjoint sets give the whole sum. -/theorem assemble (N : ℕ) (u : ℚ) (T F B : Finset ℕ)(hTF : Disjoint T F) (hUB : Disjoint (T ∪ F) B) (hdom : (T ∪ F) ∪ B ⊆ Icc 1 N)(hrem : ∑ j ∈ B, (j : ℚ)⁻¹ = u - (∑ j ∈ T, (j : ℚ)⁻¹) - ∑ j ∈ F, (j : ℚ)⁻¹) :u ∈ subsetSums N := byapply Finset.mem_image.mprrefine ⟨(T ∪ F) ∪ B, Finset.mem_powerset.mpr hdom, ?_⟩rw [Finset.sum_union hUB, Finset.sum_union hTF, hrem]ring/-- The arithmetic grid with denominator D, expressed without a choice of reduced numerator. -/def onGrid (D : ℕ) (x : ℚ) : Prop := ∃ a : ℤ, x * D = atheorem grid_sub {D : ℕ} {x y : ℚ} (hx : onGrid D x) (hy : onGrid D y) :onGrid D (x-y) := byobtain ⟨a,ha⟩ := hxobtain ⟨b,hb⟩ := hyexact ⟨a-b, by rw [sub_mul,ha,hb]; norm_cast⟩theorem grid_sum {ι : Type*} (D : ℕ) (S : Finset ι) (w : ι → ℚ)(h : ∀ j ∈ S, onGrid D (w j)) : onGrid D (∑ j ∈ S,w j) := byclassicalinduction S using Finset.induction_on with| empty => exact ⟨0,by simp⟩| @insert j S hj ih =>obtain ⟨a,ha⟩ := h j (by simp)obtain ⟨b,hb⟩ := ih (fun i hi => h i (by simp [hi]))exact ⟨a+b,by rw [Finset.sum_insert hj, add_mul, ha,hb]; norm_cast⟩/-- Select a subset of small increments to put the remaining rational in the target interval. -/theorem subset_hit {ι : Type*} (S : Finset ι) (w : ι → ℚ) (lo hi z : ℚ)(start : lo ≤ z) (mass : z-hi ≤ ∑ j ∈ S, w j)(step : ∀ j ∈ S, w j ≤ hi-lo) :∃ F ⊆ S, lo ≤ z-(∑ j ∈ F, w j) ∧ z-(∑ j ∈ F,w j) ≤ hi := byclassicalinduction S using Finset.induction_on generalizing z with| empty => exact ⟨∅,Finset.Subset.rfl,by simpa using start,by simpa using (sub_nonpos.mp (by simpa using mass) : z ≤ hi)⟩| @insert j S hj ih =>by_cases hz : z ≤ hi· exact ⟨∅,Finset.empty_subset _,by simpa using start,by simpa using hz⟩have hw := step j (by simp)obtain ⟨F,hF,hlo,hhi⟩ := ih (z-w j) (by linarith)(by rw [Finset.sum_insert hj] at mass; linarith) (fun d hd => step d (by simp [hd]))have hjF : j ∉ F := fun h => hj (hF h)exact ⟨insert j F,Finset.insert_subset_insert _ hF,by rw [Finset.sum_insert hjF]; linarith,by rw [Finset.sum_insert hjF]; linarith⟩abbrev harmonic (A : Finset ℕ) : ℚ := ∑ j ∈ A, (j : ℚ)⁻¹/-- Finite central lifting. The hypotheses expose precisely the rough congruence, the smalldenominators and the separate scaled smooth-completion input. -/theorem central_lift (N m D : ℕ) (hm : 0 < m) (x : ℚ) (T F C : Finset ℕ)(hdom : (T ∪ F) ∪ C ⊆ Icc 1 N) (hTF : Disjoint T F) (hTC : Disjoint (T ∪ F) C)(hgrid : onGrid (m*D) (x-harmonic T))(hFgrid : ∀ j ∈ F, onGrid (m*D) (j : ℚ)⁻¹)(hlo : (1/3 : ℚ) / m ≤ x-harmonic T)(hmass : x-harmonic T-1/(m : ℚ) ≤ harmonic F)(hstep : ∀ j ∈ F, (j : ℚ)⁻¹ ≤ 1/(m : ℚ)-(1/3 : ℚ)/m)(complete : ∀ v : ℚ, onGrid D v → 1/3 ≤ v → v ≤ 1 →∃ B ⊆ C, harmonic B = v/m) : x ∈ subsetSums N := byhave hm' : (0 : ℚ) < m := by exact_mod_cast hmobtain ⟨F',hF',hlo',hhi'⟩ := subset_hit F (fun j : ℕ => (j : ℚ)⁻¹) _ _ _ hlo hmass hsteplet rest := x-harmonic T-harmonic F'have hg := grid_sub hgrid (grid_sum (m*D) F' _ (fun j hj => hFgrid j (hF' hj)))obtain ⟨a,ha⟩ := hgchange rest * ↑(m*D) = a at hahave hg' : onGrid D (rest*m) := ⟨a,by simpa [Nat.cast_mul,mul_assoc] using ha⟩obtain ⟨B,hB,hvalue⟩ := complete (rest*m) hg' ((div_le_iff₀ hm').mp hlo') ((le_div_iff₀ hm').mp hhi')have hUF : T ∪ F' ⊆ T ∪ F := Finset.union_subset_union_right hF'refine assemble N x T F' B (hTF.mono_right hF') (hTC.mono hUF hB) ?_ ?_· exact (Finset.union_subset_union hUF hB).trans hdom· change harmonic B = restrw [hvalue, mul_div_cancel_right₀ _ (ne_of_gt hm')]#print axioms subset_hit#print axioms central_lift#print axioms glue#print axioms assembleend Erdos311Residues