import Mathlib.Analysis.Asymptotics.AsymptoticEquivalentimport Mathlib.Analysis.SpecialFunctions.Log.Basicimport Mathlib.Data.Finset.Maximport Mathlib.Data.Finset.Sortimport Mathlib.Data.Nat.Factorial.BigOperatorsimport Mathlib.Data.Nat.Latticeimport Mathlib.Order.Interval.Finset.Natimport Mathlib.Tactic.NormNum/-!# Exact definitions for Erdős Problem 390This file fixes the literal finite combinatorial object and the exactasymptotic target used by the paper. A `Finset` represents the distinctfactors: after sorting it, its elements are precisely a strictly increasinglist `a₁ < ... < aₖ`.The quotient `M! / (n!)²` is deliberately valued in `ℚ`. It must not bedefined with `Nat.div`: before admissibility has been proved, `(n!)²` neednot divide `M!`.-/open Filter Topologyopen scoped BigOperatorsnamespace Erdos390.WholePapernoncomputable section/-- The literal integer interval `(n, M]` from the paper. -/def factorInterval (n M : ℕ) : Finset ℕ :=Finset.Ioc n M/-- A factorization of `n!` into distinct integers in `(n, M]`. -/def IsAdmissibleEndpoint (n M : ℕ) : Prop :=∃ factors : Finset ℕ,factors ⊆ factorInterval n M ∧ factors.prod id = n.factorial/-- For `n ≥ 3`, the singleton factorization `{n!}` supplies an endpoint. -/theorem exists_admissibleEndpoint {n : ℕ} (hn : 3 ≤ n) :∃ M, IsAdmissibleEndpoint n M := byrefine ⟨n.factorial, {n.factorial}, ?_, by simp⟩intro a hahave ha' : a = n.factorial := by simpa using hasubst asimp [factorInterval, Nat.lt_factorial_self hn]/-- The Erdős--Guy--Selfridge extremal function. For `n ≥ 3` this is theleast possible upper endpoint, hence the least possible largest factor.The value outside the paper's domain is set to zero only to make thefunction total. -/def f (n : ℕ) : ℕ :=byclassicalexact if hn : 3 ≤ n then Nat.find (exists_admissibleEndpoint hn) else 0/-- The exact rational constant in the paper. -/def C0 : ℝ :=(4029639598 : ℝ) / 25970038185/-- The natural second-order scale `n / log n`. -/def secondOrderScale (n : ℕ) : ℝ :=(n : ℝ) / Real.log (n : ℝ)/-- The error in the paper's asserted expansion. -/def mainError (n : ℕ) : ℝ :=(f n : ℝ) -(2 * (n : ℝ) + C0 * secondOrderScale n)/-- Literal small-`o` formulation of the main asymptotic formula. -/def MainAsymptotic : Prop :=(mainError =o[atTop] secondOrderScale)/-- Equivalent normalized-limit expression displayed in the paper. -/def MainNormalizedLimit : Prop :=Tendsto(fun n : ℕ =>(((f n : ℝ) - 2 * (n : ℝ)) * Real.log (n : ℝ)) / (n : ℝ))atTop (nhds C0)theorem f_spec {n : ℕ} (hn : 3 ≤ n) :IsAdmissibleEndpoint n (f n) := byclassicalrw [f, dif_pos hn]exact Nat.find_spec (exists_admissibleEndpoint hn)theorem f_le_of_admissible {n M : ℕ} (hn : 3 ≤ n)(hM : IsAdmissibleEndpoint n M) :f n ≤ M := byclassicalrw [f, dif_pos hn]exact Nat.find_min' (exists_admissibleEndpoint hn) hMtheorem admissible_mono {n M M' : ℕ} (hM : M ≤ M')(h : IsAdmissibleEndpoint n M) :IsAdmissibleEndpoint n M' := byobtain ⟨s, hs, hprod⟩ := hrefine ⟨s, ?_, hprod⟩intro a hahave hai := Finset.mem_Ioc.mp (hs ha)exact Finset.mem_Ioc.mpr ⟨hai.1, hai.2.trans hM⟩/-- This characterizes `f` as the least legal endpoint, rather than merelyas an implementation via `Nat.find`. -/theorem f_le_iff_admissible {n M : ℕ} (hn : 3 ≤ n) :f n ≤ M ↔ IsAdmissibleEndpoint n M := byconstructor· intro hexact admissible_mono h (f_spec hn)· exact f_le_of_admissible hnendend Erdos390.WholePaperopen Erdos390.WholePapernamespace DeskLargestFactordef PublishedEndpoint (n m : ℕ) : Prop :=∃ factors : Finset ℕ, ∃ h : factors.Nonempty,factors.max' h = m ∧ (∀ a ∈ factors, n < a) ∧ factors.prod id = n.factorialtheorem endpoint_is_admissible {n m : ℕ} (h : PublishedEndpoint n m) :IsAdmissibleEndpoint n m := byobtain ⟨factors, hne, hmax, hgt, hprod⟩ := hrefine ⟨factors, ?_, hprod⟩intro a harefine Finset.mem_Ioc.mpr ⟨hgt a ha, ?_⟩simpa only [hmax] using factors.le_max' a hatheorem f_is_least_published_endpoint {n : ℕ} (hn : 3 ≤ n) :IsLeast {m | PublishedEndpoint n m} (f n) := byobtain ⟨factors, hsubset, hprod⟩ := f_spec hnhave hne : factors.Nonempty := byby_contra hrw [Finset.not_nonempty_iff_eq_empty.mp h, Finset.prod_empty] at hprodhave hn' := Nat.factorial_eq_one.mp hprod.symmomegahave hgt (a : ℕ) (ha : a ∈ factors) : n < a :=(Finset.mem_Ioc.mp (hsubset ha)).1have hmax_le : factors.max' hne ≤ f n :=(Finset.mem_Ioc.mp (hsubset (Finset.max'_mem factors hne))).2have hf_le : f n ≤ factors.max' hne :=f_le_of_admissible hn (endpoint_is_admissible ⟨factors, hne, rfl, hgt, hprod⟩)refine ⟨⟨factors, hne, le_antisymm hmax_le hf_le, hgt, hprod⟩, ?_⟩intro m hmexact f_le_of_admissible hn (endpoint_is_admissible hm)#print axioms f_is_least_published_endpointend DeskLargestFactor/-!# Formal Conjectures bridge for Erdős 390This module deliberately has no dependency on the `formal-conjectures`repository. It gives a namespaced literal copy of that repository'sextremal function, identifies it eventually with `Erdos390.WholePaper.f`,and exports the two conclusion shapes used upstream.The namespace is nested below `Erdos390.WholePaper`, so this module can beimported together with `FormalConjectures.ErdosProblems.390` withoutcolliding with its declarations `Erdos390.f`, `Erdos390.erdos_390`, or`Erdos390.erdos_390.variants.theta`.-/open scoped Nat BigOperatorsopen Filter Asymptotics Realnamespace Erdos390.WholePaper.FormalConjecturesBridgenoncomputable section/-! ## Literal Formal Conjectures endpoint model -//-- The endpoint predicate used by the Formal Conjectures formulation. -/def FCAdmissible (n m : ℕ) : Prop :=∃ k, ∃ g : ℕ → ℕ, StrictMono g ∧n < g 0 ∧ g (k - 1) = m ∧ ∏ i < k, g i = n.factorial/-- A literal, independently namespaced copy of the Formal Conjecturesextremal function. -/noncomputable def formalF (n : ℕ) : ℕ :=sInf {m : ℕ | ∃ k, ∃ g : ℕ → ℕ, StrictMono g ∧n < g 0 ∧ g (k - 1) = m ∧ ∏ i < k, g i = n.factorial}theorem formalF_eq_sInf_FCAdmissible (n : ℕ) :formalF n = sInf {m : ℕ | FCAdmissible n m} := byrfl/-! ## Pointwise endpoint bridge -//-- A Formal Conjectures strictly increasing prefix gives the correspondingfinite set of distinct factors. -/theorem fcAdmissible_to_isAdmissibleEndpoint {n m : ℕ} (hn : 3 ≤ n)(h : FCAdmissible n m) :Erdos390.WholePaper.IsAdmissibleEndpoint n m := byrcases h with ⟨k, g, hg, hg0, hlast, hprod⟩rw [Nat.Iio_eq_range] at hprodhave hk : 0 < k := byby_contra hnothave hk0 : k = 0 := Nat.eq_zero_of_not_pos hnotsubst ksimp only [Finset.range_zero, Finset.prod_empty] at hprodhave hn_le_one : n ≤ 1 := Nat.factorial_eq_one.mp hprod.symmomegarefine ⟨(Finset.range k).image g, ?_, ?_⟩· intro x hxrcases Finset.mem_image.mp hx with ⟨i, hi, rfl⟩have hi_lt : i < k := Finset.mem_range.mp hirw [Erdos390.WholePaper.factorInterval, Finset.mem_Ioc]constructor· exact hg0.trans_le (hg.monotone (Nat.zero_le i))· rw [← hlast]exact hg.monotone (by omega)· calc((Finset.range k).image g).prod id =(Finset.range k).prod (fun i ↦ id (g i)) :=Finset.prod_image hg.injective.injOn_ = n.factorial := by simpa using hprod/-- Every finite admissible set can be increasingly enumerated and extendedto a globally strictly increasing sequence. -/theorem isAdmissibleEndpoint_to_exists_fcAdmissible {n M : ℕ} (hn : 3 ≤ n)(hM : Erdos390.WholePaper.IsAdmissibleEndpoint n M) :∃ m ≤ M, FCAdmissible n m := byrcases hM with ⟨S, hS, hprod⟩have hS_nonempty : S.Nonempty := byby_contra hnothave hS0 : S = ∅ := Finset.not_nonempty_iff_eq_empty.mp hnotsubst Ssimp only [Finset.prod_empty] at hprodhave hn_le_one : n ≤ 1 := Nat.factorial_eq_one.mp hprod.symmomegalet k : ℕ := S.cardhave hk : 0 < k := bysimpa only [k] using Finset.card_pos.mpr hS_nonemptyhave hcard : S.card = k := rfllet e : Fin k ↪o ℕ := S.orderEmbOfFin hcardlet g : ℕ → ℕ := fun i ↦if hi : i < k then e ⟨i, hi⟩ else M + ihave hg : StrictMono g := byintro i j hijby_cases hi : i < k· by_cases hj : j < k· simp only [g, dif_pos hi, dif_pos hj]exact e.strictMono hij· have hei_mem : e ⟨i, hi⟩ ∈ S := bysimpa only [e] using S.orderEmbOfFin_mem hcard ⟨i, hi⟩have hei_le : e ⟨i, hi⟩ ≤ M :=(Finset.mem_Ioc.mp (hS hei_mem)).2have hj_pos : 0 < j := by omegasimp only [g, dif_pos hi, dif_neg hj]omega· have hj : ¬j < k := by omegasimp only [g, dif_neg hi, dif_neg hj]omegahave hlast_lt : k - 1 < k := by omegalet m : ℕ := e ⟨k - 1, hlast_lt⟩have hm_mem : m ∈ S := bysimpa only [m, e] using S.orderEmbOfFin_mem hcard ⟨k - 1, hlast_lt⟩have hm_le : m ≤ M := (Finset.mem_Ioc.mp (hS hm_mem)).2refine ⟨m, hm_le, k, g, hg, ?_, ?_, ?_⟩· have he0_mem : e ⟨0, hk⟩ ∈ S := bysimpa only [e] using S.orderEmbOfFin_mem hcard ⟨0, hk⟩have hn_e0 : n < e ⟨0, hk⟩ :=(Finset.mem_Ioc.mp (hS he0_mem)).1simpa only [g, dif_pos hk] using hn_e0· simp only [g, m, dif_pos hlast_lt]· rw [Nat.Iio_eq_range]have himage : (Finset.range k).image g = S := byapply Finset.eq_of_subset_of_card_le· intro x hxrcases Finset.mem_image.mp hx with ⟨i, hi, rfl⟩have hi_lt : i < k := Finset.mem_range.mp hisimp only [g, dif_pos hi_lt]simpa only [e] using S.orderEmbOfFin_mem hcard ⟨i, hi_lt⟩· rw [Finset.card_image_of_injective _ hg.injective, Finset.card_range, hcard]calc(Finset.range k).prod g =((Finset.range k).image g).prod id := bysymmexact Finset.prod_image hg.injective.injOn_ = S.prod id := by rw [himage]_ = n.factorial := hprod/-- For `n ≥ 3`, the independently defined endpoint minima coincide. -/theorem formalF_eq_wholePaper_f {n : ℕ} (hn : 3 ≤ n) :formalF n = Erdos390.WholePaper.f n := byrw [formalF_eq_sInf_FCAdmissible]apply le_antisymm· obtain ⟨m, hm, hfc⟩ :=isAdmissibleEndpoint_to_exists_fcAdmissible hn(Erdos390.WholePaper.f_spec hn)exact (Nat.sInf_le hfc).trans hm· have hset_nonempty : Set.Nonempty {m : ℕ | FCAdmissible n m} := byobtain ⟨m, _hm, hfc⟩ :=isAdmissibleEndpoint_to_exists_fcAdmissible hn(Erdos390.WholePaper.f_spec hn)exact ⟨m, hfc⟩have hmin : FCAdmissible n (sInf {m : ℕ | FCAdmissible n m}) :=Nat.sInf_mem hset_nonemptyexact Erdos390.WholePaper.f_le_of_admissible hn(fcAdmissible_to_isAdmissibleEndpoint hn hmin)/-- The two endpoint functions are eventually equal; the exceptional values`n < 3` are irrelevant to all `atTop` statements. -/theorem eventually_formalF_eq_wholePaper_f :formalF =ᶠ[atTop] Erdos390.WholePaper.f :=eventually_atTop.2 ⟨3, fun _n hn ↦ formalF_eq_wholePaper_f hn⟩/-! ## Small-o to asymptotic-equivalence wrapper -/private lemma C0_ne_zero : Erdos390.WholePaper.C0 ≠ 0 := bynorm_num [Erdos390.WholePaper.C0]/-- The local literal small-o theorem implies equivalence with its fixedsecond-order coefficient. -/theorem wholePaper_isEquivalent_fixedC0_of_mainAsymptotic(h : Erdos390.WholePaper.MainAsymptotic) :(fun n : ℕ =>(Erdos390.WholePaper.f n : ℝ) - 2 * (n : ℝ)) ~[atTop](fun n : ℕ =>Erdos390.WholePaper.C0 * (n : ℝ) / Real.log (n : ℝ)) := byhave hsmall :Erdos390.WholePaper.mainError =o[atTop]Erdos390.WholePaper.secondOrderScale := bysimpa only [Erdos390.WholePaper.MainAsymptotic] using hhave hscaled :Erdos390.WholePaper.mainError =o[atTop](fun n : ℕ =>Erdos390.WholePaper.C0 *Erdos390.WholePaper.secondOrderScale n) :=hsmall.const_mul_right C0_ne_zerorw [IsEquivalent]have hleft :(fun n : ℕ =>(Erdos390.WholePaper.f n : ℝ) - 2 * (n : ℝ)) -(fun n : ℕ =>Erdos390.WholePaper.C0 * (n : ℝ) /Real.log (n : ℝ)) =Erdos390.WholePaper.mainError := byfunext nsimp only [Pi.sub_apply, Erdos390.WholePaper.mainError,Erdos390.WholePaper.secondOrderScale, sub_add_eq_sub_sub,mul_div_assoc]rw [hleft]simpa only [Erdos390.WholePaper.secondOrderScale, mul_div_assoc] using hscaled/-- The local small-o theorem, together with the endpoint bridge, impliesfixed-coefficient equivalence for the literal Formal Conjectures function. -/theorem formalF_isEquivalent_fixedC0_of_mainAsymptotic(h : Erdos390.WholePaper.MainAsymptotic) :(fun n : ℕ => (formalF n : ℝ) - 2 * (n : ℝ)) ~[atTop](fun n : ℕ =>Erdos390.WholePaper.C0 * (n : ℝ) / Real.log (n : ℝ)) := byhave hleft :(fun n : ℕ => (formalF n : ℝ) - 2 * (n : ℝ)) =ᶠ[atTop](fun n : ℕ =>(Erdos390.WholePaper.f n : ℝ) - 2 * (n : ℝ)) :=eventually_formalF_eq_wholePaper_f.mono fun n hn ↦ bychange(formalF n : ℝ) - 2 * (n : ℝ) =(Erdos390.WholePaper.f n : ℝ) - 2 * (n : ℝ)rw [hn]exact hleft.trans_isEquivalent(wholePaper_isEquivalent_fixedC0_of_mainAsymptotic h)/-- The problem's exact existential shape, conditional on the author's terminal. -/theorem problem_shape_of_terminal(h : Erdos390.WholePaper.MainAsymptotic) :∃ c, (fun n => formalF n - 2 * n : ℕ → ℝ) ~[atTop](fun n => c * n / log (n : ℝ)) :=⟨Erdos390.WholePaper.C0, formalF_isEquivalent_fixedC0_of_mainAsymptotic h⟩#print axioms formalF_eq_wholePaper_f#print axioms problem_shape_of_terminalendend Erdos390.WholePaper.FormalConjecturesBridge