import Mathlib

/-!
# Halving the even top elements of the Schinzel–Szekeres set

Formal part of "Halving the even top elements of the Schinzel–Szekeres set answers Weingartner's
question on Erdős's lcm problem in the negative".

Proved here without hypotheses:
* `Bset_lcm` : the Schinzel–Szekeres set `B(x)` has the lcm property (Schinzel–Szekeres).
* `Sset_lcm`, `Sset_sub`, `Sset_sum` : replacing every even `c ∈ B(x)` with `3c > 2x` by `c/2`
  keeps the lcm property and raises `∑ 1/n` by `∑ 1/c` (Lemma 1).
* `four_mul_mem_E` : odd `m` with `x < 6m`, `4m ≤ x`, `F m ≤ x` gives `4m ∈ E` (Lemma 2).

The two analytic inputs from A. Weingartner, *The Schinzel–Szekeres function*,
Res. Number Theory 11 (2025), Paper 63 (Theorem 2, and Theorem 10 with Table 2) are taken as
explicit hypotheses `hB` and `hN` of `main` and `not_question1`.
-/

open Finset Filter

namespace WQ1

/-- The Schinzel–Szekeres function `F n = max {d · P⁻(d) : d ∣ n, d > 1}` (`0` for `n ≤ 1`). -/
def F (n : ℕ) : ℕ := (n.divisors.filter (fun d => 1 < d)).sup (fun d => d * d.minFac)

/-- `B(x) = {n ≤ x : F n > x, F d ≤ x for every proper divisor d of n}`. -/
def Bset (x : ℕ) : Finset ℕ :=
  (Finset.Icc 1 x).filter (fun n => x < F n ∧ ∀ d ∈ n.divisors, d ≠ n → F d ≤ x)

/-- The lcm property at level `x`. -/
def LcmFree (x : ℕ) (S : Finset ℕ) : Prop := ∀ a ∈ S, ∀ b ∈ S, a ≠ b → x < Nat.lcm a b

lemma le_F {n d : ℕ} (hn : n ≠ 0) (hd : d ∣ n) (h1 : 1 < d) : d * d.minFac ≤ F n := by
  unfold F
  exact Finset.le_sup (f := fun d => d * d.minFac)
    (Finset.mem_filter.mpr ⟨Nat.mem_divisors.mpr ⟨hd, hn⟩, h1⟩)

lemma F_le {n x : ℕ} (h : ∀ d, d ∣ n → 1 < d → d * d.minFac ≤ x) : F n ≤ x := by
  unfold F
  refine Finset.sup_le (fun d hd => ?_)
  obtain ⟨hd, h1⟩ := Finset.mem_filter.mp hd
  exact h d (Nat.mem_divisors.mp hd).1 h1

lemma mem_Bset {x n : ℕ} :
    n ∈ Bset x ↔ (1 ≤ n ∧ n ≤ x) ∧ x < F n ∧ ∀ d ∈ n.divisors, d ≠ n → F d ≤ x := by
  simp [Bset, Finset.mem_Icc]

/-- Elements of `B(x)` satisfy `n · P⁻(n) > x`. -/
lemma Bset_minFac {x n : ℕ} (hn : n ∈ Bset x) : x < n * n.minFac := by
  obtain ⟨⟨h1, _⟩, hF, hprop⟩ := mem_Bset.mp hn
  have hn0 : n ≠ 0 := by omega
  unfold F at hF
  obtain ⟨d, hd, hlt⟩ := Finset.lt_sup_iff.mp hF
  obtain ⟨hdd, hd1⟩ := Finset.mem_filter.mp hd
  have hdn : d ∣ n := (Nat.mem_divisors.mp hdd).1
  by_cases hdn' : d = n
  · subst hdn'; exact hlt
  · have := hprop d hdd hdn'
    have h2 : d * d.minFac ≤ F d := le_F (by omega) dvd_rfl hd1
    omega

/-- `B(x)` is an antichain for divisibility. -/
lemma Bset_antichain {x a b : ℕ} (ha : a ∈ Bset x) (hb : b ∈ Bset x) (hab : a ∣ b) : a = b := by
  by_contra hne
  obtain ⟨⟨hb1, _⟩, _, hprop⟩ := mem_Bset.mp hb
  obtain ⟨_, haF, _⟩ := mem_Bset.mp ha
  have := hprop a (Nat.mem_divisors.mpr ⟨hab, by omega⟩) hne
  omega

/-- Key step: two elements of `B(x)`, neither dividing the other, have lcm `> x`. -/
lemma lcm_gt_of_T {x a b : ℕ} (ha0 : 0 < a) (hb0 : 0 < b)
    (hTa : x < a * a.minFac) (hTb : x < b * b.minFac) (hab : ¬ a ∣ b) (hba : ¬ b ∣ a) :
    x < Nat.lcm a b := by
  by_contra hle
  replace hle := not_lt.mp hle
  set g := Nat.gcd a b with hg
  have hg0 : 0 < g := Nat.gcd_pos_of_pos_left b ha0
  obtain ⟨β, hβ⟩ : g ∣ a := Nat.gcd_dvd_left a b
  obtain ⟨β', hβ'⟩ : g ∣ b := Nat.gcd_dvd_right a b
  have hgl : g * Nat.lcm a b = a * b := Nat.gcd_mul_lcm a b
  -- lcm a b = b * β = a * β'
  have hL1 : Nat.lcm a b = b * β := by
    apply Nat.eq_of_mul_eq_mul_left hg0
    rw [hgl]; conv_lhs => rw [hβ]
    ring
  have hL2 : Nat.lcm a b = a * β' := by
    apply Nat.eq_of_mul_eq_mul_left hg0
    rw [hgl]; conv_lhs => rw [hβ']
    ring
  -- β ≥ 2 and β ∣ a
  have hβ2 : 2 ≤ β := by
    rcases Nat.lt_or_ge β 2 with h | h
    · interval_cases β
      · simp at hβ; omega
      · exact absurd (by rw [hβ, mul_one]; exact Nat.gcd_dvd_right a b) hab
    · exact h
  have hβ'2 : 2 ≤ β' := by
    rcases Nat.lt_or_ge β' 2 with h | h
    · interval_cases β'
      · simp at hβ'; omega
      · exact absurd (by rw [hβ', mul_one]; exact Nat.gcd_dvd_left a b) hba
    · exact h
  have hmfa : a.minFac ≤ β := Nat.minFac_le_of_dvd hβ2 ⟨g, by rw [hβ]; ring⟩
  have hmfb : b.minFac ≤ β' := Nat.minFac_le_of_dvd hβ'2 ⟨g, by rw [hβ']; ring⟩
  -- b β ≤ x < a minFac a ≤ a β  ⇒ b < a ;  a β' ≤ x < b minFac b ≤ b β' ⇒ a < b
  have h1 : b * β < a * β :=
    lt_of_le_of_lt (hL1 ▸ hle) (lt_of_lt_of_le hTa (Nat.mul_le_mul_left a hmfa))
  have h2 : a * β' < b * β' :=
    lt_of_le_of_lt (hL2 ▸ hle) (lt_of_lt_of_le hTb (Nat.mul_le_mul_left b hmfb))
  have := Nat.lt_of_mul_lt_mul_right h1
  have := Nat.lt_of_mul_lt_mul_right h2
  omega

/-- Schinzel–Szekeres: `B(x)` has the lcm property. -/
theorem Bset_lcm (x : ℕ) : LcmFree x (Bset x) := by
  intro a ha b hb hne
  have ha0 : 0 < a := by have := (mem_Bset.mp ha).1.1; omega
  have hb0 : 0 < b := by have := (mem_Bset.mp hb).1.1; omega
  refine lcm_gt_of_T ha0 hb0 (Bset_minFac ha) (Bset_minFac hb) ?_ ?_
  · exact fun h => hne (Bset_antichain ha hb h)
  · exact fun h => hne (Bset_antichain hb ha h).symm

/-! ## Lemma 1: halving the even top elements -/

/-- `E(x)`: the even elements `c` of `B(x)` with `c > 2x/3`. -/
def Eset (x : ℕ) : Finset ℕ := (Bset x).filter (fun c => Even c ∧ 2 * x < 3 * c)

/-- `S(x) = (B(x) ∖ E(x)) ∪ {c/2 : c ∈ E(x)}`. -/
def Sset (x : ℕ) : Finset ℕ := (Bset x \ Eset x) ∪ (Eset x).image (fun c => c / 2)

/-- A positive multiple `L ≤ x` of `m > x/3` is `m` or `2m`. -/
lemma mul_cases {x m L : ℕ} (hm : x < 3 * m) (hL0 : 0 < L) (hLx : L ≤ x) (hd : m ∣ L) :
    L = m ∨ L = 2 * m := by
  obtain ⟨k, rfl⟩ := hd
  have hk : k < 3 := by
    by_contra h
    have := Nat.mul_le_mul_left m (not_lt.mp h)
    omega
  interval_cases k <;> omega

lemma half_vs_B {x c b : ℕ} (hc : c ∈ Eset x) (hb : b ∈ Bset x \ Eset x) :
    x < Nat.lcm (c / 2) b := by
  obtain ⟨hcB, ⟨r, hr⟩, hlt⟩ := Finset.mem_filter.mp hc
  obtain ⟨hbB, hbE⟩ := Finset.mem_sdiff.mp hb
  have hb0 : 0 < b := by have := (mem_Bset.mp hbB).1.1; omega
  by_contra hle
  replace hle := not_lt.mp hle
  have hL0 : 0 < Nat.lcm (c / 2) b := Nat.lcm_pos (by omega) hb0
  have hbL := Nat.dvd_lcm_right (c / 2) b
  have hbc : b ∣ c := by
    rcases mul_cases (m := c / 2) (by omega) hL0 hle (Nat.dvd_lcm_left _ _) with h | h
    · rw [h] at hbL; exact hbL.trans ⟨2, by omega⟩
    · rw [h, show 2 * (c / 2) = c by omega] at hbL; exact hbL
  exact hbE (by rw [Bset_antichain hbB hcB hbc]; exact hc)

lemma half_vs_half {x c c' : ℕ} (hc : c ∈ Eset x) (hc' : c' ∈ Eset x) (hne : c / 2 ≠ c' / 2) :
    x < Nat.lcm (c / 2) (c' / 2) := by
  obtain ⟨hcB, ⟨r, hr⟩, hlt⟩ := Finset.mem_filter.mp hc
  obtain ⟨hcB', ⟨r', hr'⟩, hlt'⟩ := Finset.mem_filter.mp hc'
  have hcx := (mem_Bset.mp hcB).1.2
  have hcx' := (mem_Bset.mp hcB').1.2
  by_contra hle
  replace hle := not_lt.mp hle
  have hL0 : 0 < Nat.lcm (c / 2) (c' / 2) := Nat.lcm_pos (by omega) (by omega)
  rcases mul_cases (m := c / 2) (by omega) hL0 hle (Nat.dvd_lcm_left _ _) with h | h <;>
  rcases mul_cases (m := c' / 2) (by omega) hL0 hle (Nat.dvd_lcm_right _ _) with h' | h' <;>
  omega

/-- Lemma 1 (lcm part): `S(x)` has the lcm property. -/
theorem Sset_lcm (x : ℕ) : LcmFree x (Sset x) := by
  intro a ha b hb hab
  rcases Finset.mem_union.mp ha with ha | ha <;> rcases Finset.mem_union.mp hb with hb | hb
  · exact Bset_lcm x a (Finset.mem_sdiff.mp ha).1 b (Finset.mem_sdiff.mp hb).1 hab
  · obtain ⟨c', hc', rfl⟩ := Finset.mem_image.mp hb
    rw [Nat.lcm_comm]; exact half_vs_B hc' ha
  · obtain ⟨c, hc, rfl⟩ := Finset.mem_image.mp ha
    exact half_vs_B hc hb
  · obtain ⟨c, hc, rfl⟩ := Finset.mem_image.mp ha
    obtain ⟨c', hc', rfl⟩ := Finset.mem_image.mp hb
    exact half_vs_half hc hc' hab

lemma F_one : F 1 = 0 := by simp [F]

/-- Lemma 1 (range): for `x ≥ 3`, `S(x) ⊆ {2, …, x}`. -/
lemma Sset_sub {x : ℕ} (hx : 3 ≤ x) : Sset x ⊆ Finset.Icc 2 x := by
  intro n hn
  rw [Finset.mem_Icc]
  rcases Finset.mem_union.mp hn with h | h
  · obtain ⟨⟨h1, h2⟩, hF, -⟩ := mem_Bset.mp (Finset.mem_sdiff.mp h).1
    refine ⟨?_, h2⟩
    by_contra h0
    have : n = 1 := by omega
    subst this
    rw [F_one] at hF
    omega
  · obtain ⟨c, hc, rfl⟩ := Finset.mem_image.mp h
    obtain ⟨hcB, ⟨r, hr⟩, hlt⟩ := Finset.mem_filter.mp hc
    have := (mem_Bset.mp hcB).1.2
    constructor <;> omega

/-- Lemma 1 (sum): `∑_{S(x)} 1/n = ∑_{B(x)} 1/n + ∑_{E(x)} 1/c`. -/
theorem Sset_sum (x : ℕ) :
    ∑ n ∈ Sset x, (1 : ℝ) / n = ∑ n ∈ Bset x, (1 : ℝ) / n + ∑ c ∈ Eset x, (1 : ℝ) / c := by
  have hEB : Eset x ⊆ Bset x := Finset.filter_subset _ _
  have hdisj : Disjoint (Bset x \ Eset x) ((Eset x).image (fun c => c / 2)) := by
    rw [Finset.disjoint_left]
    intro n hn hn'
    obtain ⟨c, hc, rfl⟩ := Finset.mem_image.mp hn'
    obtain ⟨hcB, ⟨r, hr⟩, hlt⟩ := Finset.mem_filter.mp hc
    have h1 := Bset_antichain (Finset.mem_sdiff.mp hn).1 hcB ⟨2, by omega⟩
    omega
  have hinj : Set.InjOn (fun c => c / 2) (Eset x) := by
    intro c hc c' hc' h
    obtain ⟨-, ⟨r, hr⟩, -⟩ := Finset.mem_filter.mp hc
    obtain ⟨-, ⟨r', hr'⟩, -⟩ := Finset.mem_filter.mp hc'
    simp only at h
    omega
  rw [Sset, Finset.sum_union hdisj, Finset.sum_image hinj, ← Finset.sum_sdiff hEB, add_assoc,
    ← Finset.sum_add_distrib]
  congr 1
  apply Finset.sum_congr rfl
  intro c hc
  obtain ⟨hcB, ⟨r, hr⟩, hlt⟩ := Finset.mem_filter.mp hc
  have hr0 : 0 < r := by omega
  have h2 : c / 2 = r := by omega
  subst hr
  rw [h2]
  have : (0 : ℝ) < r := by exact_mod_cast hr0
  push_cast
  field_simp

/-! ## Lemma 2: the counted family `4m` -/

/-- Lemma 2: odd `m` with `4m ≤ x < 6m` and `F m ≤ x` gives `4m ∈ E(x)`. -/
lemma four_mul_mem_E {x m : ℕ} (hodd : Odd m) (h4 : 4 * m ≤ x) (h6 : x < 6 * m) (hF : F m ≤ x) :
    4 * m ∈ Eset x := by
  have hm0 : 0 < m := hodd.pos
  have hn0 : 4 * m ≠ 0 := by omega
  refine Finset.mem_filter.mpr ⟨mem_Bset.mpr ⟨⟨by omega, h4⟩, ?_, ?_⟩, ⟨2 * m, by ring⟩, by omega⟩
  · have hmf : (4 * m).minFac = 2 := (Nat.minFac_eq_two_iff _).mpr ⟨2 * m, by ring⟩
    have := le_F hn0 (dvd_refl (4 * m)) (by omega)
    rw [hmf] at this
    omega
  · intro d hd hdne
    have hdvd := (Nat.mem_divisors.mp hd).1
    have hd2 : d ≤ 2 * m := by
      obtain ⟨k, hk⟩ := hdvd
      rcases k with _ | _ | k
      · omega
      · simp at hk; omega
      · nlinarith
    have hd0 : 0 < d := Nat.pos_of_dvd_of_pos hdvd (by omega)
    apply F_le
    intro e he he1
    by_cases hev : 2 ∣ e
    · rw [(Nat.minFac_eq_two_iff e).mpr hev]
      have : e ≤ d := Nat.le_of_dvd hd0 he
      omega
    · have h2 : Nat.Coprime 2 e := (Nat.Prime.coprime_iff_not_dvd Nat.prime_two).mpr hev
      have hcop : Nat.Coprime e 4 := by
        have := Nat.Coprime.pow_left 2 h2
        norm_num at this
        exact this.symm
      have hem : e ∣ m := hcop.dvd_of_dvd_mul_left (he.trans hdvd)
      exact (le_F hm0.ne' hem he1).trans hF

/-! ## Counting -/

/-- `T(X, Y) = {n ≤ X : n odd, F n ≤ Y}`; for `X = x/y` and `Y = x` its size is Weingartner's
`A(x/y, y, 2)` (odd `n` are exactly those with `P⁻(n) > 2`, including `n = 1`). -/
def Tset (X Y : ℕ) : Finset ℕ := (Finset.Icc 1 X).filter (fun n => Odd n ∧ F n ≤ Y)

def Acnt (X Y : ℕ) : ℕ := (Tset X Y).card

/-- The `m` of Lemma 2: odd, `x/6 < m ≤ x/4`, `F m ≤ x`. -/
def Mset (x : ℕ) : Finset ℕ := Tset (x / 4) x \ Tset (x / 6) x

lemma Mset_mem {x m : ℕ} (hm : m ∈ Mset x) : 4 * m ≤ x ∧ x < 6 * m ∧ Odd m ∧ F m ≤ x := by
  obtain ⟨h4, h6⟩ := Finset.mem_sdiff.mp hm
  obtain ⟨hI, hodd, hF⟩ := Finset.mem_filter.mp h4
  rw [Finset.mem_Icc] at hI
  have h6' : ¬ (m ≤ x / 6) := fun h =>
    h6 (Finset.mem_filter.mpr ⟨Finset.mem_Icc.mpr ⟨hI.1, h⟩, hodd, hF⟩)
  exact ⟨by omega, by omega, hodd, hF⟩

/-- `N(x) = A(x/4, 4, 2) − A(x/6, 6, 2)`. -/
lemma Mset_card (x : ℕ) : (Mset x).card + Acnt (x / 6) x = Acnt (x / 4) x := by
  unfold Mset Acnt
  apply Finset.card_sdiff_add_card_eq_card
  intro n hn
  obtain ⟨hI, hP⟩ := Finset.mem_filter.mp hn
  rw [Finset.mem_Icc] at hI
  exact Finset.mem_filter.mpr ⟨Finset.mem_Icc.mpr ⟨hI.1, by omega⟩, hP⟩

/-- The elements `4m` contribute at least `N(x)/x` to `∑_{E(x)} 1/c`. -/
lemma Eset_sum_ge (x : ℕ) (hx : 0 < x) :
    ((Mset x).card : ℝ) / x ≤ ∑ c ∈ Eset x, (1 : ℝ) / c := by
  have hsub : (Mset x).image (fun m : ℕ => 4 * m) ⊆ Eset x := by
    intro n hn
    obtain ⟨m, hm, rfl⟩ := Finset.mem_image.mp hn
    obtain ⟨h4, h6, hodd, hF⟩ := Mset_mem hm
    exact four_mul_mem_E hodd h4 h6 hF
  have hinj : Set.InjOn (fun m : ℕ => 4 * m) (Mset x) := by
    intro a _ b _ h
    simp only at h
    omega
  have hxR : (0 : ℝ) < x := by exact_mod_cast hx
  calc ((Mset x).card : ℝ) / x = ∑ m ∈ Mset x, (1 : ℝ) / x := by
        rw [Finset.sum_const, nsmul_eq_mul]; ring
    _ ≤ ∑ m ∈ Mset x, (1 : ℝ) / (((4 * m : ℕ)) : ℝ) := by
        apply Finset.sum_le_sum
        intro m hm
        obtain ⟨h4, h6, hodd, hF⟩ := Mset_mem hm
        have hm0 : 0 < 4 * m := by have := hodd.pos; omega
        apply one_div_le_one_div_of_le
        · exact_mod_cast hm0
        · exact_mod_cast h4
    _ = ∑ c ∈ (Mset x).image (fun m : ℕ => 4 * m), (1 : ℝ) / (c : ℝ) :=
        (Finset.sum_image (f := fun c : ℕ => (1 : ℝ) / (c : ℝ)) hinj).symm
    _ ≤ ∑ c ∈ Eset x, (1 : ℝ) / (c : ℝ) :=
        Finset.sum_le_sum_of_subset_of_nonneg hsub (fun c _ _ => by positivity)

/-! ## The theorem

Weingartner's two analytic inputs enter as hypotheses, in a weaker form than he proves:
* `hB` ([W25, Theorem 2]): `∑_{B(x)} 1/n = 1 − δ/log x + O((log x)^{-3/2})`, used only as
  `∑_{B(x)} 1/n ≥ 1 − (δ + ε)/log x` eventually, for each `ε > 0`.
* `hA` ([W25, Theorem 10] at `(y, z) = (4, 2)` and `(6, 2)`):
  `A(x/y, y, 2) = 1 + π(√x, 2) + a_{y,2} (x/y)/log x · (1 + O(1/log x))`, used only with an error
  `ε x / log x`. The term `P x = 1 + π(√x, 2)` is common to both counts.
The numerical bounds `δ ≤ 0.560579`, `a_{4,2} ≥ 1.5152`, `a_{6,2} ≤ 1.9281` are from [W25,
Theorem 2 and Table 2]; Table 2 rounds to four decimals (our evaluation gives a_{4,2} = 1.51529…),
so we use 1.5152 rather than 1.5153. -/

theorem main (δ a4 a6 : ℝ) (hδ : δ ≤ 560579 / 1000000) (ha4 : 15152 / 10000 ≤ a4)
    (ha6 : a6 ≤ 19281 / 10000)
    (hB : ∀ ε > 0, ∀ᶠ x : ℕ in atTop,
      1 - (δ + ε) / Real.log (x : ℝ) ≤ ∑ n ∈ Bset x, (1 : ℝ) / n)
    (hA : ∃ P : ℕ → ℝ, ∀ ε > 0, ∀ᶠ x : ℕ in atTop,
      |(Acnt (x / 4) x : ℝ) - P x - a4 * ((x : ℝ) / 4) / Real.log (x : ℝ)|
          ≤ ε * x / Real.log (x : ℝ) ∧
      |(Acnt (x / 6) x : ℝ) - P x - a6 * ((x : ℝ) / 6) / Real.log (x : ℝ)|
          ≤ ε * x / Real.log (x : ℝ)) :
    ∀ᶠ x : ℕ in atTop, ∃ S ⊆ Finset.Icc 2 x, LcmFree x S ∧
      1 - (5032 / 10000) / Real.log (x : ℝ) ≤ ∑ n ∈ S, (1 : ℝ) / n := by
  obtain ⟨P, hA⟩ := hA
  filter_upwards [hB (3 / 100000) (by norm_num), hA (1 / 100000) (by norm_num),
    eventually_ge_atTop 3] with x hBx hAx hx3
  refine ⟨Sset x, Sset_sub hx3, Sset_lcm x, ?_⟩
  set L := Real.log (x : ℝ) with hLdef
  have hL : 0 < L := Real.log_pos (by exact_mod_cast (by omega : 1 < x))
  have hX : (0 : ℝ) < x := by exact_mod_cast (by omega : 0 < x)
  have hE := Eset_sum_ge x (by omega)
  have hM : ((Mset x).card : ℝ) = (Acnt (x / 4) x : ℝ) - Acnt (x / 6) x := by
    have := Mset_card x
    have : ((Mset x).card : ℝ) + Acnt (x / 6) x = Acnt (x / 4) x := by exact_mod_cast this
    linarith
  obtain ⟨h4, h6⟩ := hAx
  have h4' := (abs_le.mp h4).1
  have h6' := (abs_le.mp h6).2
  have hMlow : (a4 / 4 - a6 / 6 - 2 / 100000) / L ≤ ((Mset x).card : ℝ) / x := by
    rw [le_div_iff₀ hX]
    have e : (a4 / 4 - a6 / 6 - 2 / 100000) / L * x
        = a4 * ((x : ℝ) / 4) / L - a6 * ((x : ℝ) / 6) / L - 2 * ((1 / 100000) * x / L) := by
      ring
    rw [e, hM]
    linarith
  rw [Sset_sum]
  have hnum : δ + 3 / 100000 - (a4 / 4 - a6 / 6 - 2 / 100000) ≤ 5032 / 10000 := by linarith
  have hdiv : (δ + 3 / 100000 - (a4 / 4 - a6 / 6 - 2 / 100000)) / L ≤ (5032 / 10000) / L :=
    div_le_div_of_nonneg_right hnum hL.le
  have e2 : (δ + 3 / 100000 - (a4 / 4 - a6 / 6 - 2 / 100000)) / L
      = (δ + 3 / 100000) / L - (a4 / 4 - a6 / 6 - 2 / 100000) / L := by ring
  linarith

/-- Weingartner's Question 1 has a negative answer. Let `R x` be any upper bound for the reciprocal
sums of lcm-free subsets of `{2, …, x}` (for instance the maximum). Under the hypotheses of `main`,
`R x = 1 − (κ + o(1))/log x` fails for every `κ ≥ 0.543595`, in particular for Weingartner's
`κ ∈ (0.543595, 0.543804)`. -/
theorem not_question1 (δ a4 a6 : ℝ) (hδ : δ ≤ 560579 / 1000000) (ha4 : 15152 / 10000 ≤ a4)
    (ha6 : a6 ≤ 19281 / 10000)
    (hB : ∀ ε > 0, ∀ᶠ x : ℕ in atTop,
      1 - (δ + ε) / Real.log (x : ℝ) ≤ ∑ n ∈ Bset x, (1 : ℝ) / n)
    (hA : ∃ P : ℕ → ℝ, ∀ ε > 0, ∀ᶠ x : ℕ in atTop,
      |(Acnt (x / 4) x : ℝ) - P x - a4 * ((x : ℝ) / 4) / Real.log (x : ℝ)|
          ≤ ε * x / Real.log (x : ℝ) ∧
      |(Acnt (x / 6) x : ℝ) - P x - a6 * ((x : ℝ) / 6) / Real.log (x : ℝ)|
          ≤ ε * x / Real.log (x : ℝ))
    (κ : ℝ) (hκ : 543595 / 1000000 ≤ κ) (R : ℕ → ℝ)
    (hR : ∀ x S, S ⊆ Finset.Icc 2 x → LcmFree x S → ∑ n ∈ S, (1 : ℝ) / n ≤ R x)
    (f : ℕ → ℝ) (hf : Tendsto f atTop (nhds 0)) :
    ¬ ∀ᶠ x : ℕ in atTop, R x = 1 - (κ + f x) / Real.log (x : ℝ) := by
  intro hq
  have hm := main δ a4 a6 hδ ha4 ha6 hB hA
  have hf' : ∀ᶠ x in atTop, -(4 / 100) < f x := (tendsto_order.1 hf).1 _ (by norm_num)
  obtain ⟨x, ⟨S, hS, hSl, hsum⟩, hqx, hfx, hx3⟩ :=
    (hm.and (hq.and (hf'.and (eventually_ge_atTop 3)))).exists
  have hRx := hR x S hS hSl
  have hL : 0 < Real.log (x : ℝ) := Real.log_pos (by exact_mod_cast (by omega : 1 < x))
  rw [hqx] at hRx
  have h1 : (κ + f x) / Real.log (x : ℝ) ≤ (5032 / 10000) / Real.log (x : ℝ) := by linarith
  have h2 := (div_le_div_iff_of_pos_right hL).mp h1
  linarith

end WQ1
