Open Proof Network

Problems / erdos-1094 / erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2

under k^0.1 smooth elements, log n above log⁴k/(loglog k)⁶

erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2

open

Hole h_few of core with a second k-smooth element, n below √(k·lcm(1..k)) (split by 20261203T132804Z-t1203r2-k-partial.lean).

$\displaystyle \forall c>0\ \forall c_1>0\ \exists K\ \forall n,k:\ 0<k,\ k^3\le n,\ \exp(c\log^2 k)<n,\ \operatorname{lpf}\binom nk>\lfloor n/k\rfloor,\ \bigl(\forall p \text{ prime},\ k<p\le\lfloor n/k\rfloor,\ \forall m\in(n-k,n],\ p\nmid m\bigr),\ \prod_{m=n-k+1}^{n} s_k(m) = k!,\ \bigl(\forall m\in(n-k,n]:\ s_k(m)=m \text{ or } s_k(m)<k\bigr),\ \lfloor n/k\rfloor\mid\binom{k-1}{n \bmod k},\ n<k\,2^{k-1}+k,\ \bigl(\exists x\in(n-k,n]:\ x\ne k\lfloor n/k\rfloor,\ s_k(x)=x\bigr),\ (n-k)^2<k\prod_{p\le k \text{ prime}}p^{\lfloor\log_p k\rfloor},\ N^{10}<k,\ c_1\log^4 k<\log n\,(\log\log k)^6 \Rightarrow k\le K,\quad N=\#\{x\in(n-k,n]:\ s_k(x)=x\}$

Here $s_k(m)=\prod_{p\le k \text{ prime}}p^{v_p(m)}$ is the $k$-smooth part and $N$ the number of $k$-smooth elements of the window. The parent statement with two more hypotheses, the last two: $N<k^{1/10}$, and $\log n\,(\log\log k)^6$ exceeds $c_1\log^4 k$ for a second constant $c_1>0$ quantified before $K$. In the Lean, n / k is the floor, n % k the remainder, n - k truncated subtraction (here $k\le n$), Nat.log p k is $\lfloor\log_p k\rfloor$, minFac the least prime factor, Finset.Ioc (n - k) n the window, and the number of smooth elements is written as a sum of ones.

unverified · history

Lean
theorem erdos_1094__h2__h2__h5__h2__h2__h1__h1__h2 : ∀ (c : ℝ),
  (0 : ℝ) < c →
    ∀ (c₁ : ℝ),
      (0 : ℝ) < c₁ →
        ∃ (K : ℕ),
          ∀ (n k : ℕ),
            (0 : ℕ) < k →
              k * k * k ≤ n →
                Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) →
                  (n.choose k).minFac > n / k →
                    (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) →
                      ∏ m ∈ Finset.Ioc (n - k) n,
                            ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p =
                          k.factorial →
                        (∀ (m : ℕ),
                            n - k < m →
                              m ≤ n →
                                ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨
                                  ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) →
                          n / k ∣ (k - (1 : ℕ)).choose (n % k) →
                            n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k →
                              (∃ (x : ℕ),
                                  n - k < x ∧
                                    x ≤ n ∧
                                      x ≠ k * (n / k) ∧
                                        ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                          x) →
                                (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k →
                                  (∑ x ∈ Finset.Ioc (n - k) n with
                                          ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                            x,
                                          (1 : ℕ)) ^
                                        (10 : ℕ) <
                                      k →
                                    c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) <
                                        Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) →
                                      k ≤ K := by
  sorry
Why does it end in sorry?

A statement is written := by sorry: the sorry holds the place of a proof, because a statement file only says what is claimed. A proof the gate checked is its own file, Proof.lean. How to read a proof page →

Explainer

On the partial assembly targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean:

Informal account, unverified — unverified prose about a kernel-checked proof; shown section by section, each labelled with who drafted, wrote or verified it (D-3 v3.31).

Unverified: explainer, its words from explainer/70e42dd67793….md, written by t1203r2-k, drafted with Claude Opus 5.5 (claude-opus-5-5), 2026-12-03.

Draft · drafted, by the model named above

What this partial proof does

The statement it decomposes describes a would-be exception of Erdős's conjecture whose window $W=(n-k,n]$ holds few $k$-smooth elements: $N^{10}<k$, where $N$ is their number. The assembly proves it whenever $\log n\le\log^7k$, so the one remaining hole, h_few7, carries the extra hypothesis $\log^7k<\log n$.

Many moduli with a multiple in the window. For every $A$ there are $u_0,K_0$ such that, if $k\ge K_0$, $n\ge k^{u_0}$ and $\log n\le\log^Ak$, then $M$, the number of $d\in(k,2k]$ with $n\bmod d<k$, satisfies $M^{A+2}\ge k^{A+1}$. The certificates behind it are built from nonnegative cosine polynomials whose constant term is small against their value at $0$; those polynomials are constructed and proved in the file, so no result from the literature is assumed. (Stronger constructions were known: Belov and Konyagin, 1996, reach a power of the logarithm. The one here is weaker, a power saving, and is enough.) With $A=98$, $M^{100}\ge k^{99}$.

Few divisors. If $\log x\le\log^7k$ and $k$ is large, $x$ has $D$ divisors in $(k,2k]$ with $D^9<k^8$ (Rankin's method).

The count. If $d\in(k,2k]$ has the multiple $x\in W$, then $d$ has no prime factor above $k$, because no prime of $(k,n/k]$ divides a window element. So the $k$-smooth part of $x$ is at least $d>k$, and the parent's dichotomy makes $x$ itself $k$-smooth. Each of the $M$ moduli therefore divides one of the $N$ smooth elements, $M\le N\,k^{8/9}$, and $N\ge k^{99/100-8/9}\ge k^{1/10}$, against $N^{10}<k$.

Where it stops. The exponent 7 comes from the threshold $k^{1/10}$: an integer of size $e^{\log^Ak}$ may have about $k^{1-1/A}$ divisors in $(k,2k]$, so this count gives $N\ge k^{1/10}$ only for $A$ below about 10. The hole is the parent beyond that height and is not closed here.

The run certificates are due to the contributors t1203-a and t1203-b, their general form to agent K1, the cosine polynomials and the divisor count to agent K3, all of t1203r2-k.

Improve these words →

History: 1 version on record
  1. 2026-12-03 · written by t1203r2-k, drafted with Claude Opus 5.5 (claude-opus-5-5) · not signed · current · explainer/70e42dd67793….md · the first version of its chain

    Read this version

    What this partial proof does

    The statement it decomposes describes a would-be exception of Erdős's conjecture whose window $W=(n-k,n]$ holds few $k$-smooth elements: $N^{10}<k$, where $N$ is their number. The assembly proves it whenever $\log n\le\log^7k$, so the one remaining hole, h_few7, carries the extra hypothesis $\log^7k<\log n$.

    Many moduli with a multiple in the window. For every $A$ there are $u_0,K_0$ such that, if $k\ge K_0$, $n\ge k^{u_0}$ and $\log n\le\log^Ak$, then $M$, the number of $d\in(k,2k]$ with $n\bmod d<k$, satisfies $M^{A+2}\ge k^{A+1}$. The certificates behind it are built from nonnegative cosine polynomials whose constant term is small against their value at $0$; those polynomials are constructed and proved in the file, so no result from the literature is assumed. (Stronger constructions were known: Belov and Konyagin, 1996, reach a power of the logarithm. The one here is weaker, a power saving, and is enough.) With $A=98$, $M^{100}\ge k^{99}$.

    Few divisors. If $\log x\le\log^7k$ and $k$ is large, $x$ has $D$ divisors in $(k,2k]$ with $D^9<k^8$ (Rankin's method).

    The count. If $d\in(k,2k]$ has the multiple $x\in W$, then $d$ has no prime factor above $k$, because no prime of $(k,n/k]$ divides a window element. So the $k$-smooth part of $x$ is at least $d>k$, and the parent's dichotomy makes $x$ itself $k$-smooth. Each of the $M$ moduli therefore divides one of the $N$ smooth elements, $M\le N\,k^{8/9}$, and $N\ge k^{99/100-8/9}\ge k^{1/10}$, against $N^{10}<k$.

    Where it stops. The exponent 7 comes from the threshold $k^{1/10}$: an integer of size $e^{\log^Ak}$ may have about $k^{1-1/A}$ divisors in $(k,2k]$, so this count gives $N\ge k^{1/10}$ only for $A$ below about 10. The hole is the parent beyond that height and is not closed here.

    The run certificates are due to the contributors t1203-a and t1203-b, their general form to agent K1, the cosine polynomials and the divisor count to agent K3, all of t1203r2-k.

Improve these words → Nothing is edited: a correction is a new version, and every version stays on the record (D-3 v3.30).

How it is being proved

Open. A direct proof, or proofs of its holes, close it.

Split into

Reduced to this hole by 20261203T140457Z-t1203r2-k-partial.lean (merged in f5e3e8f95fc8, attestation 002858).

  • h_few7 → under k^0.1 smooth elements, log n above log⁷k (open)

    $\displaystyle \forall c>0\ \forall c_1>0\ \exists K\ \forall n,k:\ 0<k,\ k^3\le n,\ \exp(c\log^2 k)<n,\ \operatorname{lpf}\binom nk>\lfloor n/k\rfloor,\ \bigl(\forall p \text{ prime},\ k<p\le\lfloor n/k\rfloor,\ \forall m\in(n-k,n],\ p\nmid m\bigr),\ \prod_{m=n-k+1}^{n} s_k(m) = k!,\ \bigl(\forall m\in(n-k,n]:\ s_k(m)=m \text{ or } s_k(m)<k\bigr),\ \lfloor n/k\rfloor\mid\binom{k-1}{n \bmod k},\ n<k\,2^{k-1}+k,\ \bigl(\exists x\in(n-k,n]:\ x\ne k\lfloor n/k\rfloor,\ s_k(x)=x\bigr),\ (n-k)^2<k\prod_{p\le k \text{ prime}}p^{\lfloor\log_p k\rfloor},\ N^{10}<k,\ c_1\log^4 k<\log n\,(\log\log k)^6,\ \log^7 k<\log n \Rightarrow k\le K,\quad N=\#\{x\in(n-k,n]:\ s_k(x)=x\}$

Read the partial proof (Lean)
/-! `many_landing` is t1203-b's t1_landing without the deficiency-one hypothesis; `t1_kpow` is t1203-b's. -/

private theorem many_landing (n k : ℕ) (hk : 0 < k) (hkn : k ≤ n)
    (hfree : ∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m)
    (d : ℕ) (hd1 : k < d) (hd2 : d ≤ n / k) (hd3 : n % d < k) :
    k < ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ ((n - n % d).factorization : ℕ → ℕ) p := by
  have hd0 : 0 < d := by omega
  -- the multiple of d in the window
  have hxd : d ∣ n - n % d := Nat.dvd_sub_mod n
  have hmodle : n % d ≤ n := Nat.mod_le n d
  have hx1 : n - k < n - n % d := by omega
  have hx2 : n - n % d ≤ n := Nat.sub_le _ _
  have hx0 : n - n % d ≠ 0 := by omega
  generalize hxdef : n - n % d = x at hxd hx1 hx2 hx0
  set S := (Finset.Icc 1 k).filter Nat.Prime with hS
  set s := ∏ p ∈ S, p ^ (x.factorization p) with hsdef
  set r := ∏ p ∈ x.primeFactors.filter (fun p => k < p), p ^ (x.factorization p) with hr
  -- x = s * r
  have hsplit : x = s * r := by
    have h1 : x = ∏ p ∈ x.primeFactors, p ^ (x.factorization p) :=
      (Nat.prod_factorization_pow_eq_self hx0).symm.trans
        (by rw [Nat.prod_factorization_eq_prod_primeFactors])
    have h2 : (∏ p ∈ x.primeFactors, p ^ (x.factorization p)) =
        (∏ p ∈ x.primeFactors.filter (fun p => p ≤ k), p ^ (x.factorization p)) *
        (∏ p ∈ x.primeFactors.filter (fun p => ¬ p ≤ k), p ^ (x.factorization p)) :=
      (Finset.prod_filter_mul_prod_filter_not _ _ _).symm
    have h3 : (∏ p ∈ x.primeFactors.filter (fun p => p ≤ k), p ^ (x.factorization p)) = s := by
      rw [hsdef]
      apply Finset.prod_subset
      · intro p hp
        rw [Finset.mem_filter, Nat.mem_primeFactors] at hp
        exact Finset.mem_filter.mpr ⟨Finset.mem_Icc.mpr ⟨hp.1.1.one_le, hp.2⟩, hp.1.1⟩
      · intro p hpS hpn
        rw [Finset.mem_filter, Nat.mem_primeFactors] at hpn
        have hp : p.Prime := (Finset.mem_filter.mp hpS).2
        have hpk : p ≤ k := (Finset.mem_Icc.mp (Finset.mem_filter.mp hpS).1).2
        have : ¬ p ∣ x := fun h => hpn ⟨⟨hp, h, hx0⟩, hpk⟩
        rw [Nat.factorization_eq_zero_of_not_dvd this, pow_zero]
    have h4 : x.primeFactors.filter (fun p => ¬ p ≤ k) = x.primeFactors.filter (fun p => k < p) := by
      apply Finset.filter_congr; intro p _; exact not_le
    rw [h1, h2, h3, h4]
  -- d is coprime to r: a common prime would be a prime of (k, n/k] dividing x
  have hcop : Nat.Coprime d r := by
    apply Nat.coprime_of_dvd
    intro q hq hqd hqr
    have hkq : k < q := by
      rw [hr] at hqr
      obtain ⟨p, hp, hqp⟩ := (Prime.dvd_finsetProd_iff hq.prime _).mp hqr
      have hpk : k < p := (Finset.mem_filter.mp hp).2
      have hpp : p.Prime := (Nat.mem_primeFactors.mp (Finset.mem_filter.mp hp).1).1
      have : q = p := (Nat.prime_dvd_prime_iff_eq hq hpp).mp (hq.dvd_of_dvd_pow hqp)
      omega
    have hqle : q ≤ n / k := le_trans (Nat.le_of_dvd hd0 hqd) hd2
    exact hfree q hq hkq hqle x hx1 hx2 (hqd.trans hxd)
  have hds : d ∣ s := hcop.dvd_of_dvd_mul_right (hsplit ▸ hxd)
  have hspos : 0 < s := by
    rw [hsdef]
    apply Finset.prod_pos
    intro p hp
    exact pow_pos (Finset.mem_filter.mp hp).2.pos _
  have hle : d ≤ s := Nat.le_of_dvd hspos hds
  omega

/-- For `k ≥ exp(u₀/c)`, `exp(c·log²k) < n` gives `k^u₀ ≤ n`. -/
private theorem t1_kpow (c : ℝ) (hc : 0 < c) (u₀ n k : ℕ) (hk : 0 < k)
    (hbig : ⌈Real.exp ((u₀ : ℝ) / c)⌉₊ ≤ k)
    (hexp : Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ)) : k ^ u₀ ≤ n := by
  have hkpos : (0 : ℝ) < (k : ℝ) := by exact_mod_cast hk
  have hk' : Real.exp ((u₀ : ℝ) / c) ≤ (k : ℝ) := le_trans (Nat.le_ceil _) (by exact_mod_cast hbig)
  have hlog : (u₀ : ℝ) / c ≤ Real.log (k : ℝ) := by
    have := Real.log_le_log (Real.exp_pos _) hk'
    rwa [Real.log_exp] at this
  have h21 : (u₀ : ℝ) ≤ c * Real.log (k : ℝ) := by
    rw [div_le_iff₀ hc] at hlog
    linarith
  have hlk : 0 ≤ Real.log (k : ℝ) := Real.log_nonneg (by exact_mod_cast hk)
  have h1 : ((k : ℝ)) ^ u₀ = Real.exp ((u₀ : ℝ) * Real.log (k : ℝ)) := by
    rw [Real.exp_nat_mul, Real.exp_log hkpos]
  have h2 : Real.exp ((u₀ : ℝ) * Real.log (k : ℝ)) ≤ Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) := by
    apply Real.exp_le_exp.mpr
    nlinarith [mul_le_mul_of_nonneg_right h21 hlk]
  have h3 : ((k : ℝ)) ^ u₀ < (n : ℝ) := by
    rw [h1]
    exact lt_of_le_of_lt h2 hexp
  exact_mod_cast h3.le



/-- The covering count with general exponents, at A = 7 and M = 100. -/
private lemma many_count7 (S T : Finset ℕ) (k : ℕ)
    (hT : ∀ d ∈ T, ∃ x ∈ S, d ∣ x)
    (hD : ∀ x ∈ S, (Finset.card (Finset.filter (fun (d : ℕ) => d ∣ x) T)) ^ (9 : ℕ) < k ^ (8 : ℕ))
    (hN : k ^ (99 : ℕ) ≤ T.card ^ (100 : ℕ)) (hk : 0 < k) : k ≤ S.card ^ (10 : ℕ) := by
  classical
  have hsub : T ⊆ S.biUnion (fun x => Finset.filter (fun (d : ℕ) => d ∣ x) T) := by
    intro d hd
    obtain ⟨x, hx, hdx⟩ := hT d hd
    exact Finset.mem_biUnion.mpr ⟨x, hx, Finset.mem_filter.mpr ⟨hd, hdx⟩⟩
  set D := S.sup (fun x => Finset.card (Finset.filter (fun (d : ℕ) => d ∣ x) T)) with hDdef
  have h1 : T.card ≤ S.card * D := by
    refine (Finset.card_le_card hsub).trans (Finset.card_biUnion_le_card_mul _ _ _ ?_)
    intro x hx
    exact Finset.le_sup (f := fun x => Finset.card (Finset.filter (fun (d : ℕ) => d ∣ x) T)) hx
  have h2 : D ^ 9 ≤ k ^ 8 := by
    rcases S.eq_empty_or_nonempty with h | h
    · simp [hDdef, h]
    · obtain ⟨x, hx, he⟩ := Finset.exists_mem_eq_sup S h
        (fun x => Finset.card (Finset.filter (fun (d : ℕ) => d ∣ x) T))
      rw [hDdef, he]
      exact (hD x hx).le
  generalize S.card = s at *
  generalize T.card = t at *
  have h3 : k ^ 800 * k ^ 91 ≤ k ^ 800 * s ^ 900 :=
    calc k ^ 800 * k ^ 91 = (k ^ 99) ^ 9 := by rw [← pow_add, ← pow_mul]
      _ ≤ (t ^ 100) ^ 9 := Nat.pow_le_pow_left hN 9
      _ ≤ ((s * D) ^ 100) ^ 9 := Nat.pow_le_pow_left (Nat.pow_le_pow_left h1 100) 9
      _ = (D ^ 9) ^ 100 * s ^ 900 := by rw [mul_pow, mul_pow, ← pow_mul, ← pow_mul, ← pow_mul, mul_comm]
      _ ≤ (k ^ 8) ^ 100 * s ^ 900 := Nat.mul_le_mul_right _ (Nat.pow_le_pow_left h2 100)
      _ = k ^ 800 * s ^ 900 := by rw [← pow_mul]
  have h4 : k ^ 91 ≤ s ^ 900 := Nat.le_of_mul_le_mul_left h3 (Nat.pow_pos hk)
  by_contra hc
  have h5 : s ^ 10 + 1 ≤ k := by omega
  rcases Nat.eq_zero_or_pos s with h0 | h0
  · subst h0
    have : 0 < k ^ 91 := Nat.pow_pos hk
    rw [zero_pow (by norm_num)] at h4
    omega
  · have h6 : (s ^ 10) ^ 91 < k ^ 91 := Nat.pow_lt_pow_left (by omega) (by norm_num)
    have h7 : s ^ 900 ≤ (s ^ 10) ^ 91 := by
      rw [← pow_mul]
      exact Nat.pow_le_pow_right h0 (by norm_num)
    exact absurd (lt_of_le_of_lt (h4.trans h7) h6) (lt_irrefl _)

/-! T1 for every power of log k: run certificates (t1203-a, t1203-b), general window and density (agent K1), cosine polynomials and divisor count (agent K3). -/

/-! NOT FILED. Joined file for one fast check: A/t1_general.lean (agent A, t1203-a, copied verbatim at 12:19Z, sha256 be54ca1f6015…)
followed by B/t1_analytic.lean (agent B, t1203-b) and the joining theorem at the end. -/


/-!
Part (a) of B's T1 (`B/T1-proof.md` §1–§2 and the scaling (1.3)) for ANY list of steps, on the
fast check only. No node. Repeated steps are allowed.

Notation: `S : List ℕ` the steps `s_1, …, s_ρ`; `c = ∏ (1 - z^s)` with coefficients `coefL S i`;
`N = S.sum`, `ρ = S.length`, `β = S.prod`, norm `A = Σ_{i ≤ N} |coefL S i|`;
`gfun ρ x = ρ!/(x(x+1)⋯(x+ρ))`; `d` is bad for `(n, k)` when `k ≤ n % d`.

* `cert_general`: Lemma 2 for an ARBITRARY integer vector `c` on `0..N` with `Σ c i = 0` (the
  product structure is not used there).
* `lemma1_shifts`, `lemma1_bounds`: Lemma 1 and (2.1) without the integral: the iterated
  difference `Dl S (1/x) = Δ_{s_1} … Δ_{s_ρ} (1/x)` is a sum, over a list of `β` shifts `j` with
  `j + ρ ≤ N`, of `gfun ρ (x + j)`; hence `β·g_ρ(x+N-ρ) ≤ Dl S (1/x) ≤ β·g_ρ(x)`.
* `coefL_sum`, `Dl_seqD`: `Σ_i coefL S i / (x + i) = Dl S (1/x)`.
* `coefL_eq_coeff`: `coefL S i` is the coefficient of `X^i` in `∏ (1 - X^s) : Polynomial ℤ`.
* `no_bad_translate`: Proposition 3.
* `coefL_scale`, `norm_scale`: (1.3), scaling the steps by `g` keeps the norm.
* `no_bad_translate_scaled`: Proposition 3 after `z ↦ z^g`, with the unscaled norm.

Not here: any bound on the norm `A` of a concrete family (§3A (A4), §3B (B0)), the choice of
`ρ` and `g` (Lemma 5, 5′), Rankin's count (Lemma 7).
-/

/-- Lemma 2 of `B/T1-proof.md` for an arbitrary coefficient vector with sum zero: if `m + i` is
bad (`k ≤ n % (m + i)`) whenever `c i ≠ 0`, the integer `Σ c i · (⌊n/(m+i)⌋ + 1)` is within
`(Σ|c i|)/2 · (m+N-k)/(m+N)` of `n · Σ c i/(m+i)`. -/
private theorem cert_general (n k m N : ℕ) (c : ℕ → ℤ) (hk : 0 < k) (hm : k < m)
    (hsum : ∑ i ∈ Finset.range (N + 1), c i = 0)
    (hbad : ∀ i ≤ N, c i ≠ 0 → k ≤ n % (m + i)) :
    |((∑ i ∈ Finset.range (N + 1), c i * ((n / (m + i) + 1 : ℕ) : ℤ) : ℤ) : ℚ)
        - (n : ℚ) * ∑ i ∈ Finset.range (N + 1), (c i : ℚ) / ((m : ℚ) + (i : ℚ))|
      ≤ (∑ i ∈ Finset.range (N + 1), |(c i : ℚ)|) / 2
          * (((m + N - k : ℕ) : ℚ) / ((m + N : ℕ) : ℚ)) := by
  have hmq : (0 : ℚ) < (m : ℚ) := by exact_mod_cast (by omega : 0 < m)
  have hkq : (0 : ℚ) < (k : ℚ) := by exact_mod_cast hk
  have hkm : (k : ℚ) < (m : ℚ) := by exact_mod_cast hm
  obtain ⟨e, he_def⟩ : ∃ e : ℕ → ℚ, ∀ i : ℕ,
      e i = ((n / (m + i) + 1 : ℕ) : ℚ) - (n : ℚ) / ((m : ℚ) + (i : ℚ)) := ⟨_, fun _ => rfl⟩
  obtain ⟨ε, hε⟩ : ∃ ε : ℚ, ε = 1 - (k : ℚ) / ((m : ℚ) + (N : ℚ)) := ⟨_, rfl⟩
  have he : ∀ i : ℕ, i ≤ N → c i ≠ 0 → 0 < e i ∧ e i ≤ ε := by
    intro i hi hci
    have hd : 0 < m + i := by omega
    have hdq : (0 : ℚ) < (m : ℚ) + (i : ℚ) := by positivity
    have hρ1 : ((k : ℕ) : ℚ) ≤ ((n % (m + i) : ℕ) : ℚ) := by exact_mod_cast hbad i hi hci
    have hρ2 : ((n % (m + i) : ℕ) : ℚ) < (m : ℚ) + (i : ℚ) := by
      exact_mod_cast Nat.mod_lt n hd
    have hn : (n : ℚ) = ((m : ℚ) + (i : ℚ)) * ((n / (m + i) : ℕ) : ℚ) + ((n % (m + i) : ℕ) : ℚ) := by
      exact_mod_cast (Nat.div_add_mod n (m + i)).symm
    have hnd : (n : ℚ) / ((m : ℚ) + (i : ℚ))
        = ((n / (m + i) : ℕ) : ℚ) + ((n % (m + i) : ℕ) : ℚ) / ((m : ℚ) + (i : ℚ)) := by
      rw [div_eq_iff hdq.ne', add_mul, div_mul_cancel₀ _ hdq.ne']
      linarith
    have hee : e i = 1 - ((n % (m + i) : ℕ) : ℚ) / ((m : ℚ) + (i : ℚ)) := by
      rw [he_def i, hnd]
      push_cast
      ring
    have h1 : ((n % (m + i) : ℕ) : ℚ) / ((m : ℚ) + (i : ℚ)) < 1 := (div_lt_one hdq).mpr hρ2
    have h2 : (k : ℚ) / ((m : ℚ) + (i : ℚ)) ≤ ((n % (m + i) : ℕ) : ℚ) / ((m : ℚ) + (i : ℚ)) :=
      div_le_div_of_nonneg_right hρ1 hdq.le
    have hir : (m : ℚ) + (i : ℚ) ≤ (m : ℚ) + (N : ℚ) := by
      have : (i : ℚ) ≤ (N : ℚ) := by exact_mod_cast hi
      linarith
    have h3 : (k : ℚ) / ((m : ℚ) + (N : ℚ)) ≤ (k : ℚ) / ((m : ℚ) + (i : ℚ)) :=
      div_le_div_of_nonneg_left hkq.le hdq hir
    rw [hee, hε]
    constructor <;> linarith
  have hsumq : ∑ i ∈ Finset.range (N + 1), (c i : ℚ) = 0 := by exact_mod_cast hsum
  have hkey : ((∑ i ∈ Finset.range (N + 1), c i * ((n / (m + i) + 1 : ℕ) : ℤ) : ℤ) : ℚ)
        - (n : ℚ) * ∑ i ∈ Finset.range (N + 1), (c i : ℚ) / ((m : ℚ) + (i : ℚ))
      = ∑ i ∈ Finset.range (N + 1), (c i : ℚ) * (e i - ε / 2) := by
    have h1 : ∀ i ∈ Finset.range (N + 1), (c i : ℚ) * (e i - ε / 2)
        = (c i : ℚ) * ((n / (m + i) + 1 : ℕ) : ℚ)
          - (n : ℚ) * ((c i : ℚ) / ((m : ℚ) + (i : ℚ)))
          - ε / 2 * (c i : ℚ) := by
      intro i _
      rw [he_def i]
      ring
    have hTcast : ((∑ i ∈ Finset.range (N + 1), c i * ((n / (m + i) + 1 : ℕ) : ℤ) : ℤ) : ℚ)
        = ∑ i ∈ Finset.range (N + 1), (c i : ℚ) * ((n / (m + i) + 1 : ℕ) : ℚ) := by
      rw [Int.cast_sum]
      apply Finset.sum_congr rfl
      intro i _
      rw [Int.cast_mul, Int.cast_natCast]
    rw [Finset.sum_congr rfl h1, Finset.sum_sub_distrib, Finset.sum_sub_distrib,
      ← Finset.mul_sum, ← Finset.mul_sum, hsumq, hTcast]
    ring
  rw [hkey]
  have hterm : ∀ i ∈ Finset.range (N + 1),
      |(c i : ℚ) * (e i - ε / 2)| ≤ |(c i : ℚ)| * (ε / 2) := by
    intro i hi
    have hiN : i ≤ N := Nat.lt_succ_iff.mp (Finset.mem_range.mp hi)
    by_cases hci : c i = 0
    · simp [hci]
    · obtain ⟨h1, h2⟩ := he i hiN hci
      have h3 : |e i - ε / 2| ≤ ε / 2 := abs_le.mpr ⟨by linarith, by linarith⟩
      rw [abs_mul]
      exact mul_le_mul_of_nonneg_left h3 (abs_nonneg _)
  have hrhs : (∑ i ∈ Finset.range (N + 1), |(c i : ℚ)|) / 2
        * (((m + N - k : ℕ) : ℚ) / ((m + N : ℕ) : ℚ))
      = ∑ i ∈ Finset.range (N + 1), |(c i : ℚ)| * (ε / 2) := by
    have hmNq : (0 : ℚ) < (m : ℚ) + (N : ℚ) := by positivity
    rw [← Finset.sum_mul, hε, Nat.cast_sub (by omega)]
    push_cast
    field_simp
  rw [hrhs]
  exact (Finset.abs_sum_le_sum_abs _ _).trans (Finset.sum_le_sum hterm)

/-- `gfun ρ x = ρ!/(x(x+1)⋯(x+ρ))`. -/
private def gfun (ρ : ℕ) (x : ℚ) : ℚ := (ρ.factorial : ℚ) / ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ))

/-- One difference raises the order: `g_ρ(x) - g_ρ(x+1) = g_{ρ+1}(x)`. -/
private theorem gfun_step (ρ : ℕ) (x : ℚ) (hx : 0 < x) :
    gfun ρ x - gfun ρ (x + 1) = gfun (ρ + 1) x := by
  unfold gfun
  have hM : (0 : ℚ) < ∏ i ∈ Finset.range ρ, (x + 1 + (i : ℚ)) :=
    Finset.prod_pos (fun i _ => by positivity)
  have hP1 : ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ))
      = (∏ i ∈ Finset.range ρ, (x + 1 + (i : ℚ))) * x := by
    rw [Finset.prod_range_succ']
    congr 1
    · apply Finset.prod_congr rfl
      intro i _
      push_cast
      ring
    · simp
  have hP2 : ∏ i ∈ Finset.range (ρ + 1), (x + 1 + (i : ℚ))
      = (∏ i ∈ Finset.range ρ, (x + 1 + (i : ℚ))) * (x + 1 + (ρ : ℚ)) :=
    Finset.prod_range_succ _ _
  have hP3 : ∏ i ∈ Finset.range (ρ + 1 + 1), (x + (i : ℚ))
      = (∏ i ∈ Finset.range ρ, (x + 1 + (i : ℚ))) * x * (x + 1 + (ρ : ℚ)) := by
    rw [Finset.prod_range_succ, hP1]
    push_cast
    ring
  rw [hP1, hP2, hP3, Nat.factorial_succ]
  have hxr : (0 : ℚ) < x + 1 + (ρ : ℚ) := by positivity
  push_cast
  field_simp
  ring

/-- Telescoping: `g_ρ(x) - g_ρ(x+s) = Σ_{i<s} g_{ρ+1}(x+i)`. -/
private theorem gfun_tele (ρ s : ℕ) (x : ℚ) (hx : 0 < x) :
    gfun ρ x - gfun ρ (x + (s : ℚ)) = ∑ i ∈ Finset.range s, gfun (ρ + 1) (x + (i : ℚ)) := by
  induction s with
  | zero => simp
  | succ s ih =>
    have hxs : 0 < x + (s : ℚ) := by positivity
    have e1 : x + ((s + 1 : ℕ) : ℚ) = x + (s : ℚ) + 1 := by
      push_cast
      ring
    rw [Finset.sum_range_succ, ← ih, ← gfun_step ρ (x + (s : ℚ)) hxs, e1]
    ring

private theorem gfun_pos (ρ : ℕ) (x : ℚ) (hx : 0 < x) : 0 < gfun ρ x := by
  unfold gfun
  have hP : (0 : ℚ) < ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) :=
    Finset.prod_pos (fun i _ => by positivity)
  have hf : (0 : ℚ) < (ρ.factorial : ℚ) := by exact_mod_cast Nat.factorial_pos ρ
  exact div_pos hf hP

private theorem gfun_anti (ρ : ℕ) (x y : ℚ) (hx : 0 < x) (hxy : x ≤ y) : gfun ρ y ≤ gfun ρ x := by
  unfold gfun
  have hP : (0 : ℚ) < ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) :=
    Finset.prod_pos (fun i _ => by positivity)
  have hle : ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) ≤ ∏ i ∈ Finset.range (ρ + 1), (y + (i : ℚ)) :=
    Finset.prod_le_prod (fun i _ => by positivity) (fun i _ => by linarith)
  exact div_le_div_of_nonneg_left (Nat.cast_nonneg _) hP hle

/-- The iterated difference `Δ_{s_1} … Δ_{s_ρ} f`, where `Δ_s f (x) = f x - f (x + s)`. -/
private def Dl : List ℕ → (ℚ → ℚ) → ℚ → ℚ
  | [], f => f
  | s :: S, f => fun x => Dl S f x - Dl S f (x + (s : ℚ))

private theorem sum_flatMap_aux (L : List ℕ) (F : ℕ → List ℕ) (h : ℕ → ℚ) :
    ((L.flatMap F).map h).sum = (L.map (fun j => ((F j).map h).sum)).sum := by
  induction L with
  | nil => simp
  | cons a L ih => simp [List.flatMap_cons, ih]

private theorem sum_map_sub_aux (L : List ℕ) (a b : ℕ → ℚ) :
    (L.map a).sum - (L.map b).sum = (L.map (fun j => a j - b j)).sum := by
  induction L with
  | nil => simp
  | cons x L ih =>
    simp only [List.map_cons, List.sum_cons]
    rw [← ih]
    ring

private theorem sum_range_list (s : ℕ) (h : ℕ → ℚ) :
    ((List.range s).map h).sum = ∑ i ∈ Finset.range s, h i := by
  induction s with
  | zero => simp
  | succ s ih =>
    rw [List.range_succ, List.map_append, List.sum_append, ih, Finset.sum_range_succ]
    simp

private theorem list_sum_bounds (L : List ℕ) (h : ℕ → ℚ) (a b : ℚ)
    (hb : ∀ j ∈ L, a ≤ h j ∧ h j ≤ b) :
    (L.length : ℚ) * a ≤ (L.map h).sum ∧ (L.map h).sum ≤ (L.length : ℚ) * b := by
  induction L with
  | nil => simp
  | cons j L ih =>
    have h1 := hb j (by simp)
    have h2 := ih (fun i hi => hb i (by simp [hi]))
    simp only [List.map_cons, List.sum_cons, List.length_cons]
    push_cast
    constructor <;> nlinarith [h1.1, h1.2, h2.1, h2.2]

/-- Lemma 1 of `B/T1-proof.md` without the integral: `Δ_{s_1} … Δ_{s_ρ} (1/x)` is a sum of
`∏ s_t` shifted copies of `g_ρ`, every shift `j` having `j + ρ ≤ Σ s_t`. -/
private theorem lemma1_shifts (S : List ℕ) :
    ∃ L : List ℕ, L.length = S.prod ∧ (∀ j ∈ L, j + S.length ≤ S.sum) ∧
      ∀ x : ℚ, 0 < x →
        Dl S (fun y => 1 / y) x = (L.map (fun j : ℕ => gfun S.length (x + (j : ℚ)))).sum := by
  induction S with
  | nil =>
    refine ⟨[0], by simp, by simp, ?_⟩
    intro x hx
    simp [Dl, gfun]
  | cons s S ih =>
    obtain ⟨L, hlen, hbd, hval⟩ := ih
    refine ⟨L.flatMap (fun j => (List.range s).map (fun i => j + i)), ?_, ?_, ?_⟩
    · rw [List.length_flatMap]
      simp [hlen, mul_comm]
    · intro j hj
      rw [List.mem_flatMap] at hj
      obtain ⟨j0, hj0, hj1⟩ := hj
      rw [List.mem_map] at hj1
      obtain ⟨i, hi, rfl⟩ := hj1
      have hi' : i < s := List.mem_range.mp hi
      have := hbd j0 hj0
      simp only [List.length_cons, List.sum_cons]
      omega
    · intro x hx
      have hxs : 0 < x + (s : ℚ) := by positivity
      show Dl S (fun y => 1 / y) x - Dl S (fun y => 1 / y) (x + (s : ℚ)) = _
      rw [hval x hx, hval (x + (s : ℚ)) hxs, sum_map_sub_aux, sum_flatMap_aux]
      congr 1
      apply List.map_congr_left
      intro j _
      rw [List.map_map, sum_range_list]
      have hxj : 0 < x + (j : ℚ) := by positivity
      have h := gfun_tele S.length s (x + (j : ℚ)) hxj
      have e1 : x + (s : ℚ) + (j : ℚ) = x + (j : ℚ) + (s : ℚ) := by ring
      rw [e1, h]
      apply Finset.sum_congr rfl
      intro i _
      simp only [Function.comp_apply, List.length_cons]
      congr 1
      push_cast
      ring

/-- (2.1) of `B/T1-proof.md`: with `ρ` the number of steps, `β` their product and `N` their sum,
`β · g_ρ(x + N - ρ) ≤ Δ_{s_1} … Δ_{s_ρ} (1/x) ≤ β · g_ρ(x)` for `x > 0`. -/
private theorem lemma1_bounds (S : List ℕ) (x : ℚ) (hx : 0 < x) :
    (S.prod : ℚ) * gfun S.length (x + ((S.sum - S.length : ℕ) : ℚ)) ≤ Dl S (fun y => 1 / y) x ∧
      Dl S (fun y => 1 / y) x ≤ (S.prod : ℚ) * gfun S.length x := by
  obtain ⟨L, hlen, hbd, hval⟩ := lemma1_shifts S
  rw [hval x hx, ← hlen]
  apply list_sum_bounds
  intro j hj
  have hj' := hbd j hj
  have hjq : (j : ℚ) ≤ ((S.sum - S.length : ℕ) : ℚ) := by exact_mod_cast (by omega : j ≤ S.sum - S.length)
  have hj0 : (0 : ℚ) ≤ (j : ℚ) := Nat.cast_nonneg j
  constructor
  · exact gfun_anti S.length (x + (j : ℚ)) (x + ((S.sum - S.length : ℕ) : ℚ)) (by positivity) (by linarith)
  · exact gfun_anti S.length x (x + (j : ℚ)) hx (by linarith)

/-- Coefficients of `∏ (1 - z^s)` over the list of steps: `coefL S i` is the coefficient of
`z^i`. -/
private def coefL : List ℕ → ℕ → ℤ
  | [], i => if i = 0 then 1 else 0
  | s :: S, i => coefL S i - (if s ≤ i then coefL S (i - s) else 0)

/-- The iterated difference on sequences: `seqD S f = Σ_i (coefL S i) · f i`. -/
private def seqD : List ℕ → (ℕ → ℚ) → ℚ
  | [], f => f 0
  | s :: S, f => seqD S f - seqD S (fun i => f (i + s))

private theorem coefL_sum (S : List ℕ) : ∀ (f : ℕ → ℚ) (N : ℕ), S.sum ≤ N →
    ∑ i ∈ Finset.range (N + 1), (coefL S i : ℚ) * f i = seqD S f := by
  induction S with
  | nil =>
    intro f N _
    rw [Finset.sum_eq_single 0]
    · simp [coefL, seqD]
    · intro i _ hi
      simp [coefL, hi]
    · intro h
      exact absurd (Finset.mem_range.mpr (Nat.succ_pos N)) h
  | cons s S ih =>
    intro f N hN
    rw [List.sum_cons] at hN
    have h1 : ∀ i ∈ Finset.range (N + 1), (coefL (s :: S) i : ℚ) * f i
        = (coefL S i : ℚ) * f i - (if s ≤ i then (coefL S (i - s) : ℚ) * f i else 0) := by
      intro i _
      rw [show coefL (s :: S) i = coefL S i - (if s ≤ i then coefL S (i - s) else 0) from rfl]
      split_ifs <;> push_cast <;> ring
    rw [Finset.sum_congr rfl h1, Finset.sum_sub_distrib, ih f N (by omega)]
    have h2 : ∑ i ∈ Finset.range (N + 1), (if s ≤ i then (coefL S (i - s) : ℚ) * f i else 0)
        = ∑ j ∈ Finset.range (N - s + 1), (coefL S j : ℚ) * f (j + s) := by
      have hNs : N + 1 = s + (N - s + 1) := by omega
      rw [hNs, Finset.sum_range_add]
      have hz : ∑ i ∈ Finset.range s, (if s ≤ i then (coefL S (i - s) : ℚ) * f i else 0) = 0 := by
        apply Finset.sum_eq_zero
        intro i hi
        have hlt : ¬ s ≤ i := by
          have := Finset.mem_range.mp hi
          omega
        rw [if_neg hlt]
      rw [hz, zero_add]
      apply Finset.sum_congr rfl
      intro j _
      have hle : s ≤ s + j := Nat.le_add_right s j
      rw [if_pos hle, Nat.add_sub_cancel_left, add_comm s j]
    rw [h2, ih (fun i => f (i + s)) (N - s) (by omega)]
    rfl

private theorem Dl_seqD (S : List ℕ) : ∀ (g : ℚ → ℚ) (x : ℚ),
    Dl S g x = seqD S (fun i => g (x + (i : ℚ))) := by
  induction S with
  | nil =>
    intro g x
    simp [Dl, seqD]
  | cons s S ih =>
    intro g x
    show Dl S g x - Dl S g (x + (s : ℚ))
      = seqD S (fun i => g (x + (i : ℚ))) - seqD S (fun i => g (x + ((i + s : ℕ) : ℚ)))
    rw [ih g x, ih g (x + (s : ℚ))]
    have e : (fun i : ℕ => g (x + (s : ℚ) + (i : ℚ))) = (fun i : ℕ => g (x + ((i + s : ℕ) : ℚ))) := by
      funext i
      congr 1
      push_cast
      ring
    rw [e]

private theorem seqD_const (s : ℕ) (S : List ℕ) (a : ℚ) : seqD (s :: S) (fun _ => a) = 0 := by
  show seqD S (fun _ => a) - seqD S (fun _ => a) = 0
  exact sub_self _

/-- Proposition 3 of `B/T1-proof.md` for any nonempty list of steps `S` (repeats allowed), with
`N = S.sum`, `ρ = S.length`, `β = S.prod` and the norm `A = Σ |coefL S i|`: if
`2·A·(m+N-k) < m+N`, `1/4 ≤ n·β·g_ρ(m+N-ρ)` and `n·β·g_ρ(m) ≤ 3/4`, then some `m + i` with
`coefL S i ≠ 0`, `i ≤ N`, is not bad. -/
private theorem no_bad_translate (n k m : ℕ) (S : List ℕ) (hS : S ≠ []) (hk : 0 < k) (hm : k < m)
    (hsmall : 2 * (∑ i ∈ Finset.range (S.sum + 1), |(coefL S i : ℚ)|) * ((m + S.sum - k : ℕ) : ℚ)
      < ((m + S.sum : ℕ) : ℚ))
    (hlo : 1 / 4 ≤ (n : ℚ) * ((S.prod : ℚ) * gfun S.length ((m : ℚ) + ((S.sum - S.length : ℕ) : ℚ))))
    (hhi : (n : ℚ) * ((S.prod : ℚ) * gfun S.length (m : ℚ)) ≤ 3 / 4) :
    ∃ i ≤ S.sum, coefL S i ≠ 0 ∧ n % (m + i) < k := by
  by_contra hcon
  have hbad : ∀ i ≤ S.sum, coefL S i ≠ 0 → k ≤ n % (m + i) :=
    fun i hi hc => not_lt.mp (fun h => hcon ⟨i, hi, hc, h⟩)
  have hmq : (0 : ℚ) < (m : ℚ) := by exact_mod_cast (by omega : 0 < m)
  have hsum : ∑ i ∈ Finset.range (S.sum + 1), coefL S i = 0 := by
    obtain ⟨s, S', rfl⟩ := List.exists_cons_of_ne_nil hS
    have h := coefL_sum (s :: S') (fun _ => (1 : ℚ)) (s :: S').sum le_rfl
    rw [seqD_const] at h
    have h' : ((∑ i ∈ Finset.range ((s :: S').sum + 1), coefL (s :: S') i : ℤ) : ℚ) = 0 := by
      rw [Int.cast_sum]
      simpa using h
    exact_mod_cast h'
  have hC := cert_general n k m S.sum (coefL S) hk hm hsum hbad
  have hΦ : ∑ i ∈ Finset.range (S.sum + 1), (coefL S i : ℚ) / ((m : ℚ) + (i : ℚ))
      = Dl S (fun y => 1 / y) (m : ℚ) := by
    rw [Dl_seqD S (fun y => 1 / y) (m : ℚ),
      ← coefL_sum S (fun i => 1 / ((m : ℚ) + (i : ℚ))) S.sum le_rfl]
    apply Finset.sum_congr rfl
    intro i _
    ring
  rw [hΦ] at hC
  obtain ⟨hb1, hb2⟩ := lemma1_bounds S (m : ℚ) hmq
  have hn : (0 : ℚ) ≤ (n : ℚ) := Nat.cast_nonneg n
  have hV1 : 1 / 4 ≤ (n : ℚ) * Dl S (fun y => 1 / y) (m : ℚ) :=
    hlo.trans (mul_le_mul_of_nonneg_left hb1 hn)
  have hV2 : (n : ℚ) * Dl S (fun y => 1 / y) (m : ℚ) ≤ 3 / 4 :=
    (mul_le_mul_of_nonneg_left hb2 hn).trans hhi
  have hmNq : (0 : ℚ) < ((m + S.sum : ℕ) : ℚ) := by exact_mod_cast (by omega : 0 < m + S.sum)
  have hE : (∑ i ∈ Finset.range (S.sum + 1), |(coefL S i : ℚ)|) / 2
      * (((m + S.sum - k : ℕ) : ℚ) / ((m + S.sum : ℕ) : ℚ)) < 1 / 4 := by
    rw [div_mul_div_comm, div_lt_iff₀ (by linarith : (0 : ℚ) < 2 * ((m + S.sum : ℕ) : ℚ))]
    linarith
  have habs := lt_of_le_of_lt hC hE
  rw [abs_lt] at habs
  obtain ⟨T, hT⟩ : ∃ T : ℤ, T = ∑ i ∈ Finset.range (S.sum + 1), coefL S i * ((n / (m + i) + 1 : ℕ) : ℤ) :=
    ⟨_, rfl⟩
  rw [← hT] at habs
  rcases le_or_gt T 0 with hT0 | hT1
  · have : (T : ℚ) ≤ 0 := by exact_mod_cast hT0
    linarith [habs.1]
  · have : (1 : ℚ) ≤ (T : ℚ) := by exact_mod_cast hT1
    linarith [habs.2]

/-- `coefL S` is the coefficient sequence of the polynomial `∏_{s ∈ S} (1 - X^s)` over `ℤ`: the
interface to any bound on the norm `Σ |coefL S i|` proved with `Polynomial`. -/
private theorem coefL_eq_coeff (S : List ℕ) : ∀ i : ℕ,
    coefL S i = ((S.map (fun s => (1 - Polynomial.X ^ s : Polynomial ℤ))).prod).coeff i := by
  induction S with
  | nil =>
    intro i
    simp [coefL, Polynomial.coeff_one]
  | cons s S ih =>
    intro i
    rw [show coefL (s :: S) i = coefL S i - (if s ≤ i then coefL S (i - s) else 0) from rfl]
    rw [List.map_cons, List.prod_cons, sub_mul, one_mul, Polynomial.coeff_sub,
      Polynomial.coeff_X_pow_mul', ih i]
    split_ifs with h
    · rw [ih (i - s)]
    · rfl

/-- (1.3) of `B/T1-proof.md`, coefficients: scaling every step by `g` spreads the coefficients
to the multiples of `g`. -/
private theorem coefL_scale (g : ℕ) (hg : 0 < g) (S : List ℕ) : ∀ i : ℕ,
    coefL (S.map (fun s => g * s)) i = if g ∣ i then coefL S (i / g) else 0 := by
  induction S with
  | nil =>
    intro i
    simp only [List.map_nil, coefL]
    by_cases hi : i = 0
    · subst hi
      simp
    · by_cases hd : g ∣ i
      · have hq : i / g ≠ 0 := by
          obtain ⟨t, rfl⟩ := hd
          have ht : t ≠ 0 := by
            rintro rfl
            simp at hi
          rw [Nat.mul_div_cancel_left _ hg]
          exact ht
        simp [hi, hd, hq]
      · simp [hi, hd]
  | cons s S ih =>
    intro i
    rw [List.map_cons,
      show coefL (g * s :: S.map (fun s => g * s)) i
        = coefL (S.map (fun s => g * s)) i
          - (if g * s ≤ i then coefL (S.map (fun s => g * s)) (i - g * s) else 0) from rfl,
      ih i]
    by_cases hd : g ∣ i
    · obtain ⟨t, rfl⟩ := hd
      rw [if_pos (Dvd.intro t rfl), if_pos (Dvd.intro t rfl), Nat.mul_div_cancel_left _ hg,
        show coefL (s :: S) t = coefL S t - (if s ≤ t then coefL S (t - s) else 0) from rfl]
      by_cases hst : s ≤ t
      · have h1 : g * s ≤ g * t := Nat.mul_le_mul_left g hst
        have h2 : g * t - g * s = g * (t - s) := (Nat.mul_sub g t s).symm
        rw [if_pos h1, if_pos hst, h2, ih (g * (t - s)), if_pos (Dvd.intro _ rfl),
          Nat.mul_div_cancel_left _ hg]
      · have h1 : ¬ g * s ≤ g * t := fun h => hst (Nat.le_of_mul_le_mul_left h hg)
        rw [if_neg h1, if_neg hst]
    · rw [if_neg hd, if_neg hd]
      by_cases h1 : g * s ≤ i
      · have h3 : ¬ g ∣ i - g * s := by
          intro h
          apply hd
          have e : i = (i - g * s) + g * s := (Nat.sub_add_cancel h1).symm
          rw [e]
          exact dvd_add h (Dvd.intro s rfl)
        rw [if_pos h1, ih (i - g * s), if_neg h3]
        simp
      · rw [if_neg h1]
        simp

/-- (1.3) of `B/T1-proof.md`, norm: scaling every step by `g ≥ 1` does not change `Σ |c_i|`. -/
private theorem norm_scale (g : ℕ) (hg : 0 < g) (S : List ℕ) (N : ℕ) :
    ∑ i ∈ Finset.range (g * N + 1), |(coefL (S.map (fun s => g * s)) i : ℚ)|
      = ∑ j ∈ Finset.range (N + 1), |(coefL S j : ℚ)| := by
  induction N with
  | zero => simp [coefL_scale g hg S]
  | succ N ih =>
    have e : g * (N + 1) + 1 = (g * N + 1) + g := by ring
    rw [e, Finset.sum_range_add, ih, Finset.sum_range_succ (fun j => |(coefL S j : ℚ)|) (N + 1)]
    congr 1
    rw [Finset.sum_eq_single (g - 1)]
    · have e2 : g * N + 1 + (g - 1) = g * (N + 1) := by
        rw [Nat.mul_succ]
        omega
      rw [e2, coefL_scale g hg S, if_pos (Dvd.intro _ rfl), Nat.mul_div_cancel_left _ hg]
    · intro t ht hne
      have ht' := Finset.mem_range.mp ht
      have hnd : ¬ g ∣ g * N + 1 + t := by
        intro h
        rw [Nat.add_assoc] at h
        have h' : g ∣ 1 + t := (Nat.dvd_add_right (Dvd.intro N rfl : g ∣ g * N)).mp h
        have := Nat.le_of_dvd (by omega) h'
        omega
      rw [coefL_scale g hg S, if_neg hnd]
      simp
    · intro h
      exact absurd (Finset.mem_range.mpr (by omega)) h

private theorem sum_map_scale (g : ℕ) (S : List ℕ) : (S.map (fun s => g * s)).sum = g * S.sum := by
  induction S with
  | nil => simp
  | cons s S ih => rw [List.map_cons, List.sum_cons, ih, List.sum_cons, Nat.mul_add]

private theorem prod_map_scale (g : ℕ) (S : List ℕ) :
    (S.map (fun s => g * s)).prod = g ^ S.length * S.prod := by
  induction S with
  | nil => simp
  | cons s S ih =>
    rw [List.map_cons, List.prod_cons, ih, List.length_cons, List.prod_cons, pow_succ]
    ring

/-- Proposition 3 after the substitution `z ↦ z^g` (the tuning of `B/T1-proof.md` §3): the
moduli are `m + g·j`, the degree is `g·N`, `β` becomes `g^ρ·β`, and the norm is that of the
UNSCALED product. -/
private theorem no_bad_translate_scaled (n k m g : ℕ) (S : List ℕ) (hS : S ≠ []) (hg : 0 < g)
    (hk : 0 < k) (hm : k < m)
    (hsmall : 2 * (∑ j ∈ Finset.range (S.sum + 1), |(coefL S j : ℚ)|)
        * ((m + g * S.sum - k : ℕ) : ℚ)
      < ((m + g * S.sum : ℕ) : ℚ))
    (hlo : 1 / 4 ≤ (n : ℚ) * (((g ^ S.length * S.prod : ℕ) : ℚ)
      * gfun S.length ((m : ℚ) + ((g * S.sum - S.length : ℕ) : ℚ))))
    (hhi : (n : ℚ) * (((g ^ S.length * S.prod : ℕ) : ℚ) * gfun S.length (m : ℚ)) ≤ 3 / 4) :
    ∃ j ≤ S.sum, coefL S j ≠ 0 ∧ n % (m + g * j) < k := by
  have hS' : S.map (fun s => g * s) ≠ [] := by
    intro h
    exact hS (List.map_eq_nil_iff.mp h)
  have h := no_bad_translate n k m (S.map (fun s => g * s)) hS' hk hm
  rw [sum_map_scale, prod_map_scale, List.length_map, norm_scale g hg S S.sum] at h
  obtain ⟨i, hi, hci, hbad⟩ := h hsmall hlo hhi
  rw [coefL_scale g hg S i] at hci
  by_cases hd : g ∣ i
  · rw [if_pos hd] at hci
    obtain ⟨j, rfl⟩ := hd
    rw [Nat.mul_div_cancel_left _ hg] at hci
    exact ⟨j, Nat.le_of_mul_le_mul_left hi hg, hci, hbad⟩
  · rw [if_neg hd] at hci
    exact absurd rfl hci

/-! ### From here: B/t1_analytic.lean -/

/-- T1, (A2)/(B0) of B/T1-proof.md in its general form: for any finite set `S` of integers the
cosine sum over ALL ordered differences is nonnegative, because it is `|Σ e^{isθ}|²`. (The diagonal
contributes `|S|`, and each positive difference twice: `Σ_{t} cos(s_t θ) ≥ -|S|/2`.) -/
private theorem t1_cos_differences_nonneg (S : Finset ℕ) (θ : ℝ) :
    0 ≤ ∑ s ∈ S, ∑ t ∈ S, Real.cos (((s : ℝ) - (t : ℝ)) * θ) := by
  have h : ∀ s t : ℕ, Real.cos (((s : ℝ) - (t : ℝ)) * θ) =
      Real.cos ((s : ℝ) * θ) * Real.cos ((t : ℝ) * θ) + Real.sin ((s : ℝ) * θ) * Real.sin ((t : ℝ) * θ) := by
    intro s t
    rw [sub_mul, Real.cos_sub]
  simp_rw [h, Finset.sum_add_distrib, ← Finset.sum_mul_sum]
  nlinarith [mul_self_nonneg (∑ s ∈ S, Real.cos ((s : ℝ) * θ)), mul_self_nonneg (∑ s ∈ S, Real.sin ((s : ℝ) * θ))]

/-- T1, (A3): on the unit circle, shrinking the radius to `r` loses at most the factor `r`. -/
private theorem t1_shrink (w : ℂ) (hw : ‖w‖ = 1) (r : ℝ) (hr0 : 0 < r) (hr1 : r ≤ 1) :
    r * ‖1 - w‖ ≤ ‖1 - (r : ℂ) * w‖ := by
  have hw2 : w.re * w.re + w.im * w.im = 1 := by
    have h := Complex.sq_norm w
    rw [hw, Complex.normSq_apply] at h
    linarith
  have ha : w.re ≤ 1 := by nlinarith [mul_self_nonneg w.im, mul_self_nonneg (w.re - 1)]
  have h1 : (r * ‖1 - w‖) ^ 2 ≤ ‖1 - (r : ℂ) * w‖ ^ 2 := by
    rw [mul_pow, Complex.sq_norm, Complex.sq_norm, Complex.normSq_apply, Complex.normSq_apply]
    simp only [Complex.sub_re, Complex.sub_im, Complex.one_re, Complex.one_im, Complex.mul_re,
      Complex.mul_im, Complex.ofReal_re, Complex.ofReal_im]
    have e : r ^ 2 * (w.im * w.im) = r ^ 2 * (1 - w.re * w.re) := by
      rw [show w.im * w.im = 1 - w.re * w.re by linarith]
    have hp : 0 ≤ (1 - r) * (1 + r - 2 * r * w.re) := by
      apply mul_nonneg (by linarith)
      nlinarith
    nlinarith [e, hp]
  exact (pow_le_pow_iff_left₀ (by positivity) (norm_nonneg _) two_ne_zero).mp h1

/-- T1, (A4)/(B0), the analytic core: if a family of exponents `s t` (repeats allowed) has
`Σ_t Re(w^(s t)) ≥ -M` at every point `w` of the unit circle (that is, `Σ_t cos(s_t θ) ≥ -M`), then
for every `z` on the circle and every `0 < r < 1`,
`r^ρ · |∏_t (1 - z^(s t))| ≤ (1/(1-r))^M`, `ρ` the number of factors.
(With `r = 1 - 1/ρ` this is `sup |∏| ≤ r^(-ρ) · ρ^M`.) -/
private theorem t1_product_bound {ι : Type} (T : Finset ι) (s : ι → ℕ) (M : ℝ)
    (hM : ∀ w : ℂ, ‖w‖ = 1 → -M ≤ ∑ t ∈ T, (w ^ s t).re)
    (z : ℂ) (hz : ‖z‖ = 1) (r : ℝ) (hr0 : 0 < r) (hr1 : r < 1) :
    r ^ T.card * ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ Real.exp (M * (-Real.log (1 - r))) := by
  have hzs : ∀ t, ‖z ^ s t‖ = 1 := by intro t; rw [norm_pow, hz, one_pow]
  have hu : ∀ t, ‖(r : ℂ) * z ^ s t‖ < 1 := by
    intro t
    rw [norm_mul, hzs t, mul_one, Complex.norm_real, Real.norm_eq_abs, abs_of_pos hr0]
    exact hr1
  -- step 1: shrink every factor
  have h1 : r ^ T.card * ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ ∏ t ∈ T, ‖1 - (r : ℂ) * z ^ s t‖ := by
    rw [norm_prod, ← Finset.prod_const, ← Finset.prod_mul_distrib]
    apply Finset.prod_le_prod
    · intro t _; exact mul_nonneg hr0.le (norm_nonneg _)
    · intro t _; exact t1_shrink (z ^ s t) (hzs t) r hr0 hr1.le
  -- step 2: each factor as an exponential
  have hne : ∀ t, 1 - (r : ℂ) * z ^ s t ≠ 0 := by
    intro t h
    have h' : (r : ℂ) * z ^ s t = 1 := (sub_eq_zero.mp h).symm
    have := hu t
    rw [h', norm_one] at this
    exact lt_irrefl _ this
  have h2 : ∏ t ∈ T, ‖1 - (r : ℂ) * z ^ s t‖ =
      Real.exp (∑ t ∈ T, (Complex.log (1 - (r : ℂ) * z ^ s t)).re) := by
    rw [Real.exp_sum]
    apply Finset.prod_congr rfl
    intro t _
    rw [Complex.log_re, Real.exp_log (norm_pos_iff.mpr (hne t))]
  -- step 3: the series
  have h3 : HasSum (fun m : ℕ => ∑ t ∈ T, (((r : ℂ) * z ^ s t) ^ m / (m : ℂ)).re)
      (∑ t ∈ T, (-Complex.log (1 - (r : ℂ) * z ^ s t)).re) := by
    apply hasSum_sum
    intro t _
    exact Complex.hasSum_re (Complex.hasSum_taylorSeries_neg_log (hu t))
  have hr' : ‖(r : ℂ)‖ < 1 := by
    rw [Complex.norm_real, Real.norm_eq_abs, abs_of_pos hr0]; exact hr1
  have h4 : HasSum (fun m : ℕ => -M * (r ^ m / (m : ℝ))) (-M * (-Real.log (1 - r))) := by
    have h := Complex.hasSum_re (Complex.hasSum_taylorSeries_neg_log hr')
    have e1 : ∀ m : ℕ, (((r : ℂ)) ^ m / (m : ℂ)).re = r ^ m / (m : ℝ) := by
      intro m
      rw [← Complex.ofReal_pow, ← Complex.ofReal_natCast, ← Complex.ofReal_div, Complex.ofReal_re]
    have e2 : (-Complex.log (1 - (r : ℂ))).re = -Real.log (1 - r) := by
      rw [Complex.neg_re, Complex.log_re, ← Complex.ofReal_one, ← Complex.ofReal_sub,
        Complex.norm_real, Real.norm_eq_abs, abs_of_pos (by linarith)]
    simp only [e1] at h
    rw [e2] at h
    exact h.mul_left (-M)
  have hterm : ∀ m : ℕ, -M * (r ^ m / (m : ℝ)) ≤ ∑ t ∈ T, (((r : ℂ) * z ^ s t) ^ m / (m : ℂ)).re := by
    intro m
    have e : ∀ t, (((r : ℂ) * z ^ s t) ^ m / (m : ℂ)).re = (r ^ m / (m : ℝ)) * ((z ^ m) ^ s t).re := by
      intro t
      have : ((r : ℂ) * z ^ s t) ^ m / (m : ℂ) = ((r ^ m / (m : ℝ) : ℝ) : ℂ) * (z ^ m) ^ s t := by
        push_cast
        ring
      rw [this, Complex.re_ofReal_mul]
    simp_rw [e, ← Finset.mul_sum]
    have hnn : 0 ≤ r ^ m / (m : ℝ) := by positivity
    have hw := hM (z ^ m) (by rw [norm_pow, hz, one_pow])
    nlinarith [mul_le_mul_of_nonneg_left hw hnn]
  have h5 := hasSum_le hterm h4 h3
  have h6 : ∑ t ∈ T, (Complex.log (1 - (r : ℂ) * z ^ s t)).re ≤ M * (-Real.log (1 - r)) := by
    simp only [Complex.neg_re, Finset.sum_neg_distrib] at h5
    linarith
  calc r ^ T.card * ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ ∏ t ∈ T, ‖1 - (r : ℂ) * z ^ s t‖ := h1
    _ = Real.exp (∑ t ∈ T, (Complex.log (1 - (r : ℂ) * z ^ s t)).re) := h2
    _ ≤ Real.exp (M * (-Real.log (1 - r))) := Real.exp_le_exp.mpr h6

/-- T1, (A4)/(B0) as used: `ρ ≥ 2` factors with cosine sum `≥ -M` have
`sup_{|z|=1} |∏ (1 - z^(s t))| ≤ 8·ρ^M`. (The paper has the constant 4; `e² < 8` is what the short
proof of `(1 - 1/ρ)^ρ ≥ e^(-2)` gives.) -/
private theorem t1_sup_bound {ι : Type} (T : Finset ι) (s : ι → ℕ) (M : ℝ)
    (hM : ∀ w : ℂ, ‖w‖ = 1 → -M ≤ ∑ t ∈ T, (w ^ s t).re)
    (hρ : 2 ≤ T.card) (z : ℂ) (hz : ‖z‖ = 1) :
    ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ 8 * (T.card : ℝ) ^ M := by
  have hρ2 : (2 : ℝ) ≤ (T.card : ℝ) := by exact_mod_cast hρ
  have hρpos : (0 : ℝ) < (T.card : ℝ) := by linarith
  obtain ⟨x, hxdef⟩ : ∃ x : ℝ, x = 1 / (T.card : ℝ) := ⟨_, rfl⟩
  have hx0 : 0 < x := by rw [hxdef]; positivity
  have hx1 : x ≤ 1 / 2 := by
    rw [hxdef]
    exact one_div_le_one_div_of_le (by norm_num) hρ2
  have hxρ : (T.card : ℝ) * x = 1 := by rw [hxdef]; field_simp
  have hb := t1_product_bound T s M hM z hz (1 - x) (by linarith) (by linarith)
  have e1 : 1 - (1 - x) = ((T.card : ℝ))⁻¹ := by rw [hxdef]; ring
  rw [e1, Real.log_inv, neg_neg] at hb
  have e2 : Real.exp (M * Real.log (T.card : ℝ)) = (T.card : ℝ) ^ M := by
    rw [Real.rpow_def_of_pos hρpos, mul_comm]
  rw [e2] at hb
  -- (1 - x)^ρ ≥ e^(-2)
  have h1 : 1 + 2 * x ≤ Real.exp (2 * x) := by
    have := Real.add_one_le_exp (2 * x)
    linarith
  have h2 : Real.exp (-(2 * x)) ≤ 1 - x := by
    rw [Real.exp_neg]
    calc (Real.exp (2 * x))⁻¹ ≤ (1 + 2 * x)⁻¹ := inv_anti₀ (by positivity) h1
      _ ≤ 1 - x := by
        rw [inv_le_iff_one_le_mul₀ (by positivity)]
        nlinarith [mul_nonneg hx0.le (by linarith : (0 : ℝ) ≤ 1 - 2 * x)]
  have h3 : Real.exp (-(2 * x)) ^ T.card ≤ (1 - x) ^ T.card :=
    pow_le_pow_left₀ (Real.exp_pos _).le h2 _
  have h4 : Real.exp (-(2 * x)) ^ T.card = Real.exp (-2) := by
    rw [← Real.exp_nat_mul]
    congr 1
    linarith
  rw [h4] at h3
  have hP : 0 ≤ ‖∏ t ∈ T, (1 - z ^ s t)‖ := norm_nonneg _
  have h5 : Real.exp (-2) * ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ (T.card : ℝ) ^ M :=
    le_trans (mul_le_mul_of_nonneg_right h3 hP) hb
  have h6 : ‖∏ t ∈ T, (1 - z ^ s t)‖ = Real.exp 2 * (Real.exp (-2) * ‖∏ t ∈ T, (1 - z ^ s t)‖) := by
    rw [← mul_assoc, ← Real.exp_add]
    norm_num
  have h7 : Real.exp 2 ≤ 8 := by
    have h : Real.exp 2 = Real.exp 1 * Real.exp 1 := by
      rw [← Real.exp_add]; norm_num
    nlinarith [Real.exp_one_lt_d9, Real.exp_pos 1]
  have hpow : 0 ≤ (T.card : ℝ) ^ M := Real.rpow_nonneg hρpos.le M
  rw [h6]
  calc Real.exp 2 * (Real.exp (-2) * ‖∏ t ∈ T, (1 - z ^ s t)‖)
      ≤ Real.exp 2 * (T.card : ℝ) ^ M := mul_le_mul_of_nonneg_left h5 (Real.exp_pos _).le
    _ ≤ 8 * (T.card : ℝ) ^ M := mul_le_mul_of_nonneg_right h7 hpow

/-! ### A concrete family with no outside input: all differences `j - i`, `0 ≤ i < j < p`
(the exponent `d` with multiplicity `p - d`: the Fejér kernel). -/

private theorem t1_unit_shift (w : ℂ) (hw : ‖w‖ = 1) (i d : ℕ) :
    (w ^ i * (starRingEnd ℂ) (w ^ (i + d))).re = (w ^ d).re := by
  have hww : w ^ i * (starRingEnd ℂ) (w ^ i) = 1 := by
    rw [Complex.mul_conj, Complex.normSq_eq_norm_sq, norm_pow, hw]
    simp
  rw [pow_add, map_mul, ← mul_assoc, hww, one_mul, Complex.conj_re]

private theorem t1_fejer_normSq (w : ℂ) (hw : ‖w‖ = 1) (p : ℕ) :
    Complex.normSq (∑ i ∈ Finset.range p, w ^ i) =
      (p : ℝ) + 2 * ∑ j ∈ Finset.range p, ∑ i ∈ Finset.range j, (w ^ (j - i)).re := by
  induction p with
  | zero => simp
  | succ p ih =>
    rw [Finset.sum_range_succ (fun i => w ^ i),
      Finset.sum_range_succ (fun j => ∑ i ∈ Finset.range j, (w ^ (j - i)).re),
      Complex.normSq_add, ih]
    have h1 : Complex.normSq (w ^ p) = 1 := by
      rw [Complex.normSq_eq_norm_sq, norm_pow, hw]
      simp
    have h2 : ((∑ i ∈ Finset.range p, w ^ i) * (starRingEnd ℂ) (w ^ p)).re =
        ∑ i ∈ Finset.range p, (w ^ (p - i)).re := by
      rw [Finset.sum_mul, Complex.re_sum]
      apply Finset.sum_congr rfl
      intro i hi
      have hi' : i ≤ p := (Finset.mem_range.mp hi).le
      have h := t1_unit_shift w hw i (p - i)
      rw [Nat.add_sub_of_le hi'] at h
      exact h
    rw [h1, h2]
    push_cast
    ring

/-- The cosine sum of the family is at least `-p/2`. -/
private theorem t1_fejer_cos (w : ℂ) (hw : ‖w‖ = 1) (p : ℕ) :
    -((p : ℝ) / 2) ≤ ∑ j ∈ Finset.range p, ∑ i ∈ Finset.range j, (w ^ (j - i)).re := by
  have h := t1_fejer_normSq w hw p
  have h0 := Complex.normSq_nonneg (∑ i ∈ Finset.range p, w ^ i)
  linarith

private theorem t1_fejer_card (p : ℕ) :
    ((Finset.range p).sigma (fun j => Finset.range j)).card * 2 = p * (p - 1) := by
  rw [Finset.card_sigma]
  simp only [Finset.card_range]
  exact Finset.sum_range_id_mul_two p

/-- T1's small-norm product, with no citation: for `p ≥ 3` the product of `1 - z^(j-i)` over
`0 ≤ i < j < p` (`ρ = p(p-1)/2` factors, exponents below `p`) is at most `8·ρ^(p/2)` on the unit circle,
against `2^ρ` for `(1 - z)^ρ`. -/
private theorem t1_fejer_sup (p : ℕ) (hp : 3 ≤ p) (z : ℂ) (hz : ‖z‖ = 1) :
    ‖∏ t ∈ (Finset.range p).sigma (fun j => Finset.range j), (1 - z ^ (t.1 - t.2))‖ ≤
      8 * ((((Finset.range p).sigma (fun j => Finset.range j)).card : ℕ) : ℝ) ^ ((p : ℝ) / 2) := by
  apply t1_sup_bound ((Finset.range p).sigma (fun j => Finset.range j)) (fun t => t.1 - t.2) ((p : ℝ) / 2)
  · intro w hw
    rw [Finset.sum_sigma]
    exact t1_fejer_cos w hw p
  · have hc := t1_fejer_card p
    have h6 : 3 * 2 ≤ p * (p - 1) := Nat.mul_le_mul hp (by omega)
    omega
  · exact hz

/-- T1: a coefficient of a polynomial is at most its sup on the unit circle (average over the
`n`-th roots of unity, `n = deg + 1`). This is the bridge from a sup bound for `∏(1 - z^s)` to the
ℓ¹ norm the certificate needs: `‖c‖₁ ≤ (N + 1)·sup`. -/
private theorem t1_coeff_le_sup (P : Polynomial ℂ) (B : ℝ) (hB : ∀ z : ℂ, ‖z‖ = 1 → ‖P.eval z‖ ≤ B) (i : ℕ) :
    ‖P.coeff i‖ ≤ B := by
  have hB0 : 0 ≤ B := le_trans (norm_nonneg _) (hB 1 (by simp))
  by_cases hi' : P.natDegree < i
  · rw [Polynomial.coeff_eq_zero_of_natDegree_lt hi']
    simpa using hB0
  have hi : i ≤ P.natDegree := not_lt.mp hi'
  obtain ⟨n, hn⟩ : ∃ n : ℕ, n = P.natDegree + 1 := ⟨_, rfl⟩
  have hn0 : n ≠ 0 := by omega
  obtain ⟨ζ, hprim⟩ : ∃ ζ : ℂ, IsPrimitiveRoot ζ n := ⟨_, Complex.isPrimitiveRoot_exp n hn0⟩
  have hζ1 : ‖ζ‖ = 1 := hprim.norm'_eq_one hn0
  have key : ∑ j ∈ Finset.range n, P.eval (ζ ^ j) * (ζ ^ j) ^ (n - i) = (n : ℂ) * P.coeff i := by
    simp_rw [Polynomial.eval_eq_sum_range, Finset.sum_mul]
    rw [Finset.sum_comm]
    have inner : ∀ m ∈ Finset.range (P.natDegree + 1),
        ∑ j ∈ Finset.range n, P.coeff m * (ζ ^ j) ^ m * (ζ ^ j) ^ (n - i) =
          if m = i then (n : ℂ) * P.coeff i else 0 := by
      intro m hm
      have hm' : m < n := by rw [hn]; exact Finset.mem_range.mp hm
      have e : ∀ j : ℕ, P.coeff m * (ζ ^ j) ^ m * (ζ ^ j) ^ (n - i) =
          P.coeff m * (ζ ^ (m + (n - i))) ^ j := by
        intro j; ring
      simp_rw [e, ← Finset.mul_sum]
      split_ifs with hmi
      · rw [hmi]
        have h1 : ζ ^ (i + (n - i)) = 1 := by
          rw [Nat.add_sub_of_le (by omega : i ≤ n)]
          exact hprim.pow_eq_one
        rw [h1]
        simp only [one_pow, Finset.sum_const, Finset.card_range, nsmul_eq_mul, mul_one]
        ring
      · have hne : ζ ^ (m + (n - i)) ≠ 1 := by
          intro h
          obtain ⟨q, hq⟩ := (hprim.pow_eq_one_iff_dvd _).mp h
          rcases q with _ | _ | q
          · simp at hq; omega
          · simp at hq; omega
          · have h2 : n * 2 ≤ n * (q + 1 + 1) := Nat.mul_le_mul_left n (by omega)
            omega
        have hs : ∑ j ∈ Finset.range n, (ζ ^ (m + (n - i))) ^ j = 0 := by
          have h := mul_geom_sum (ζ ^ (m + (n - i))) n
          have hpow : (ζ ^ (m + (n - i))) ^ n = 1 := by
            rw [← pow_mul, mul_comm, pow_mul, hprim.pow_eq_one, one_pow]
          rw [hpow, sub_self] at h
          exact (mul_eq_zero.mp h).resolve_left (sub_ne_zero.mpr hne)
        rw [hs, mul_zero]
    rw [Finset.sum_congr rfl inner, Finset.sum_ite_eq']
    exact if_pos (Finset.mem_range.mpr (Nat.lt_succ_of_le hi))
  have hnorm : ‖(n : ℂ) * P.coeff i‖ ≤ n * B := by
    rw [← key]
    calc ‖∑ j ∈ Finset.range n, P.eval (ζ ^ j) * (ζ ^ j) ^ (n - i)‖
        ≤ ∑ j ∈ Finset.range n, ‖P.eval (ζ ^ j) * (ζ ^ j) ^ (n - i)‖ := norm_sum_le _ _
      _ ≤ ∑ _j ∈ Finset.range n, B := by
          apply Finset.sum_le_sum
          intro j _
          rw [norm_mul, norm_pow, norm_pow, hζ1, one_pow, one_pow, mul_one]
          exact hB _ (by rw [norm_pow, hζ1, one_pow])
      _ = n * B := by rw [Finset.sum_const, Finset.card_range, nsmul_eq_mul]
  rw [norm_mul, Complex.norm_natCast] at hnorm
  have hnpos : (0 : ℝ) < n := by exact_mod_cast Nat.pos_of_ne_zero hn0
  exact le_of_mul_le_mul_left hnorm hnpos

/-- T1: the ℓ¹ norm of the certificate polynomial `∏ (1 - X^(s t))` over its first `N + 1`
coefficients, from the cosine-sum hypothesis: at most `(N + 1)·8·ρ^M`. -/
private theorem t1_l1_product {ι : Type} (T : Finset ι) (s : ι → ℕ) (M : ℝ)
    (hM : ∀ w : ℂ, ‖w‖ = 1 → -M ≤ ∑ t ∈ T, (w ^ s t).re)
    (hρ : 2 ≤ T.card) (N : ℕ) :
    ∑ i ∈ Finset.range (N + 1), ‖(∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℂ) ^ s t)).coeff i‖ ≤
      ((N : ℝ) + 1) * (8 * (T.card : ℝ) ^ M) := by
  have hB : ∀ z : ℂ, ‖z‖ = 1 →
      ‖(∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℂ) ^ s t)).eval z‖ ≤ 8 * (T.card : ℝ) ^ M := by
    intro z hz
    rw [Polynomial.eval_prod]
    simp only [Polynomial.eval_sub, Polynomial.eval_one, Polynomial.eval_pow, Polynomial.eval_X]
    exact t1_sup_bound T s M hM hρ z hz
  calc ∑ i ∈ Finset.range (N + 1), ‖(∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℂ) ^ s t)).coeff i‖
      ≤ ∑ _i ∈ Finset.range (N + 1), (8 * (T.card : ℝ) ^ M) :=
        Finset.sum_le_sum (fun i _ => t1_coeff_le_sup _ _ hB i)
    _ = ((N : ℝ) + 1) * (8 * (T.card : ℝ) ^ M) := by
        rw [Finset.sum_const, Finset.card_range, nsmul_eq_mul]
        push_cast
        ring

/-- The same for the integer polynomial (the form `A/t1_general.lean`'s `coefL_eq_coeff` speaks). -/
private theorem t1_l1_product_int {ι : Type} (T : Finset ι) (s : ι → ℕ) (M : ℝ)
    (hM : ∀ w : ℂ, ‖w‖ = 1 → -M ≤ ∑ t ∈ T, (w ^ s t).re)
    (hρ : 2 ≤ T.card) (N : ℕ) :
    ∑ i ∈ Finset.range (N + 1),
        |(((∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℤ) ^ s t)).coeff i : ℤ) : ℝ)| ≤
      ((N : ℝ) + 1) * (8 * (T.card : ℝ) ^ M) := by
  have hmap : (∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℤ) ^ s t)).map (Int.castRingHom ℂ) =
      ∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℂ) ^ s t) := by
    rw [Polynomial.map_prod]
    simp
  have h := t1_l1_product T s M hM hρ N
  rw [← hmap] at h
  simp only [Polynomial.coeff_map, eq_intCast, Complex.norm_intCast] at h
  exact h

/-- For the family of all differences `j - i`, `0 ≤ i < j < p`: ℓ¹ norm at most `(N+1)·8·ρ^(p/2)`,
with nothing assumed. -/
private theorem t1_fejer_l1 (p : ℕ) (hp : 3 ≤ p) (N : ℕ) :
    ∑ i ∈ Finset.range (N + 1),
        |(((∏ t ∈ (Finset.range p).sigma (fun j => Finset.range j),
            (1 - (Polynomial.X : Polynomial ℤ) ^ (t.1 - t.2))).coeff i : ℤ) : ℝ)| ≤
      ((N : ℝ) + 1) *
        (8 * ((((Finset.range p).sigma (fun j => Finset.range j)).card : ℕ) : ℝ) ^ ((p : ℝ) / 2)) := by
  apply t1_l1_product_int ((Finset.range p).sigma (fun j => Finset.range j)) (fun t => t.1 - t.2)
    ((p : ℝ) / 2)
  · intro w hw
    rw [Finset.sum_sigma]
    exact t1_fejer_cos w hw p
  · have hc := t1_fejer_card p
    have h6 : 3 * 2 ≤ p * (p - 1) := Nat.mul_le_mul hp (by omega)
    omega

/-! ### Part (a) of T1 for the small-norm family, nothing assumed (agent B, 12:25Z)

`A/t1_general.lean` (agent A, above) and `B/t1_analytic.lean` (agent B, above) joined: Proposition 3
of B/T1-proof.md for the product over all differences `j - i`, `0 ≤ i < j < p`, scaled by `g`, with
the ℓ¹ norm replaced by the proved bound `(N + 1)·8·ρ^(p/2)`. -/

/-- The exponents of the family, as a list: all `j - i` with `0 ≤ i < j < p`. -/
private noncomputable def fejerList (p : ℕ) : List ℕ :=
  ((Finset.range p).sigma (fun j => Finset.range j)).toList.map (fun t => t.1 - t.2)

private theorem fejerList_length (p : ℕ) :
    (fejerList p).length = ((Finset.range p).sigma (fun j => Finset.range j)).card := by
  unfold fejerList
  rw [List.length_map, Finset.length_toList]

private theorem fejerList_prod_poly (p : ℕ) :
    ((fejerList p).map (fun s => (1 - Polynomial.X ^ s : Polynomial ℤ))).prod =
      ∏ t ∈ (Finset.range p).sigma (fun j => Finset.range j),
        (1 - (Polynomial.X : Polynomial ℤ) ^ (t.1 - t.2)) := by
  unfold fejerList
  rw [List.map_map]
  exact Finset.prod_map_toList _ _

private theorem t1_fejer_no_bad_translate (n k m g p : ℕ) (hp : 3 ≤ p) (hg : 0 < g) (hk : 0 < k) (hm : k < m)
    (hsmall : 2 * ((((fejerList p).sum : ℝ) + 1) *
          (8 * ((fejerList p).length : ℝ) ^ ((p : ℝ) / 2)))
        * ((m + g * (fejerList p).sum - k : ℕ) : ℝ)
      < ((m + g * (fejerList p).sum : ℕ) : ℝ))
    (hlo : 1 / 4 ≤ (n : ℚ) * (((g ^ (fejerList p).length * (fejerList p).prod : ℕ) : ℚ)
      * gfun (fejerList p).length ((m : ℚ) + ((g * (fejerList p).sum - (fejerList p).length : ℕ) : ℚ))))
    (hhi : (n : ℚ) * (((g ^ (fejerList p).length * (fejerList p).prod : ℕ) : ℚ)
      * gfun (fejerList p).length (m : ℚ)) ≤ 3 / 4) :
    ∃ j ≤ (fejerList p).sum, coefL (fejerList p) j ≠ 0 ∧ n % (m + g * j) < k := by
  have hlen := fejerList_length p
  have hcard := t1_fejer_card p
  have h6 : 3 * 2 ≤ p * (p - 1) := Nat.mul_le_mul hp (by omega)
  have hne : fejerList p ≠ [] := by
    intro h
    have h0 : (fejerList p).length = 0 := by rw [h]; rfl
    omega
  have hnorm : ∑ j ∈ Finset.range ((fejerList p).sum + 1), |((coefL (fejerList p) j : ℤ) : ℝ)| ≤
      (((fejerList p).sum : ℝ) + 1) * (8 * ((fejerList p).length : ℝ) ^ ((p : ℝ) / 2)) := by
    have h := t1_fejer_l1 p hp (fejerList p).sum
    rw [hlen]
    simp only [coefL_eq_coeff, fejerList_prod_poly]
    exact h
  have hx : (0 : ℝ) ≤ ((m + g * (fejerList p).sum - k : ℕ) : ℝ) := Nat.cast_nonneg _
  refine no_bad_translate_scaled n k m g (fejerList p) hne hg hk hm ?_ hlo hhi
  have hR : ((2 * (∑ j ∈ Finset.range ((fejerList p).sum + 1), |(coefL (fejerList p) j : ℚ)|)
        * ((m + g * (fejerList p).sum - k : ℕ) : ℚ) : ℚ) : ℝ)
      < (((m + g * (fejerList p).sum : ℕ) : ℚ) : ℝ) := by
    simp only [Rat.cast_mul, Rat.cast_sum, Rat.cast_abs, Rat.cast_intCast, Rat.cast_natCast,
      Rat.cast_ofNat]
    nlinarith [mul_le_mul_of_nonneg_right hnorm hx]
  exact_mod_cast hR
private theorem t1_tuning (c : ℝ) (hc : 0 < c) (ρ G : ℕ) (hρ : 1 ≤ ρ) (hρG : ρ ≤ G)
    (hlo : c * (ρ : ℝ) ^ ρ ≤ 3 / 4) (hhi : 26 / 100 ≤ c * (G : ℝ) ^ ρ) :
    ∃ g : ℕ, ρ ≤ g ∧ g ≤ G ∧ 26 / 100 ≤ c * (g : ℝ) ^ ρ ∧ c * (g : ℝ) ^ ρ ≤ 3 / 4 := by
  classical
  have hex : ∃ g : ℕ, ρ ≤ g ∧ 26 / 100 ≤ c * (g : ℝ) ^ ρ := ⟨G, hρG, hhi⟩
  obtain ⟨hg1, hg2⟩ := Nat.find_spec hex
  have hgG : Nat.find hex ≤ G := Nat.find_min' hex ⟨hρG, hhi⟩
  refine ⟨Nat.find hex, hg1, hgG, hg2, ?_⟩
  rcases Nat.eq_or_lt_of_le hg1 with heq | hlt
  · rw [← heq]; exact hlo
  · -- the step before the first one
    obtain ⟨h, hh⟩ : ∃ h : ℕ, Nat.find hex = h + 1 := ⟨Nat.find hex - 1, by omega⟩
    have hρh : ρ ≤ h := by omega
    have hnot : ¬ (ρ ≤ h ∧ 26 / 100 ≤ c * (h : ℝ) ^ ρ) := Nat.find_min hex (by omega)
    have hsmall : c * (h : ℝ) ^ ρ < 26 / 100 := by
      by_contra hcon
      exact hnot ⟨hρh, not_lt.mp hcon⟩
    have hpos : (0 : ℝ) < h := by exact_mod_cast (by omega : 0 < h)
    have h1 : (h : ℝ) + 1 ≤ h * Real.exp (1 / h) := by
      have hexp := Real.add_one_le_exp (1 / (h : ℝ))
      calc (h : ℝ) + 1 = h * (1 / h + 1) := by field_simp; ring
        _ ≤ h * Real.exp (1 / h) := mul_le_mul_of_nonneg_left hexp hpos.le
    have h2 : ((h : ℝ) + 1) ^ ρ ≤ (h * Real.exp (1 / h)) ^ ρ :=
      pow_le_pow_left₀ (by positivity) h1 ρ
    have h3 : ((h : ℝ) * Real.exp (1 / h)) ^ ρ = (h : ℝ) ^ ρ * Real.exp ((ρ : ℝ) / h) := by
      rw [mul_pow, ← Real.exp_nat_mul]
      congr 2
      ring
    have h4 : Real.exp ((ρ : ℝ) / h) ≤ Real.exp 1 := by
      apply Real.exp_le_exp.mpr
      rw [div_le_one hpos]
      exact_mod_cast hρh
    have h5 : Real.exp 1 < 2.7182818286 := Real.exp_one_lt_d9
    have hpow : (0 : ℝ) ≤ (h : ℝ) ^ ρ := by positivity
    have hcast : ((Nat.find hex : ℕ) : ℝ) = (h : ℝ) + 1 := by rw [hh]; push_cast; ring
    rw [hcast]
    have hstep : c * ((h : ℝ) + 1) ^ ρ ≤ c * (h : ℝ) ^ ρ * Real.exp 1 := by
      calc c * ((h : ℝ) + 1) ^ ρ ≤ c * ((h : ℝ) ^ ρ * Real.exp ((ρ : ℝ) / h)) := by
            rw [← h3]; exact mul_le_mul_of_nonneg_left h2 hc.le
        _ = c * (h : ℝ) ^ ρ * Real.exp ((ρ : ℝ) / h) := by ring
        _ ≤ c * (h : ℝ) ^ ρ * Real.exp 1 :=
            mul_le_mul_of_nonneg_left h4 (mul_nonneg hc.le hpow)
    have hq : (0 : ℝ) ≤ c * (h : ℝ) ^ ρ := mul_nonneg hc.le hpow
    nlinarith [hstep, hsmall, h5, hq, Real.exp_pos 1]

/-- The family's parameters: `ρ` steps, each in `[1, p]`. -/
private theorem t1_fejer_params (p : ℕ) :
    (fejerList p).length * 2 = p * (p - 1) ∧ (fejerList p).length ≤ (fejerList p).sum ∧
      (fejerList p).sum ≤ (fejerList p).length * p ∧ 1 ≤ (fejerList p).prod ∧
      (fejerList p).prod ≤ p ^ (fejerList p).length := by
  have hmem : ∀ s ∈ fejerList p, 1 ≤ s ∧ s ≤ p := by
    intro s hs
    unfold fejerList at hs
    rw [List.mem_map] at hs
    obtain ⟨t, ht, rfl⟩ := hs
    rw [Finset.mem_toList, Finset.mem_sigma, Finset.mem_range, Finset.mem_range] at ht
    omega
  refine ⟨by rw [fejerList_length]; exact t1_fejer_card p, ?_, ?_, ?_, ?_⟩
  · have h := List.card_nsmul_le_sum (fejerList p) 1 (fun s hs => (hmem s hs).1)
    simpa using h
  · have h := List.sum_le_card_nsmul (fejerList p) p (fun s hs => (hmem s hs).2)
    simpa using h
  · exact List.one_le_prod_of_one_le (fun s hs => (hmem s hs).1)
  · exact List.prod_le_pow_card (fejerList p) p (fun s hs => (hmem s hs).2)

private theorem t1_gfun_le (ρ : ℕ) (x y : ℚ) (hy : 0 < y) (hxy : y ≤ x) :
    gfun ρ x ≤ (ρ.factorial : ℚ) / y ^ (ρ + 1) := by
  unfold gfun
  have h : y ^ (ρ + 1) ≤ ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) := by
    have := Finset.prod_le_prod (s := Finset.range (ρ + 1)) (f := fun _ : ℕ => y)
      (g := fun i : ℕ => x + (i : ℚ)) (fun i _ => hy.le)
      (fun i _ => le_add_of_le_of_nonneg hxy (Nat.cast_nonneg i))
    simpa using this
  exact div_le_div_of_nonneg_left (by positivity) (by positivity) h

private theorem t1_le_gfun (ρ : ℕ) (x y : ℚ) (hx : 0 < x) (hxy : x + (ρ : ℚ) ≤ y) :
    (ρ.factorial : ℚ) / y ^ (ρ + 1) ≤ gfun ρ x := by
  unfold gfun
  have h : ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) ≤ y ^ (ρ + 1) := by
    have := Finset.prod_le_prod (s := Finset.range (ρ + 1)) (f := fun i : ℕ => x + (i : ℚ))
      (g := fun _ : ℕ => y) (fun i _ => by positivity)
      (fun i hi => by
        have : (i : ℚ) ≤ (ρ : ℚ) := by exact_mod_cast Nat.lt_succ_iff.mp (Finset.mem_range.mp hi)
        linarith)
    simpa using this
  exact div_le_div_of_nonneg_left (by positivity) (Finset.prod_pos (fun i _ => by positivity)) h

/-- Pure counting: `M` consecutive blocks of `L` integers after `k`, each holding a `d` with `P d`. -/
private theorem t1_blocks (P : ℕ → Prop) [DecidablePred P] (k L M : ℕ) (hLM : L * M ≤ k)
    (h : ∀ m, k < m → m ≤ k + L * M → ∃ d, m ≤ d ∧ d < m + L ∧ P d) (hL : 0 < L) :
    M ≤ (Finset.filter P (Finset.Ioc k (2 * k))).card := by
  have h' : ∀ i, i < M → ∃ d, k + 1 + i * L ≤ d ∧ d < k + 1 + i * L + L ∧ P d := by
    intro i hi
    apply h
    · omega
    · have : (i + 1) * L ≤ M * L := Nat.mul_le_mul_right L hi
      nlinarith
  choose! f hf using h'
  have hmaps : ∀ i ∈ Finset.range M, f i ∈ Finset.filter P (Finset.Ioc k (2 * k)) := by
    intro i hi
    rw [Finset.mem_range] at hi
    obtain ⟨h1, h2, h3⟩ := hf i hi
    have : (i + 1) * L ≤ M * L := Nat.mul_le_mul_right L hi
    rw [Finset.mem_filter, Finset.mem_Ioc]
    refine ⟨⟨by omega, by nlinarith⟩, h3⟩
  have hinj : Set.InjOn f (Finset.range M : Set ℕ) := by
    have key : ∀ i j, i < M → j < M → i < j → f i < f j := by
      intro i j hi hj hij
      obtain ⟨_, h2, _⟩ := hf i hi
      obtain ⟨h1, _, _⟩ := hf j hj
      have : (i + 1) * L ≤ j * L := Nat.mul_le_mul_right L hij
      nlinarith
    intro i hi j hj hij
    have hi' : i < M := by simpa using hi
    have hj' : j < M := by simpa using hj
    rcases lt_trichotomy i j with hlt | heq | hgt
    · exact absurd hij (key i j hi' hj' hlt).ne
    · exact heq
    · exact absurd hij (key j i hj' hi' hgt).ne'
  have := Finset.card_le_card_of_injOn f hmaps hinj
  simpa using this

/-- `b` is the integer hundredth root of `k`. -/
private theorem t1_root (B k : ℕ) (hB : 2 ^ 100 ≤ B) (hk : B ^ 100 ≤ k) :
    ∃ b, B ≤ b ∧ b ^ 100 ≤ k ∧ k ≤ b ^ 101 := by
  have hex : ∃ x, k < (x + 1) ^ 100 := ⟨k, by
    calc k < k + 1 := Nat.lt_succ_self k
      _ ≤ (k + 1) ^ 100 := Nat.le_self_pow (by norm_num) _⟩
  classical
  obtain ⟨b, hb⟩ : ∃ b, b = Nat.find hex := ⟨_, rfl⟩
  have h1 : k < (b + 1) ^ 100 := hb ▸ Nat.find_spec hex
  have hBb : B ≤ b := by
    by_contra hcon
    have : (b + 1) ^ 100 ≤ B ^ 100 := Nat.pow_le_pow_left (by omega) 100
    omega
  have hb0 : 0 < b := lt_of_lt_of_le (lt_of_lt_of_le (by positivity) hB) hBb
  have h2 : b ^ 100 ≤ k := by
    by_contra hcon
    have hmin := Nat.find_min hex (m := b - 1) (by omega)
    apply hmin
    rw [Nat.sub_add_cancel hb0]
    omega
  refine ⟨b, hBb, h2, ?_⟩
  calc k ≤ (b + 1) ^ 100 := h1.le
    _ ≤ (2 * b) ^ 100 := Nat.pow_le_pow_left (by omega) 100
    _ = 2 ^ 100 * b ^ 100 := mul_pow _ _ _
    _ ≤ b * b ^ 100 := Nat.mul_le_mul_right _ (le_trans hB hBb)
    _ = b ^ 101 := by ring

private theorem t5_choose_g (M : ℕ) (n k b P ρ ρ' e β : ℕ) (hb : 2 ≤ b) (hn : 0 < n) (hk : 2 ≤ k) (hk2 : k ≤ b ^ (M + 101))
    (hρpos : 1 ≤ ρ) (hbase : ρ * P * ρ ≤ b) (hρG : ρ ≤ b) (he : ρ' + e = ρ + 1) (he2 : ρ' + (M + 101) * e ≤ 2 * ρ)
    (hβ1 : 1 ≤ β) (hβ2 : β ≤ P ^ ρ)
    (h1 : n * b ^ ρ ≤ k ^ ρ) (h2 : k ^ ρ' < n * b ^ ρ') :
    ∃ g : ℕ, ρ ≤ g ∧ g ≤ b ^ 2 ∧
      26 / 100 ≤ ((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ ∧
      ((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ ≤ 3 / 4 := by
  have hfac1 : 1 ≤ ρ.factorial := Nat.factorial_pos ρ
  have hkR : (0 : ℝ) < (k : ℝ) ^ (ρ + 1) := by
    have : (0 : ℝ) < k := by exact_mod_cast (by omega : 0 < k)
    positivity
  have hup : 4 * (n * ρ.factorial * β * ρ ^ ρ) ≤ 3 * k ^ (ρ + 1) := by
    have hf : ρ.factorial ≤ ρ ^ ρ := Nat.factorial_le_pow ρ
    have h3 : n * ρ.factorial * β * ρ ^ ρ ≤ n * b ^ ρ := by
      calc n * ρ.factorial * β * ρ ^ ρ ≤ n * ρ ^ ρ * P ^ ρ * ρ ^ ρ := by gcongr
        _ = n * (ρ * P * ρ) ^ ρ := by rw [mul_pow, mul_pow]; ring
        _ ≤ n * b ^ ρ := by gcongr
    have h4 : 2 * k ^ ρ ≤ k ^ (ρ + 1) := by
      rw [pow_succ, mul_comm]; exact Nat.mul_le_mul_left _ hk
    omega
  have hlow : k ^ (ρ + 1) ≤ n * ρ.factorial * β * (b ^ 2) ^ ρ := by
    calc k ^ (ρ + 1) = k ^ ρ' * k ^ e := by rw [← pow_add, he]
      _ ≤ (n * b ^ ρ') * (b ^ (M + 101)) ^ e := by gcongr
      _ = n * b ^ (ρ' + (M + 101) * e) := by rw [← pow_mul, mul_assoc, ← pow_add]
      _ ≤ n * b ^ (2 * ρ) := Nat.mul_le_mul_left _ (Nat.pow_le_pow_right (by omega) he2)
      _ = n * 1 * 1 * (b ^ 2) ^ ρ := by rw [← pow_mul]; ring
      _ ≤ n * ρ.factorial * β * (b ^ 2) ^ ρ := by gcongr
  have hc : (0 : ℝ) < ((n * ρ.factorial * β : ℕ) : ℝ) / (k : ℝ) ^ (ρ + 1) := by
    apply div_pos _ hkR
    exact_mod_cast Nat.mul_pos (Nat.mul_pos hn hfac1) hβ1
  obtain ⟨g, hg1, hg2, hg3, hg4⟩ := t1_tuning _ hc ρ (b ^ 2) hρpos
    (le_trans hρG (Nat.le_self_pow (by norm_num) b))
    (by
      rw [div_mul_eq_mul_div, div_le_div_iff₀ hkR (by norm_num)]
      have : (4 : ℝ) * (((n * ρ.factorial * β : ℕ) : ℝ) * (ρ : ℝ) ^ ρ) ≤ 3 * (k : ℝ) ^ (ρ + 1) := by
        exact_mod_cast hup
      linarith)
    (by
      rw [div_mul_eq_mul_div, div_le_div_iff₀ (by norm_num) hkR]
      have : (k : ℝ) ^ (ρ + 1) ≤ ((n * ρ.factorial * β : ℕ) : ℝ) * ((b ^ 2 : ℕ) : ℝ) ^ ρ := by
        exact_mod_cast hlow
      linarith)
  refine ⟨g, hg1, hg2, ?_, ?_⟩
  · have : ((26 / 100 : ℚ) : ℝ) ≤ ((((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ : ℚ) : ℝ) := by
      push_cast at hg3 ⊢
      exact hg3
    exact_mod_cast this
  · have : ((((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ : ℚ) : ℝ) ≤ ((3 / 4 : ℚ) : ℝ) := by
      push_cast at hg4 ⊢
      exact hg4
    exact_mod_cast this

/-- No translate of the scaled family in `(k, k + b^95]` is wholly bad, for a step list `S` with
norm at most `(N+1)·8b`. -/
private theorem t5_window (M : ℕ) (n k b P ρ' e : ℕ) (S : List ℕ) (hb : 2 ^ 100 ≤ b) (hn : 0 < n) (hk1 : b ^ (M + 100) ≤ k)
    (hk2 : k ≤ b ^ (M + 101)) (hSmem : ∀ s ∈ S, 1 ≤ s ∧ s ≤ P) (hρpos : 1 ≤ S.length)
    (hbase : S.length * P * S.length ≤ b) (hNb : S.length * P + 1 ≤ b) (hρb : S.length + 1 ≤ b)
    (he : ρ' + e = S.length + 1) (he2 : ρ' + (M + 101) * e ≤ 2 * S.length)
    (hnorm : ∑ j ∈ Finset.range (S.sum + 1), |((coefL S j : ℤ) : ℝ)| ≤ ((S.sum : ℝ) + 1) * (8 * (b : ℝ)))
    (h1 : n * b ^ S.length ≤ k ^ S.length) (h2 : k ^ ρ' < n * b ^ ρ')
    (m : ℕ) (hm1 : k < m) (hm2 : m ≤ k + b ^ (M + 95)) :
    ∃ d, m ≤ d ∧ d < m + b ^ 3 ∧ n % d < k := by
  have hSne : S ≠ [] := by
    intro h
    rw [h] at hρpos
    simp at hρpos
  have hN1 : S.length ≤ S.sum := by
    have h := List.card_nsmul_le_sum S 1 (fun s hs => (hSmem s hs).1)
    simpa using h
  have hN2 : S.sum ≤ S.length * P := by
    have h := List.sum_le_card_nsmul S P (fun s hs => (hSmem s hs).2)
    simpa using h
  have hβ1 : 1 ≤ S.prod := List.one_le_prod_of_one_le (fun s hs => (hSmem s hs).1)
  have hβ2 : S.prod ≤ P ^ S.length := List.prod_le_pow_card S P (fun s hs => (hSmem s hs).2)
  have hnb := fun g hg hk hm => no_bad_translate_scaled n k m g S hSne hg hk hm
  generalize hA : ∑ j ∈ Finset.range (S.sum + 1), |(coefL S j : ℚ)| = A at hnb
  have hAR : (A : ℝ) ≤ ((S.sum : ℝ) + 1) * (8 * (b : ℝ)) := by
    rw [← hA]
    simp only [Rat.cast_sum, Rat.cast_abs, Rat.cast_intCast]
    exact hnorm
  have hA0 : 0 ≤ A := by
    rw [← hA]
    exact Finset.sum_nonneg (fun _ _ => abs_nonneg _)
  clear hnorm hSmem hSne
  generalize S.length = ρ at *
  generalize S.sum = N at *
  generalize S.prod = β at *
  have hb1 : 1 ≤ b := le_trans (Nat.one_le_two_pow) hb
  have hb64 : 64 ≤ b := le_trans (by norm_num) hb
  have hkb : b ≤ k := le_trans (Nat.le_self_pow (by norm_num) b) hk1
  obtain ⟨g, hg1, hg2, hg3, hg4⟩ := t5_choose_g M n k b P ρ ρ' e β (by omega) hn (by omega) hk2 hρpos hbase
    (by omega) he he2 hβ1 hβ2 h1 h2
  have hNp : N + 1 ≤ b := by omega
  have hgN : g * N + 1 ≤ b ^ 3 := by
    have : g * (N + 1) ≤ b ^ 2 * b := Nat.mul_le_mul hg2 hNp
    have e : b ^ 2 * b = b ^ 3 := by ring
    rw [e] at this
    nlinarith
  have hg0 : 0 < g := by omega
  have h95 : b ^ 3 ≤ b ^ (M + 95) := Nat.pow_le_pow_right hb1 (by norm_num)
  obtain ⟨j, hj1, _, hj3⟩ := hnb g hg0 (by omega) hm1
    (by
      have hlen2 : m + g * N - k ≤ 2 * b ^ (M + 95) := by omega
      have hlen2R : ((m + g * N - k : ℕ) : ℝ) ≤ 2 * (b : ℝ) ^ (M + 95) := by exact_mod_cast hlen2
      have hNR : (N : ℝ) + 1 ≤ b := by exact_mod_cast hNp
      have hbR : (64 : ℝ) ≤ b := by exact_mod_cast hb64
      have hmR : (b : ℝ) ^ (M + 100) < ((m + g * N : ℕ) : ℝ) := by
        have : b ^ (M + 100) < m + g * N := by omega
        exact_mod_cast this
      have hA0R : (0 : ℝ) ≤ A := by exact_mod_cast hA0
      have hAb : (A : ℝ) ≤ (b : ℝ) * (8 * (b : ℝ)) :=
        le_trans hAR (mul_le_mul_of_nonneg_right hNR (by positivity))
      have hR : ((2 * A * ((m + g * N - k : ℕ) : ℚ) : ℚ) : ℝ) < (((m + g * N : ℕ) : ℚ) : ℝ) := by
        simp only [Rat.cast_mul, Rat.cast_ofNat, Rat.cast_natCast]
        calc 2 * (A : ℝ) * ((m + g * N - k : ℕ) : ℝ)
            ≤ 2 * ((b : ℝ) * (8 * (b : ℝ))) * (2 * (b : ℝ) ^ (M + 95)) := by gcongr
          _ = 32 * (b : ℝ) ^ (M + 97) := by ring
          _ ≤ (b : ℝ) ^ 3 * (b : ℝ) ^ (M + 97) := by
              gcongr
              nlinarith [pow_le_pow_left₀ (by norm_num : (0 : ℝ) ≤ 64) hbR 3]
          _ = (b : ℝ) ^ (M + 100) := by ring
          _ < _ := hmR
      exact_mod_cast hR)
    (by
      have hkQ : (0 : ℚ) < k := by exact_mod_cast (by omega : 0 < k)
      have hρN : ρ ≤ g * N := le_trans hg1 (Nat.le_mul_of_pos_right g (by omega))
      have hle := t1_le_gfun ρ ((m : ℚ) + ((g * N - ρ : ℕ) : ℚ)) ((k : ℚ) + 2 * (b : ℚ) ^ (M + 95))
        (by
          have : (0 : ℚ) < m := by exact_mod_cast (by omega : 0 < m)
          positivity)
        (by
          have : m + (g * N - ρ) + ρ ≤ k + 2 * b ^ (M + 95) := by omega
          exact_mod_cast this)
      have hbQ : (64 : ℚ) ≤ b := by exact_mod_cast hb64
      have hkbQ : (b : ℚ) ^ (M + 100) ≤ k := by exact_mod_cast hk1
      have hρQ : (ρ : ℚ) + 1 ≤ b := by
        have : ρ + 1 ≤ b := by omega
        exact_mod_cast this
      obtain ⟨h, hh⟩ : ∃ h : ℚ, h = 2 * (b : ℚ) ^ (M + 95) := ⟨_, rfl⟩
      rw [← hh] at hle
      have hh0 : 0 < h := by rw [hh]; positivity
      have hkh : (0 : ℚ) < k + h := by linarith
      -- Bernoulli
      have hbern := one_add_mul_le_pow (a := -(h / (k + h)))
        (by
          have : h / (k + h) ≤ 1 := by rw [div_le_one hkh]; linarith
          linarith) (ρ + 1)
      have hratio : 1 + -(h / (k + h)) = k / (k + h) := by field_simp; ring
      rw [hratio, div_pow] at hbern
      have hsmall : ((ρ + 1 : ℕ) : ℚ) * (h / (k + h)) ≤ 1 / 26 := by
        rw [← mul_div_assoc, div_le_iff₀ hkh]
        have e1 : ((ρ + 1 : ℕ) : ℚ) * h ≤ b * h := by
          apply mul_le_mul_of_nonneg_right _ hh0.le
          push_cast
          exact hρQ
        have e2 : (b : ℚ) * h * 26 ≤ (b : ℚ) ^ (M + 100) := by
          rw [hh]
          calc (b : ℚ) * (2 * (b : ℚ) ^ (M + 95)) * 26 = 52 * (b : ℚ) ^ (M + 96) := by ring
            _ ≤ (b : ℚ) ^ 4 * (b : ℚ) ^ (M + 96) := by
                gcongr
                have := pow_le_pow_left₀ (by norm_num : (0 : ℚ) ≤ 64) hbQ 4
                linarith
            _ = (b : ℚ) ^ (M + 100) := by ring
        linarith
      have hB : 25 / 26 ≤ (k : ℚ) ^ (ρ + 1) / (k + h) ^ (ρ + 1) := by linarith
      have hkp : (0 : ℚ) < (k : ℚ) ^ (ρ + 1) := by positivity
      have hkhp : (0 : ℚ) < (k + h) ^ (ρ + 1) := by positivity
      calc (1 / 4 : ℚ) = 26 / 100 * (25 / 26) := by norm_num
        _ ≤ (((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ) *
              ((k : ℚ) ^ (ρ + 1) / (k + h) ^ (ρ + 1)) := by
            apply mul_le_mul hg3 hB (by norm_num)
            exact le_trans (by norm_num) hg3
        _ = (n : ℚ) * (((g ^ ρ * β : ℕ) : ℚ) * ((ρ.factorial : ℚ) / (k + h) ^ (ρ + 1))) := by
            push_cast
            field_simp
        _ ≤ _ := by gcongr)
    (by
      have hkQ : (0 : ℚ) < k := by exact_mod_cast (by omega : 0 < k)
      have hle := t1_gfun_le ρ (m : ℚ) (k : ℚ) hkQ (by exact_mod_cast hm1.le)
      calc (n : ℚ) * (((g ^ ρ * β : ℕ) : ℚ) * gfun ρ (m : ℚ))
          ≤ (n : ℚ) * (((g ^ ρ * β : ℕ) : ℚ) * ((ρ.factorial : ℚ) / (k : ℚ) ^ (ρ + 1))) := by gcongr
        _ = ((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ := by
            push_cast
            field_simp
        _ ≤ 3 / 4 := hg4)
  refine ⟨m + g * j, by omega, ?_, hj3⟩
  have : g * j ≤ g * N := Nat.mul_le_mul_left g hj1
  omega


private theorem t5_root (M : ℕ) (B k : ℕ) (hB : 2 ^ (M + 100) ≤ B) (hk : B ^ (M + 100) ≤ k) :
    ∃ b, B ≤ b ∧ b ^ (M + 100) ≤ k ∧ k ≤ b ^ (M + 101) := by
  have hex : ∃ x, k < (x + 1) ^ (M + 100) := ⟨k, by
    calc k < k + 1 := Nat.lt_succ_self k
      _ ≤ (k + 1) ^ (M + 100) := Nat.le_self_pow (by norm_num) _⟩
  classical
  obtain ⟨b, hb⟩ : ∃ b, b = Nat.find hex := ⟨_, rfl⟩
  have h1 : k < (b + 1) ^ (M + 100) := hb ▸ Nat.find_spec hex
  have hBb : B ≤ b := by
    by_contra hcon
    have : (b + 1) ^ (M + 100) ≤ B ^ (M + 100) := Nat.pow_le_pow_left (by omega) (M + 100)
    omega
  have hb0 : 0 < b := lt_of_lt_of_le (lt_of_lt_of_le (by positivity) hB) hBb
  have h2 : b ^ (M + 100) ≤ k := by
    by_contra hcon
    have hmin := Nat.find_min hex (m := b - 1) (by omega)
    apply hmin
    rw [Nat.sub_add_cancel hb0]
    omega
  refine ⟨b, hBb, h2, ?_⟩
  calc k ≤ (b + 1) ^ (M + 100) := h1.le
    _ ≤ (2 * b) ^ (M + 100) := Nat.pow_le_pow_left (by omega) (M + 100)
    _ = 2 ^ (M + 100) * b ^ (M + 100) := mul_pow _ _ _
    _ ≤ b * b ^ (M + 100) := Nat.mul_le_mul_right _ (le_trans hB hBb)
    _ = b ^ (M + 101) := by ring

private theorem t5_pow_le_exp (D : ℕ) : ∃ X : ℝ, 1 ≤ X ∧ ∀ x : ℝ, X ≤ x → x ^ D ≤ Real.exp x := by
  have h := (Real.tendsto_exp_div_pow_atTop D).eventually_ge_atTop 1
  obtain ⟨X, hX⟩ := Filter.eventually_atTop.mp h
  refine ⟨max X 1, le_max_right _ _, fun x hx => ?_⟩
  have h1 := hX x (le_trans (le_max_left _ _) hx)
  have hx0 : 0 < x := lt_of_lt_of_le one_pos (le_trans (le_max_right _ _) hx)
  rwa [le_div_iff₀ (pow_pos hx0 D), one_mul] at h1

/-- The least `ρ ≥ R` with `n·b^ρ ≤ k^ρ`. -/
private theorem t5_choose (R n k b : ℕ) (hb : 2 ≤ b) (hk : 2 * b ≤ k) (hn : k ^ (R + 1) ≤ n) :
    ∃ ρ, R + 1 ≤ ρ ∧ n * b ^ ρ ≤ k ^ ρ ∧ k ^ (ρ - 1) < n * b ^ (ρ - 1) := by
  classical
  have hex : ∃ ρ, R ≤ ρ ∧ n * b ^ ρ ≤ k ^ ρ := by
    refine ⟨n + R, by omega, ?_⟩
    calc n * b ^ (n + R) ≤ 2 ^ (n + R) * b ^ (n + R) := by
          apply Nat.mul_le_mul_right
          exact le_trans Nat.lt_two_pow_self.le (Nat.pow_le_pow_right (by norm_num) (by omega))
      _ = (2 * b) ^ (n + R) := (mul_pow _ _ _).symm
      _ ≤ k ^ (n + R) := Nat.pow_le_pow_left hk _
  obtain ⟨q, hq⟩ : ∃ q, q = Nat.find hex := ⟨_, rfl⟩
  have h1 : R ≤ q ∧ n * b ^ q ≤ k ^ q := by rw [hq]; exact Nat.find_spec hex
  have h2 : R + 1 ≤ q := by
    by_contra hcon
    have e : q = R := by omega
    have h2 := h1.2
    rw [e] at h2
    have h3 : k ^ R < k ^ (R + 1) := Nat.pow_lt_pow_right (by omega) (by omega)
    have h4 : n * 1 ≤ n * b ^ R := Nat.mul_le_mul_left n (Nat.one_le_pow _ _ (by omega))
    omega
  refine ⟨q, h2, h1.2, ?_⟩
  have h5 := Nat.find_min hex (m := q - 1) (by omega)
  by_contra h6
  exact h5 ⟨by omega, not_lt.mp h6⟩

/-- The budget for `log n ≤ log^A k`. -/
private theorem t5_budget (A M E : ℕ) : ∃ B : ℕ, ∀ (n k b ρ : ℕ) (T : ℝ), B ≤ b → b ^ (M + 100) ≤ k →
    k ≤ b ^ (M + 101) → 0 < n → 2 ≤ ρ → k ^ (ρ - 1) < n * b ^ (ρ - 1) →
    Real.log (n : ℝ) ≤ Real.log (k : ℝ) ^ A → 0 ≤ T → T ^ (2 * (A + 1)) ≤ (ρ : ℝ) →
    Real.exp T ≤ (b : ℝ) ∧ ρ ^ (E + 2) ≤ b := by
  obtain ⟨X, hX1, hX⟩ := t5_pow_le_exp (2 * (A + 1) * (E + 2))
  refine ⟨⌈Real.exp (max X (M + 101))⌉₊, ?_⟩
  intro n k b ρ T hB hk1 hk2 hn hρ h2 hlog hT0 hT
  have hbe : Real.exp (max X (M + 101)) ≤ (b : ℝ) := Nat.le_of_ceil_le hB
  have hbR : (0 : ℝ) < b := lt_of_lt_of_le (Real.exp_pos _) hbe
  obtain ⟨lb, hlb⟩ : ∃ x, x = Real.log (b : ℝ) := ⟨_, rfl⟩
  have hlbmax : max X (M + 101) ≤ lb := by rw [hlb]; exact (Real.le_log_iff_exp_le hbR).mpr hbe
  have hlbX : X ≤ lb := le_trans (le_max_left _ _) hlbmax
  have hlbM : (M : ℝ) + 101 ≤ lb := le_trans (le_max_right _ _) hlbmax
  have hM0 : (0 : ℝ) ≤ M := Nat.cast_nonneg M
  have hb0 : 0 < b := by exact_mod_cast hbR
  have hkR : (0 : ℝ) < k := by
    have : 0 < k := lt_of_lt_of_le (Nat.pow_pos hb0) hk1
    exact_mod_cast this
  have hnR : (0 : ℝ) < n := by exact_mod_cast hn
  obtain ⟨L, hL⟩ : ∃ x, x = Real.log (k : ℝ) := ⟨_, rfl⟩
  rw [← hL] at hlog
  have hL1 : ((M : ℝ) + 100) * lb ≤ L := by
    have := Real.log_le_log (by positivity) (show ((b : ℝ)) ^ (M + 100) ≤ k by exact_mod_cast hk1)
    rw [Real.log_pow] at this
    rw [hlb, hL]
    push_cast at this
    exact this
  have hL2 : L ≤ ((M : ℝ) + 101) * lb := by
    have := Real.log_le_log hkR (show (k : ℝ) ≤ (b : ℝ) ^ (M + 101) by exact_mod_cast hk2)
    rw [Real.log_pow] at this
    rw [hlb, hL]
    push_cast at this
    exact this
  have hlb0 : 0 < lb := by linarith
  have hL100 : 100 * lb ≤ L := le_trans (mul_le_mul_of_nonneg_right (by linarith) hlb0.le) hL1
  have hLsq : L ≤ lb ^ 2 := by
    rw [sq]
    exact le_trans hL2 (mul_le_mul_of_nonneg_right hlbM hlb0.le)
  have hL10 : 100 ≤ L := by linarith
  have hρ1 : ((ρ - 1 : ℕ) : ℝ) * (99 / 100 * L) ≤ L ^ A := by
    have h3 : Real.log ((k : ℝ) ^ (ρ - 1)) ≤ Real.log ((n : ℝ) * (b : ℝ) ^ (ρ - 1)) :=
      Real.log_le_log (by positivity) (by exact_mod_cast h2.le)
    rw [Real.log_mul hnR.ne' (by positivity), Real.log_pow, Real.log_pow, ← hlb, ← hL] at h3
    have h4 : ((ρ - 1 : ℕ) : ℝ) * lb ≤ ((ρ - 1 : ℕ) : ℝ) * (1 / 100 * L) :=
      mul_le_mul_of_nonneg_left (by linarith) (Nat.cast_nonneg _)
    linarith
  have hρL : (ρ : ℝ) ≤ L ^ (A + 1) := by
    have h3 : (ρ : ℝ) ≤ 2 * ((ρ - 1 : ℕ) : ℝ) := by
      have : ρ ≤ 2 * (ρ - 1) := by omega
      exact_mod_cast this
    have h4 : ((ρ - 1 : ℕ) : ℝ) * 2 ≤ ((ρ - 1 : ℕ) : ℝ) * (99 / 100 * L) :=
      mul_le_mul_of_nonneg_left (by linarith) (Nat.cast_nonneg _)
    have h5 : L ^ A * 1 ≤ L ^ A * L := mul_le_mul_of_nonneg_left (by linarith) (by positivity)
    rw [pow_succ]
    linarith
  constructor
  · have h3 : (T ^ 2) ^ (A + 1) ≤ L ^ (A + 1) := by
      rw [← pow_mul]
      exact le_trans hT hρL
    have h4 : T ^ 2 ≤ L := le_of_pow_le_pow_left₀ (by omega) (by linarith) h3
    have h5 : T ≤ lb := le_of_pow_le_pow_left₀ (show (2 : ℕ) ≠ 0 by norm_num) hlb0.le (le_trans h4 hLsq)
    rw [← Real.le_log_iff_exp_le hbR, ← hlb]
    exact h5
  · have h3 : (ρ : ℝ) ^ (E + 2) ≤ (b : ℝ) := by
      calc (ρ : ℝ) ^ (E + 2) ≤ ((lb ^ 2) ^ (A + 1)) ^ (E + 2) := by
            apply pow_le_pow_left₀ (Nat.cast_nonneg _)
            exact le_trans hρL (pow_le_pow_left₀ (by linarith) hLsq _)
        _ = lb ^ (2 * (A + 1) * (E + 2)) := by rw [← pow_mul, ← pow_mul, mul_assoc]
        _ ≤ Real.exp lb := hX lb hlbX
        _ = b := by rw [hlb, Real.exp_log hbR]
    exact_mod_cast h3

private theorem t5_dense (hprod : ∀ (j : ℕ), ∃ (E ρ₀ : ℕ), ∀ (ρ : ℕ), ρ₀ ≤ ρ → ∃ (s : ℕ → ℕ),
      (∀ (i : ℕ), i < ρ → (1 : ℕ) ≤ s i ∧ s i ≤ ρ ^ E) ∧
        ∃ (T : ℝ), (0 : ℝ) ≤ T ∧ T ^ j ≤ (↑ρ : ℝ) ∧
          ∀ (N : ℕ), ∑ i ∈ Finset.range (N + (1 : ℕ)),
            |(↑((∏ t ∈ Finset.range ρ, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ s t)).coeff i) : ℝ)| ≤
              ((N : ℝ) + 1) * (8 * Real.exp T)) :
    ∀ (A : ℕ), ∃ (u₀ K₀ : ℕ), ∀ (n k : ℕ), K₀ ≤ k → k ^ u₀ ≤ n →
      Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ A →
      k ^ (A + (1 : ℕ)) ≤ (Finset.card (Finset.filter (fun (d : ℕ) => n % d < k) (Finset.Ioc k ((2 : ℕ) * k)))) ^ (A + (2 : ℕ)) := by
  intro A
  obtain ⟨M, hM⟩ : ∃ M, M = 9 * A := ⟨_, rfl⟩
  obtain ⟨E, ρ₀, hfam⟩ := hprod (2 * (A + 1))
  obtain ⟨B1, hB1⟩ := t5_budget A M E
  obtain ⟨R, hR1, hR2⟩ : ∃ R, ρ₀ ≤ R ∧ 2 * M + 202 ≤ R :=
    ⟨max ρ₀ (2 * M + 202), le_max_left _ _, le_max_right _ _⟩
  obtain ⟨B, hBa, hBb'⟩ : ∃ B : ℕ, B1 ≤ B ∧ 2 ^ (M + 100) ≤ B := ⟨max B1 (2 ^ (M + 100)), le_max_left _ _, le_max_right _ _⟩
  refine ⟨R + 1, B ^ (M + 100), ?_⟩
  intro n k hK hn hrange
  obtain ⟨b, hBb, hk1, hk2⟩ := t5_root M B k hBb' hK
  have hb : 2 ^ 100 ≤ b := le_trans (Nat.pow_le_pow_right (by norm_num) (by omega)) (le_trans hBb' hBb)
  have hb2 : 2 ≤ b := le_trans (by norm_num) hb
  have hk2b : 2 * b ≤ k := by
    calc 2 * b ≤ b * b := Nat.mul_le_mul_right b hb2
      _ = b ^ 2 := (sq b).symm
      _ ≤ b ^ (M + 100) := Nat.pow_le_pow_right (by omega) (by omega)
      _ ≤ k := hk1
  have hn0 : 0 < n := lt_of_lt_of_le (Nat.pow_pos (by omega)) hn
  obtain ⟨ρ, hρ, h1, h2⟩ := t5_choose R n k b hb2 hk2b hn
  obtain ⟨s, hs, T, hT0, hT, hsum⟩ := hfam ρ (by omega)
  obtain ⟨hexp, hpow⟩ := hB1 n k b ρ T (le_trans hBa hBb) hk1 hk2 hn0 (by omega) h2 hrange hT0 hT
  obtain ⟨S, hS⟩ : ∃ S, S = (Finset.range ρ).toList.map s := ⟨_, rfl⟩
  have hlen : S.length = ρ := by rw [hS, List.length_map, Finset.length_toList, Finset.card_range]
  have hSmem : ∀ x ∈ S, 1 ≤ x ∧ x ≤ ρ ^ E := by
    intro x hx
    rw [hS, List.mem_map] at hx
    obtain ⟨t, ht, rfl⟩ := hx
    rw [Finset.mem_toList, Finset.mem_range] at ht
    exact hs t ht
  have hpoly : (S.map (fun s => (1 - Polynomial.X ^ s : Polynomial ℤ))).prod =
      ∏ t ∈ Finset.range ρ, (1 - (Polynomial.X : Polynomial ℤ) ^ s t) := by
    rw [hS, List.map_map]
    exact Finset.prod_map_toList _ _
  have hP1 : 1 ≤ ρ ^ E := Nat.one_le_pow _ _ (by omega)
  have hbase : ρ * ρ ^ E * ρ ≤ b := by
    have e : ρ * ρ ^ E * ρ = ρ ^ (E + 2) := by ring
    rw [e]
    exact hpow
  have hNb : ρ * ρ ^ E + 1 ≤ b := by
    have h3 : ρ * ρ ^ E * 2 ≤ ρ * ρ ^ E * ρ := Nat.mul_le_mul_left _ (by omega)
    have h4 : 1 ≤ ρ * ρ ^ E := Nat.mul_pos (by omega) hP1
    omega
  have hρb : ρ + 1 ≤ b := by
    have : ρ * 1 ≤ ρ * ρ ^ E := Nat.mul_le_mul_left _ hP1
    omega
  have hl2 : ∑ j ∈ Finset.range (S.sum + 1), |((coefL S j : ℤ) : ℝ)| ≤ ((S.sum : ℝ) + 1) * (8 * (b : ℝ)) := by
    simp only [coefL_eq_coeff, hpoly]
    refine le_trans (hsum S.sum) (mul_le_mul_of_nonneg_left ?_ (by positivity))
    linarith
  have e95 : b ^ 3 * b ^ (M + 92) = b ^ (M + 95) := by rw [← pow_add]; congr 1; omega
  have hcount := t1_blocks (fun d => n % d < k) k (b ^ 3) (b ^ (M + 92))
    (by rw [e95]; exact le_trans (Nat.pow_le_pow_right (by omega) (by omega)) hk1)
    (by
      intro m hm1 hm2
      rw [e95] at hm2
      exact t5_window M n k b (ρ ^ E) (ρ - 1) 2 S hb hn0 hk1 hk2
        hSmem (by omega) (by rw [hlen]; exact hbase) (by rw [hlen]; exact hNb) (by rw [hlen]; exact hρb)
        (by omega) (by omega) hl2 (by rw [hlen]; exact h1) h2 m hm1 hm2)
    (Nat.pow_pos (by omega))
  have h9 : (b ^ (M + 101)) ^ (A + 1) ≤ (b ^ (M + 92)) ^ (A + 2) := by
    rw [← pow_mul, ← pow_mul]
    apply Nat.pow_le_pow_right (by omega)
    rw [hM]
    nlinarith
  exact le_trans (Nat.pow_le_pow_left hk2 _) (le_trans h9 (Nat.pow_le_pow_left hcount _))

/-- A cosine sum with non-negative integer coefficients of total `ρ`, bounded below by `-Q`, as a family of
`ρ` exponents with a bound for the coefficients of `∏ (1 - X^s)`. -/
private theorem t5_fam (ρ : ℕ) (hρ2 : 2 ≤ ρ) (Q : ℝ) (a : ℕ → ℕ) (ha1 : ∑ k ∈ Finset.Icc (1 : ℕ) ρ, a k = ρ)
    (ha2 : ∀ (x : ℝ), -Q ≤ ∑ k ∈ Finset.Icc (1 : ℕ) ρ, (↑(a k) : ℝ) * Real.cos ((↑k : ℝ) * x)) :
    ∃ (s : ℕ → ℕ), (∀ (i : ℕ), i < ρ → (1 : ℕ) ≤ s i ∧ s i ≤ ρ) ∧
      ∀ (N : ℕ), ∑ i ∈ Finset.range (N + (1 : ℕ)),
        |(↑((∏ t ∈ Finset.range ρ, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ s t)).coeff i) : ℝ)| ≤
          ((N : ℝ) + 1) * (8 * Real.exp (Q * Real.log ρ)) := by
  have hρR : (0 : ℝ) < ρ := by exact_mod_cast (by omega : 0 < ρ)
  obtain ⟨F, hF⟩ : ∃ F, F = (Finset.Icc 1 ρ).sigma (fun d => Finset.range (a d)) := ⟨_, rfl⟩
  have hcard : F.card = ρ := by
    rw [hF, Finset.card_sigma]
    simp only [Finset.card_range]
    exact ha1
  have hmem : ∀ t ∈ F, 1 ≤ t.1 ∧ t.1 ≤ ρ := by
    intro t ht
    rw [hF, Finset.mem_sigma, Finset.mem_Icc] at ht
    exact ht.1
  obtain ⟨e⟩ : Nonempty (F ≃ Fin ρ) := ⟨Fintype.equivFinOfCardEq (by rw [Fintype.card_coe]; exact hcard)⟩
  obtain ⟨s, hs⟩ : ∃ s : ℕ → ℕ, ∀ i : Fin ρ, s i = (e.symm i).1.1 :=
    ⟨fun i => if h : i < ρ then (e.symm ⟨i, h⟩).1.1 else 1, fun i => by simp⟩
  have hprod : ∏ t ∈ Finset.range ρ, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ s t) =
      ∏ t ∈ F, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ t.1) := by
    rw [Finset.prod_range, ← Finset.prod_coe_sort F (fun t => (1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ t.1)]
    refine Fintype.prod_equiv e.symm _ _ (fun i => ?_)
    rw [hs i]
  refine ⟨s, ?_, ?_⟩
  · intro i hi
    have h2 : s i = (e.symm ⟨i, hi⟩).1.1 := hs ⟨i, hi⟩
    rw [h2]
    exact hmem _ (e.symm ⟨i, hi⟩).2
  · intro N
    rw [hprod]
    have h := t1_l1_product_int F (fun t => t.1) Q
      (by
        intro w hw
        have hsig : ∑ t ∈ F, (w ^ t.1).re = ∑ d ∈ Finset.Icc 1 ρ, (a d : ℝ) * (w ^ d).re := by
          rw [hF, Finset.sum_sigma]
          simp only [Finset.sum_const, Finset.card_range, nsmul_eq_mul]
        rw [hsig]
        have h1 := ha2 w.arg
        have hw2 : w = Complex.exp (w.arg * Complex.I) := by
          have := Complex.norm_mul_exp_arg_mul_I w
          rw [hw] at this
          simpa using this.symm
        have h2 : ∀ d : ℕ, (w ^ d).re = Real.cos ((d : ℝ) * w.arg) := by
          intro d
          conv_lhs => rw [hw2]
          rw [← Complex.exp_nat_mul, ← mul_assoc]
          have : ((d : ℂ) * (w.arg : ℂ)) = (((d : ℝ) * w.arg : ℝ) : ℂ) := by push_cast; rfl
          rw [this, Complex.exp_ofReal_mul_I_re]
        simp only [h2]
        exact h1)
      (by rw [hcard]; exact hρ2) N
    rw [hcard] at h
    have hr : (ρ : ℝ) ^ Q = Real.exp (Q * Real.log ρ) := by
      rw [Real.rpow_def_of_pos hρR, mul_comm]
    rw [hr] at h
    exact h

/-- The least `ν` with `ρ ≤ ν^q` has `ν^q ≤ 2^q·ρ`. -/
private theorem t5_root_up (q ρ : ℕ) (hq : 1 ≤ q) (hρ : 2 ≤ ρ) : ∃ ν : ℕ, ρ ≤ ν ^ q ∧ ν ^ q ≤ 2 ^ q * ρ := by
  classical
  have hex : ∃ ν, ρ ≤ ν ^ q := ⟨ρ, Nat.le_self_pow (by omega) ρ⟩
  obtain ⟨ν, hν⟩ : ∃ ν, ν = Nat.find hex := ⟨_, rfl⟩
  have h1 : ρ ≤ ν ^ q := hν ▸ Nat.find_spec hex
  have h2 : 2 ≤ ν := by
    by_contra hcon
    have : ν ^ q ≤ 1 ^ q := Nat.pow_le_pow_left (by omega) q
    rw [one_pow] at this
    omega
  have h3 : (ν - 1) ^ q < ρ := by
    have := Nat.find_min hex (m := ν - 1) (by omega)
    omega
  refine ⟨ν, h1, ?_⟩
  calc ν ^ q ≤ (2 * (ν - 1)) ^ q := Nat.pow_le_pow_left (by omega) q
    _ = 2 ^ q * (ν - 1) ^ q := mul_pow _ _ _
    _ ≤ 2 ^ q * ρ := Nat.mul_le_mul_left _ h3.le

private theorem t5_cosq (hcos : ∀ q : ℕ, 2 ≤ q → ∃ C : ℕ, ∀ ν ρ : ℕ, 1 ≤ ρ → ρ ≤ ν ^ q → ∃ a : ℕ → ℕ,
      (∑ k ∈ Finset.Icc 1 ρ, a k = ρ) ∧
        ∀ x : ℝ, -((C : ℝ) * ν) ≤ ∑ k ∈ Finset.Icc 1 ρ, (a k : ℝ) * Real.cos (k * x)) :
    ∀ (j : ℕ), ∃ (E ρ₀ : ℕ), ∀ (ρ : ℕ), ρ₀ ≤ ρ → ∃ (s : ℕ → ℕ),
      (∀ (i : ℕ), i < ρ → (1 : ℕ) ≤ s i ∧ s i ≤ ρ ^ E) ∧
        ∃ (T : ℝ), (0 : ℝ) ≤ T ∧ T ^ j ≤ (↑ρ : ℝ) ∧
          ∀ (N : ℕ), ∑ i ∈ Finset.range (N + (1 : ℕ)),
            |(↑((∏ t ∈ Finset.range ρ, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ s t)).coeff i) : ℝ)| ≤
              ((N : ℝ) + 1) * (8 * Real.exp T) := by
  intro j
  obtain ⟨C, hC⟩ := hcos (2 * (j + 1)) (by omega)
  obtain ⟨X, hX1, hX⟩ := t5_pow_le_exp (4 * (j + 1) + 1)
  refine ⟨1, ⌈Real.exp (max X (max (2 * C) 4))⌉₊, ?_⟩
  intro ρ hρ
  have hρe : Real.exp (max X (max (2 * C) 4)) ≤ (ρ : ℝ) := Nat.le_of_ceil_le hρ
  have hρR : (0 : ℝ) < ρ := lt_of_lt_of_le (Real.exp_pos _) hρe
  have hlog : max X (max (2 * C) 4) ≤ Real.log ρ := (Real.le_log_iff_exp_le hρR).mpr hρe
  have hlX : X ≤ Real.log ρ := le_trans (le_max_left _ _) hlog
  have hlC : 2 * (C : ℝ) ≤ Real.log ρ := le_trans (le_trans (le_max_left _ _) (le_max_right _ _)) hlog
  have hl4 : 4 ≤ Real.log ρ := le_trans (le_trans (le_max_right _ _) (le_max_right _ _)) hlog
  have hexpl : Real.exp (Real.log ρ) = ρ := Real.exp_log hρR
  have hρ2 : 2 ≤ ρ := by
    have h1 := Real.add_one_le_exp (Real.log ρ)
    rw [hexpl] at h1
    have : (2 : ℝ) ≤ ρ := by linarith
    exact_mod_cast this
  obtain ⟨ν, hν1, hν2⟩ := t5_root_up (2 * (j + 1)) ρ (by omega) hρ2
  obtain ⟨a, ha1, ha2⟩ := hC ν ρ (by omega) hν1
  obtain ⟨s, hs1, hs2⟩ := t5_fam ρ hρ2 (C * ν) a ha1 ha2
  generalize Real.log (ρ : ℝ) = l at *
  have hT0 : 0 ≤ (C : ℝ) * ν * l := by positivity
  refine ⟨s, fun i hi => by rw [pow_one]; exact hs1 i hi, C * ν * l, hT0, ?_, hs2⟩
  have hCl : (2 * (C : ℝ) * l) ^ (2 * (j + 1)) ≤ ρ := by
    have h1 : 2 * (C : ℝ) * l ≤ l ^ 2 := by
      rw [sq]
      exact mul_le_mul_of_nonneg_right hlC (by linarith)
    have h2 : (2 * (C : ℝ) * l) ^ (2 * (j + 1)) ≤ l ^ (4 * (j + 1)) := by
      calc (2 * (C : ℝ) * l) ^ (2 * (j + 1)) ≤ (l ^ 2) ^ (2 * (j + 1)) := pow_le_pow_left₀ (by positivity) h1 _
        _ = l ^ (4 * (j + 1)) := by rw [← pow_mul]; congr 1; ring
    have h3 := hX l hlX
    rw [hexpl, pow_succ] at h3
    have h4 : l ^ (4 * (j + 1)) * 1 ≤ l ^ (4 * (j + 1)) * l := mul_le_mul_of_nonneg_left (by linarith) (by positivity)
    linarith
  have hνR : (ν : ℝ) ^ (2 * (j + 1)) ≤ 2 ^ (2 * (j + 1)) * ρ := by exact_mod_cast hν2
  have hmain : (((C : ℝ) * ν * l) ^ (j + 1)) ^ 2 ≤ (ρ : ℝ) ^ 2 := by
    calc (((C : ℝ) * ν * l) ^ (j + 1)) ^ 2 = ((C : ℝ) * l) ^ (2 * (j + 1)) * (ν : ℝ) ^ (2 * (j + 1)) := by
          rw [← pow_mul, ← mul_pow]
          congr 1 <;> ring
      _ ≤ ((C : ℝ) * l) ^ (2 * (j + 1)) * (2 ^ (2 * (j + 1)) * ρ) :=
          mul_le_mul_of_nonneg_left hνR (by positivity)
      _ = (2 * (C : ℝ) * l) ^ (2 * (j + 1)) * ρ := by
          rw [← mul_assoc, ← mul_pow]
          congr 2
          ring
      _ ≤ ρ * ρ := mul_le_mul_of_nonneg_right hCl hρR.le
      _ = (ρ : ℝ) ^ 2 := (sq _).symm
  have hT : ((C : ℝ) * ν * l) ^ (j + 1) ≤ ρ := le_of_pow_le_pow_left₀ (by norm_num) hρR.le hmain
  rcases le_total ((C : ℝ) * ν * l) 1 with h | h
  · refine le_trans (pow_le_one₀ hT0 h) ?_
    exact_mod_cast (by omega : 1 ≤ ρ)
  · exact le_trans (pow_le_pow_right₀ h (Nat.le_succ j)) hT

private lemma cq_dirichlet (N : ℕ) (x : ℝ) :
    2 * Real.sin (x / 2) * ∑ k ∈ Finset.Icc 1 N, Real.cos ((k : ℝ) * x) =
      Real.sin (((N : ℝ) + 1 / 2) * x) - Real.sin (x / 2) := by
  induction N with
  | zero =>
    have e : (((0 : ℕ) : ℝ) + 1 / 2) * x = x / 2 := by push_cast; ring
    rw [e]; simp
  | succ N ih =>
    rw [Finset.sum_Icc_succ_top (by omega), mul_add, ih]
    have e1 : (((N + 1 : ℕ) : ℝ) + 1 / 2) * x = ((N + 1 : ℕ) : ℝ) * x + x / 2 := by ring
    have e2 : ((N : ℝ) + 1 / 2) * x = ((N + 1 : ℕ) : ℝ) * x - x / 2 := by push_cast; ring
    rw [e1, e2, Real.sin_add, Real.sin_sub]
    ring

private noncomputable def cq_g (w : ℝ) (q : ℕ) (u : ℝ) : ℝ := w ^ q * (u ^ q)⁻¹

private noncomputable def cq_P (w : ℝ) (q : ℕ) (u : ℝ) : ℝ :=
  -(u ^ (q + 1) / (q * w ^ q)) * (1 - Real.cos (cq_g w q u))

private lemma cq_g_pos (w : ℝ) (hw : 0 < w) (q : ℕ) (u : ℝ) (hu : 0 < u) : 0 < cq_g w q u := by
  unfold cq_g; positivity

private lemma cq_g_anti (w : ℝ) (hw : 0 < w) (q : ℕ) (u v : ℝ) (hu : 0 < u) (huv : u ≤ v) :
    cq_g w q v ≤ cq_g w q u := by
  unfold cq_g
  have h1 : u ^ q ≤ v ^ q := pow_le_pow_left₀ hu.le huv q
  exact mul_le_mul_of_nonneg_left (inv_anti₀ (by positivity) h1) (by positivity)

private lemma cq_deriv (w : ℝ) (hw : 0 < w) (q : ℕ) (hq : 2 ≤ q) (u : ℝ) (hu : 0 < u) :
    HasDerivAt (cq_P w q) (Real.sin (cq_g w q u) -
      ((q : ℝ) + 1) / (q * cq_g w q u) * (1 - Real.cos (cq_g w q u))) u := by
  have hq0 : (q : ℝ) ≠ 0 := by positivity
  have h1 : HasDerivAt (fun u : ℝ => u ^ q) ((q : ℝ) * u ^ (q - 1)) u := hasDerivAt_pow q u
  have h2 : HasDerivAt (cq_g w q) (w ^ q * (-((q : ℝ) * u ^ (q - 1)) / (u ^ q) ^ 2)) u :=
    (h1.inv (by positivity)).const_mul (w ^ q)
  have h3 : HasDerivAt (fun u : ℝ => -(u ^ (q + 1) / (q * w ^ q)))
      (-((((q + 1 : ℕ) : ℝ) * u ^ (q + 1 - 1)) / (q * w ^ q))) u :=
    ((hasDerivAt_pow (q + 1) u).div_const _).neg
  have h4 := h3.mul ((h2.cos).const_sub 1)
  refine h4.congr_deriv ?_
  have e : u ^ q = u ^ (q - 1) * u := by rw [← pow_succ]; congr 1; omega
  unfold cq_g
  rw [Nat.add_sub_cancel, pow_succ u q, e]
  push_cast
  field_simp
  ring

/-- One step: `sin g(m)` dominates the increment of `P + g`, with a gain where `1 ≤ g ≤ 2`. -/
private lemma cq_step (w : ℝ) (hw : 0 < w) (q : ℕ) (hq : 2 ≤ q) (m : ℝ) (hm : 0 < m) (e : ℝ)
    (he : e = 0 ∨ (e = 1 / 5 ∧ cq_g w q m ≤ 2 ∧ 1 ≤ cq_g w q (m + 1))) :
    (cq_P w q (m + 1) + cq_g w q (m + 1)) - (cq_P w q m + cq_g w q m) + e ≤ Real.sin (cq_g w q m) := by
  obtain ⟨c, hc, hc2⟩ := exists_hasDerivAt_eq_slope (cq_P w q) _ (show m < m + 1 by linarith)
    (fun u hu => (cq_deriv w hw q hq u (lt_of_lt_of_le hm hu.1)).continuousAt.continuousWithinAt)
    (fun u hu => cq_deriv w hw q hq u (hm.trans hu.1))
  have hc0 : 0 < c := hm.trans hc.1
  have hgc := cq_g_pos w hw q c hc0
  have h1 := cq_g_anti w hw q m c hm hc.1.le
  have h2 := cq_g_anti w hw q c (m + 1) hc0 hc.2.le
  have e1 : m + 1 - m = 1 := by ring
  rw [e1, div_one] at hc2
  have hlip : Real.sin (cq_g w q c) - Real.sin (cq_g w q m) ≤ cq_g w q m - cq_g w q c := by
    have h := Real.abs_sin_sub_sin_le (cq_g w q c) (cq_g w q m)
    have h3 := le_abs_self (Real.sin (cq_g w q c) - Real.sin (cq_g w q m))
    rw [abs_sub_comm (cq_g w q c), abs_of_nonneg (sub_nonneg.mpr h1)] at h
    linarith
  have hgain : e ≤ ((q : ℝ) + 1) / (q * cq_g w q c) * (1 - Real.cos (cq_g w q c)) := by
    have hcos : 0 ≤ 1 - Real.cos (cq_g w q c) := by linarith [Real.cos_le_one (cq_g w q c)]
    have hq0 : (0 : ℝ) < q := by exact_mod_cast (by omega : 0 < q)
    rcases he with he | ⟨he, h3, h4⟩
    · rw [he]; positivity
    · have h5 : Real.cos (cq_g w q c) ≤ Real.cos 1 :=
        Real.cos_le_cos_of_nonneg_of_le_pi (by norm_num) (by linarith [Real.pi_gt_three]) (by linarith)
      have h6 := Real.cos_one_le
      have h7 : 1 / 2 ≤ ((q : ℝ) + 1) / (q * cq_g w q c) := by
        rw [div_le_div_iff₀ (by norm_num) (by positivity)]
        nlinarith
      rw [he]
      nlinarith
  linarith

private lemma cq_tele (F : ℕ → ℝ) (a b : ℕ) (hab : a ≤ b) :
    ∑ m ∈ Finset.Ico a b, (F (m + 1) - F m) = F b - F a := by
  induction b, hab using Nat.le_induction with
  | base => simp
  | succ b hb ih => rw [Finset.sum_Ico_succ_top hb, ih]; ring

private lemma cq_half (q : ℕ) (hq : 2 ≤ q) (y : ℝ) (hy : 0 ≤ y) (h : y ≤ 1 + 1 / (2 * (q : ℝ))) : y ^ q ≤ 2 := by
  have hq0 : (0 : ℝ) < q := by exact_mod_cast (by omega : 0 < q)
  have h1 : y ≤ Real.exp (1 / (2 * (q : ℝ))) := by
    linarith [Real.add_one_le_exp (1 / (2 * (q : ℝ)))]
  have h2 : y ^ q ≤ Real.exp (1 / (2 * (q : ℝ))) ^ q := pow_le_pow_left₀ hy h1 q
  rw [← Real.exp_nat_mul] at h2
  have e : (q : ℝ) * (1 / (2 * (q : ℝ))) = 1 / 2 := by field_simp
  rw [e] at h2
  have h3 : Real.exp (1 / 2) ≤ 2 := by
    have h4 : Real.exp (1 / 2) ^ (2 : ℕ) ≤ 2 ^ (2 : ℕ) := by
      rw [← Real.exp_nat_mul]
      have e2 : ((2 : ℕ) : ℝ) * (1 / 2) = 1 := by norm_num
      rw [e2]
      linarith [Real.exp_one_lt_d9]
    exact le_of_pow_le_pow_left₀ (by norm_num) (by norm_num) h4
  linarith

/-- The sum of `sin ((w/m)^q)` over `μ ≤ m < b` is almost nonnegative once `μ` is large. -/
private lemma cq_main (q : ℕ) (hq : 2 ≤ q) : ∃ μ : ℕ, 0 < μ ∧ ∀ (w : ℝ), 0 < w → ∀ b : ℕ, μ ≤ b → w ≤ b →
    -((b : ℝ) * cq_g w q b) ≤ ∑ m ∈ Finset.Ico μ b, Real.sin (cq_g w q m) := by
  have hqR : (2 : ℝ) ≤ q := by exact_mod_cast hq
  obtain ⟨c, hc⟩ : ∃ c : ℝ, c = 1 / (2 * (q : ℝ) + 1) := ⟨_, rfl⟩
  have hc0 : 0 < c := by rw [hc]; positivity
  have hc1 : c ≤ 1 / 5 := by
    rw [hc]; exact one_div_le_one_div_of_le (by norm_num) (by linarith)
  have hcq : c * (2 * (q : ℝ) + 1) = 1 := by rw [hc]; field_simp
  obtain ⟨B, hB⟩ : ∃ B : ℝ, B = (10 / c) ^ q := ⟨_, rfl⟩
  have hB0 : 0 < B := by rw [hB]; positivity
  refine ⟨⌈10 / c * (B + 2)⌉₊ + 1, by omega, ?_⟩
  set μ := ⌈10 / c * (B + 2)⌉₊ + 1 with hμ
  intro w hw b hb hwb
  have hμ1 : 10 / c * (B + 2) ≤ (μ : ℝ) := by
    rw [hμ]; push_cast
    linarith [Nat.le_ceil (10 / c * (B + 2))]
  have hμ0 : (0 : ℝ) < μ := by positivity
  have hbR : (0 : ℝ) < b := lt_of_lt_of_le hμ0 (by exact_mod_cast hb)
  have hgb := cq_g_pos w hw q b hbR
  have hfrac : ∀ u : ℝ, 0 < u → cq_g w q u = (w / u) ^ q := by
    intro u hu; unfold cq_g; rw [div_pow]; rfl
  by_cases hsmall : cq_g w q μ ≤ 3
  · have h0 : 0 ≤ ∑ m ∈ Finset.Ico μ b, Real.sin (cq_g w q m) := by
      apply Finset.sum_nonneg
      intro m hm
      have hm1 : (μ : ℝ) ≤ m := by exact_mod_cast (Finset.mem_Ico.mp hm).1
      have h1 := cq_g_anti w hw q μ m hμ0 hm1
      have h2 := cq_g_pos w hw q m (hμ0.trans_le hm1)
      exact Real.sin_nonneg_of_nonneg_of_le_pi h2.le (by linarith [Real.pi_gt_three])
    have : 0 ≤ (b : ℝ) * cq_g w q b := by positivity
    linarith
  rw [not_le] at hsmall
  have hμw : (μ : ℝ) ≤ w * (1 - c) := by
    by_contra hcon
    rw [not_le] at hcon
    have h1 : w / μ ≤ 1 + 1 / (2 * (q : ℝ)) := by
      rw [div_le_iff₀ hμ0]
      have e : (1 + 1 / (2 * (q : ℝ))) * (1 - c) = 1 := by rw [hc]; field_simp; ring
      have h2 : 0 < 1 + 1 / (2 * (q : ℝ)) := by positivity
      have h3 := mul_le_mul_of_nonneg_left hcon.le h2.le
      have h4 : (1 + 1 / (2 * (q : ℝ))) * (w * (1 - c)) = w := by
        calc _ = w * ((1 + 1 / (2 * (q : ℝ))) * (1 - c)) := by ring
          _ = w := by rw [e, mul_one]
      linarith
    have h2 := cq_half q hq (w / μ) (by positivity) h1
    rw [hfrac μ hμ0] at hsmall
    linarith
  have hwμ : (μ : ℝ) ≤ w := by nlinarith
  set P := ⌈w * (1 - c)⌉₊ with hP
  set Q := ⌊w⌋₊ with hQ
  set a := max μ ⌈c / 10 * w⌉₊ with ha
  have hwc : 0 ≤ w * (1 - c) := by nlinarith
  have haP : a ≤ P := by
    apply max_le
    · have := hμw.trans (Nat.le_ceil (w * (1 - c)))
      exact_mod_cast this
    · exact Nat.ceil_mono (by nlinarith)
  have hQw : (Q : ℝ) ≤ w := Nat.floor_le hw.le
  have hQb : Q ≤ b := by exact_mod_cast hQw.trans hwb
  have hPw : (P : ℝ) < w * (1 - c) + 1 := Nat.ceil_lt_add_one hwc
  have hQw2 : w < (Q : ℝ) + 1 := Nat.lt_floor_add_one w
  have hab : a ≤ b := by
    apply max_le hb
    have : (⌈c / 10 * w⌉₊ : ℕ) ≤ b := by
      rw [Nat.ceil_le]; nlinarith
    exact this
  have haμ : μ ≤ a := le_max_left _ _
  have haR : (0 : ℝ) < a := hμ0.trans_le (by exact_mod_cast haμ)
  have hga : cq_g w q a ≤ B := by
    rw [hfrac a haR, hB]
    apply pow_le_pow_left₀ (by positivity)
    have h1 : c / 10 * w ≤ a := (Nat.le_ceil _).trans (by exact_mod_cast le_max_right _ _)
    rw [div_le_div_iff₀ haR hc0]
    linarith
  have hcost : (a : ℝ) - μ ≤ c / 10 * w + 1 := by
    have h1 : (⌈c / 10 * w⌉₊ : ℝ) < c / 10 * w + 1 := Nat.ceil_lt_add_one (by positivity)
    rw [ha, Nat.cast_max]
    rcases le_total (μ : ℝ) (⌈c / 10 * w⌉₊ : ℝ) with h | h
    · rw [max_eq_right h]; linarith
    · rw [max_eq_left h]
      have : 0 < c / 10 * w := by positivity
      linarith
  have hfirst : -((a : ℝ) - μ) ≤ ∑ m ∈ Finset.Ico μ a, Real.sin (cq_g w q m) := by
    have h := Finset.card_nsmul_le_sum (Finset.Ico μ a) (fun m : ℕ => Real.sin (cq_g w q m)) (-1)
      (fun m _ => Real.neg_one_le_sin _)
    rw [Nat.card_Ico, nsmul_eq_mul, Nat.cast_sub haμ] at h
    linarith
  set F : ℕ → ℝ := fun m => cq_P w q m + cq_g w q m with hF
  have hsecond : F b - F a + ((Q : ℝ) - P) / 5 ≤ ∑ m ∈ Finset.Ico a b, Real.sin (cq_g w q m) := by
    have h1 : ∑ m ∈ Finset.Ico a b, ((F (m + 1) - F m) + (if P ≤ m ∧ m < Q then (1 / 5 : ℝ) else 0)) ≤
        ∑ m ∈ Finset.Ico a b, Real.sin (cq_g w q m) := by
      apply Finset.sum_le_sum
      intro m hm
      have hma : (a : ℝ) ≤ m := by exact_mod_cast (Finset.mem_Ico.mp hm).1
      have hm0 : (0 : ℝ) < m := haR.trans_le hma
      have h := cq_step w hw q hq m hm0 (if P ≤ m ∧ m < Q then (1 / 5 : ℝ) else 0) (by
        by_cases hc3 : P ≤ m ∧ m < Q
        · right
          rw [if_pos hc3]
          refine ⟨rfl, ?_, ?_⟩
          · rw [hfrac m hm0]
            apply cq_half q hq _ (by positivity)
            have h2 : w * (1 - c) ≤ m := (Nat.le_ceil _).trans (by exact_mod_cast hc3.1)
            rw [div_le_iff₀ hm0]
            have e : (1 + 1 / (2 * (q : ℝ))) * (1 - c) = 1 := by rw [hc]; field_simp; ring
            have h3 : 0 < 1 + 1 / (2 * (q : ℝ)) := by positivity
            have h4 := mul_le_mul_of_nonneg_left h2 h3.le
            have h5 : (1 + 1 / (2 * (q : ℝ))) * (w * (1 - c)) = w := by
              calc _ = w * ((1 + 1 / (2 * (q : ℝ))) * (1 - c)) := by ring
                _ = w := by rw [e, mul_one]
            linarith
          · rw [hfrac _ (by positivity)]
            apply one_le_pow₀
            rw [one_le_div (by positivity)]
            have : ((m + 1 : ℕ) : ℝ) ≤ Q := by exact_mod_cast hc3.2
            push_cast at this
            linarith
        · left; rw [if_neg hc3])
      simp only [hF]
      push_cast
      linarith
    rw [Finset.sum_add_distrib, cq_tele F a b hab, ← Finset.sum_filter, Finset.sum_const, nsmul_eq_mul] at h1
    have h2 : Finset.Ico P Q ⊆ (Finset.Ico a b).filter (fun m => P ≤ m ∧ m < Q) := by
      intro m hm
      rw [Finset.mem_Ico] at hm
      rw [Finset.mem_filter, Finset.mem_Ico]
      exact ⟨⟨haP.trans hm.1, hm.2.trans_le hQb⟩, hm⟩
    have h3 := Finset.card_le_card h2
    rw [Nat.card_Ico] at h3
    have h4 : (Q : ℝ) - P ≤ (((Finset.Ico a b).filter (fun m => P ≤ m ∧ m < Q)).card : ℝ) := by
      have h5 : Q ≤ ((Finset.Ico a b).filter (fun m => P ≤ m ∧ m < Q)).card + P := by omega
      have h6 : (Q : ℝ) ≤ (((Finset.Ico a b).filter (fun m => P ≤ m ∧ m < Q)).card : ℝ) + P := by exact_mod_cast h5
      linarith
    linarith
  have hFa : F a ≤ B := by
    have h1 : cq_P w q a ≤ 0 := by
      unfold cq_P
      have h2 : 0 ≤ 1 - Real.cos (cq_g w q a) := by linarith [Real.cos_le_one (cq_g w q a)]
      have h3 : 0 ≤ (a : ℝ) ^ (q + 1) / (q * w ^ q) := by positivity
      nlinarith
    simp only [hF]; linarith
  have hFb : -((b : ℝ) * cq_g w q b) ≤ F b := by
    have h1 : 1 - Real.cos (cq_g w q b) ≤ cq_g w q b ^ 2 / 2 := by
      linarith [Real.one_sub_sq_div_two_le_cos (x := cq_g w q b)]
    have h2 : 0 ≤ (b : ℝ) ^ (q + 1) / (q * w ^ q) := by positivity
    have h3 := mul_le_mul_of_nonneg_left h1 h2
    have h4 : (b : ℝ) ^ (q + 1) / (q * w ^ q) * (cq_g w q b ^ 2 / 2) = (b : ℝ) * cq_g w q b / (2 * q) := by
      unfold cq_g
      rw [pow_succ]
      field_simp
    have h5 : (b : ℝ) * cq_g w q b / (2 * q) ≤ (b : ℝ) * cq_g w q b :=
      div_le_self (by positivity) (by linarith)
    simp only [hF]
    unfold cq_P
    linarith
  rw [← Finset.sum_Ico_consecutive _ haμ hab]
  have hfin : B + 2 ≤ c / 10 * w := by
    have h1 : 10 / c * (B + 2) ≤ w := hμ1.trans hwμ
    have h2 := mul_le_mul_of_nonneg_left h1 (show 0 ≤ c / 10 by positivity)
    have e : c / 10 * (10 / c * (B + 2)) = B + 2 := by field_simp
    linarith
  linarith

private def cq_N (s μ q m : ℕ) : ℕ := (s * μ ^ q) / m ^ q

/-- For `0 < x ≤ π`: the sum of Dirichlet kernels is at least `-4b`. -/
private lemma cq_poly (q : ℕ) (hq : 2 ≤ q) (μ : ℕ) (hμ : 0 < μ)
    (hmain : ∀ (w : ℝ), 0 < w → ∀ b : ℕ, μ ≤ b → w ≤ b →
      -((b : ℝ) * cq_g w q b) ≤ ∑ m ∈ Finset.Ico μ b, Real.sin (cq_g w q m))
    (s b : ℕ) (hs : 0 < s) (hb : μ ≤ b) (hsb : 4 ^ q * (s * μ ^ q) ≤ b ^ q) (x : ℝ) (hx : 0 < x) (hxπ : x ≤ Real.pi) :
    -(4 * (b : ℝ)) ≤ ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * x) := by
  have hμR : (0 : ℝ) < μ := by exact_mod_cast hμ
  have hsR : (0 : ℝ) < s := by exact_mod_cast hs
  have hbR : (0 : ℝ) < b := hμR.trans_le (by exact_mod_cast hb)
  have hq0 : (q : ℝ) ≠ 0 := by positivity
  have hsbR : (4 : ℝ) ^ q * (s * (μ : ℝ) ^ q) ≤ (b : ℝ) ^ q := by exact_mod_cast hsb
  have hpi4 : x ≤ 4 := hxπ.trans Real.pi_le_four
  obtain ⟨z, hz0, hzq⟩ : ∃ z : ℝ, 0 < z ∧ z ^ q = s * x := by
    refine ⟨(s * x) ^ (1 / (q : ℝ)), by positivity, ?_⟩
    rw [← Real.rpow_natCast, ← Real.rpow_mul (by positivity), one_div_mul_cancel hq0, Real.rpow_one]
  have hwq : (z * μ) ^ q = s * x * (μ : ℝ) ^ q := by rw [mul_pow, hzq]
  have h4q : (4 : ℝ) ≤ 4 ^ q := by
    calc (4 : ℝ) = 4 ^ 1 := by norm_num
      _ ≤ 4 ^ q := pow_le_pow_right₀ (by norm_num) (by omega)
  have hwb : z * μ ≤ b := by
    apply le_of_pow_le_pow_left₀ (show q ≠ 0 by omega) hbR.le
    rw [hwq]
    calc (s : ℝ) * x * (μ : ℝ) ^ q = x * (s * (μ : ℝ) ^ q) := by ring
      _ ≤ 4 ^ q * (s * (μ : ℝ) ^ q) := mul_le_mul_of_nonneg_right (hpi4.trans h4q) (by positivity)
      _ ≤ (b : ℝ) ^ q := hsbR
  have h1 := hmain (z * μ) (by positivity) b hb hwb
  have hg : ∀ u : ℝ, cq_g (z * μ) q u = x * (s * (μ : ℝ) ^ q * (u ^ q)⁻¹) := by
    intro u; unfold cq_g; rw [hwq]; ring
  have htail : cq_g (z * μ) q b ≤ x := by
    rw [hg]
    have h2 : (s : ℝ) * (μ : ℝ) ^ q * ((b : ℝ) ^ q)⁻¹ ≤ 1 := by
      rw [← div_eq_mul_inv, div_le_one (by positivity)]
      have : 0 ≤ (s : ℝ) * (μ : ℝ) ^ q := by positivity
      nlinarith
    calc x * (s * (μ : ℝ) ^ q * ((b : ℝ) ^ q)⁻¹) ≤ x * 1 := mul_le_mul_of_nonneg_left h2 hx.le
      _ = x := mul_one x
  have hterm : ∀ m ∈ Finset.Ico μ b, Real.sin (cq_g (z * μ) q m) - x / 2 ≤
      Real.sin (((cq_N s μ q m : ℝ) + 1 / 2) * x) := by
    intro m hm
    have hm0 : 0 < m := hμ.trans_le (Finset.mem_Ico.mp hm).1
    have hmq : 0 < m ^ q := Nat.pow_pos hm0
    have hmqR : (0 : ℝ) < (m : ℝ) ^ q := by exact_mod_cast hmq
    set r : ℝ := s * (μ : ℝ) ^ q * ((m : ℝ) ^ q)⁻¹ with hr
    have hr' : r * (m : ℝ) ^ q = s * (μ : ℝ) ^ q := by rw [hr]; field_simp
    have hlo : (cq_N s μ q m : ℝ) ≤ r := by
      have h2 : cq_N s μ q m * m ^ q ≤ s * μ ^ q := Nat.div_mul_le_self _ _
      have h3 : (cq_N s μ q m : ℝ) * (m : ℝ) ^ q ≤ s * (μ : ℝ) ^ q := by exact_mod_cast h2
      rw [← hr'] at h3
      exact le_of_mul_le_mul_right h3 hmqR
    have hhi : r ≤ (cq_N s μ q m : ℝ) + 1 := by
      have h2 : s * μ ^ q < (cq_N s μ q m + 1) * m ^ q := by
        have h := Nat.lt_mul_div_succ (s * μ ^ q) hmq
        calc s * μ ^ q < m ^ q * (s * μ ^ q / m ^ q + 1) := h
          _ = (cq_N s μ q m + 1) * m ^ q := mul_comm _ _
      have h3 : (s : ℝ) * (μ : ℝ) ^ q < ((cq_N s μ q m : ℝ) + 1) * (m : ℝ) ^ q := by exact_mod_cast h2
      rw [← hr'] at h3
      exact (lt_of_mul_lt_mul_right h3 hmqR.le).le
    have h4 := Real.abs_sin_sub_sin_le (cq_g (z * μ) q m) (((cq_N s μ q m : ℝ) + 1 / 2) * x)
    have h5 := le_abs_self (Real.sin (cq_g (z * μ) q m) - Real.sin (((cq_N s μ q m : ℝ) + 1 / 2) * x))
    have h6 : |cq_g (z * μ) q m - ((cq_N s μ q m : ℝ) + 1 / 2) * x| ≤ x / 2 := by
      rw [hg, ← hr, abs_le]
      constructor <;> nlinarith
    linarith
  have hsum := Finset.sum_le_sum hterm
  rw [Finset.sum_sub_distrib, Finset.sum_const, Nat.card_Ico, nsmul_eq_mul, Nat.cast_sub hb] at hsum
  have hD : 2 * Real.sin (x / 2) * ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * x) =
      ∑ m ∈ Finset.Ico μ b, Real.sin (((cq_N s μ q m : ℝ) + 1 / 2) * x) - ((b : ℝ) - μ) * Real.sin (x / 2) := by
    rw [Finset.mul_sum, Finset.sum_congr rfl (fun m _ => cq_dirichlet (cq_N s μ q m) x),
      Finset.sum_sub_distrib, Finset.sum_const, Nat.card_Ico, nsmul_eq_mul, Nat.cast_sub hb]
  have hsin : x / 4 ≤ Real.sin (x / 2) := by
    have h2 := Real.mul_le_sin (show 0 ≤ x / 2 by linarith) (by linarith)
    have h3 : 1 / 4 ≤ 1 / Real.pi := one_div_le_one_div_of_le Real.pi_pos Real.pi_le_four
    have h4 : 2 / Real.pi * (x / 2) = 1 / Real.pi * x := by ring
    rw [h4] at h2
    nlinarith
  have hsin0 : 0 < Real.sin (x / 2) := by linarith
  set T := ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * x) with hT
  have h7 : (b : ℝ) * cq_g (z * μ) q b ≤ b * x := mul_le_mul_of_nonneg_left htail hbR.le
  have h8 : ((b : ℝ) - μ) * (x / 2) ≤ b * (x / 2) := mul_le_mul_of_nonneg_right (by linarith) (by linarith)
  have h9 : ((b : ℝ) - μ) * Real.sin (x / 2) ≤ b * Real.sin (x / 2) := mul_le_mul_of_nonneg_right (by linarith) hsin0.le
  have h10 : (b : ℝ) * (x / 4) ≤ b * Real.sin (x / 2) := mul_le_mul_of_nonneg_left hsin hbR.le
  have h11 : 0 ≤ 2 * Real.sin (x / 2) * (T + 4 * b) := by
    have e : 2 * Real.sin (x / 2) * (T + 4 * b) = 2 * Real.sin (x / 2) * T + 8 * (b * Real.sin (x / 2)) := by ring
    have h12 : 0 ≤ (b : ℝ) * x := by positivity
    rw [e, hD]
    linarith
  have h12 : 0 ≤ T + 4 * b := nonneg_of_mul_nonneg_right h11 (by linarith)
  linarith

private lemma cq_reduce (x : ℝ) : ∃ y : ℝ, 0 ≤ y ∧ y ≤ Real.pi ∧ ∀ k : ℕ, Real.cos ((k : ℝ) * x) = Real.cos ((k : ℝ) * y) := by
  refine ⟨Real.arccos (Real.cos x), Real.arccos_nonneg _, Real.arccos_le_pi _, fun k => ?_⟩
  have h : Real.cos (Real.arccos (Real.cos x)) = Real.cos x :=
    Real.cos_arccos (Real.neg_one_le_cos x) (Real.cos_le_one x)
  have h1 := Polynomial.Chebyshev.T_real_cos x (k : ℤ)
  have h2 := Polynomial.Chebyshev.T_real_cos (Real.arccos (Real.cos x)) (k : ℤ)
  rw [h] at h2
  push_cast at h1 h2
  rw [← h1, ← h2]

private theorem cos_q : ∀ (q : ℕ), (2 : ℕ) ≤ q → ∃ (C : ℕ), ∀ (ν ρ : ℕ), (1 : ℕ) ≤ ρ → ρ ≤ ν ^ q →
    ∃ (a : ℕ → ℕ), (∑ k ∈ Finset.Icc (1 : ℕ) ρ, a k = ρ) ∧
      ∀ (x : ℝ), -((↑C : ℝ) * (↑ν : ℝ)) ≤ ∑ k ∈ Finset.Icc (1 : ℕ) ρ, (↑(a k) : ℝ) * Real.cos ((↑k : ℝ) * x) := by
  intro q hq
  obtain ⟨μ, hμ, hmain⟩ := cq_main q hq
  refine ⟨20 * μ, ?_⟩
  intro ν ρ hρ hρν
  have hν : 0 < ν := by
    rcases Nat.eq_zero_or_pos ν with h | h
    · rw [h, zero_pow (by omega)] at hρν; omega
    · exact h
  obtain ⟨b, hbdef⟩ : ∃ b : ℕ, b = 4 * (μ * ν) := ⟨_, rfl⟩
  have hμν : μ ≤ μ * ν := Nat.le_mul_of_pos_right μ hν
  have hb : μ < b := by omega
  set R : ℕ → ℕ := fun s => ∑ m ∈ Finset.Ico μ b, cq_N s μ q m with hR
  have hRs : ∀ s, s ≤ R s := by
    intro s
    have h1 : cq_N s μ q μ = s := Nat.mul_div_cancel _ (Nat.pow_pos hμ)
    have h2 := Finset.single_le_sum (f := fun m => cq_N s μ q m) (fun i _ => Nat.zero_le _)
      (Finset.mem_Ico.mpr ⟨le_refl μ, hb⟩)
    simp only [hR]
    omega
  have hR0 : R 0 = 0 := by simp [hR, cq_N]
  have hstep : ∀ s, R (s + 1) ≤ R s + b := by
    intro s
    have h1 : ∀ m ∈ Finset.Ico μ b, cq_N (s + 1) μ q m ≤ cq_N s μ q m + 1 := by
      intro m hm
      have hmμ := (Finset.mem_Ico.mp hm).1
      have hmq : 0 < m ^ q := Nat.pow_pos (hμ.trans_le hmμ)
      have h2 : μ ^ q ≤ m ^ q := Nat.pow_le_pow_left hmμ q
      unfold cq_N
      rw [← Nat.add_div_right _ hmq]
      apply Nat.div_le_div_right
      rw [add_mul, one_mul]
      omega
    have h2 := Finset.sum_le_sum h1
    rw [Finset.sum_add_distrib, Finset.sum_const, Nat.card_Ico, smul_eq_mul, mul_one] at h2
    simp only [hR]
    omega
  have hex : ∃ s, ρ < R s := ⟨ρ + 1, (Nat.lt_succ_self ρ).trans_le (hRs (ρ + 1))⟩
  obtain ⟨s, hs1, hs2⟩ : ∃ s, R s ≤ ρ ∧ ρ < R (s + 1) := by
    have h1 := Nat.find_spec hex
    have h0 : Nat.find hex ≠ 0 := by
      intro h; rw [h, hR0] at h1; omega
    obtain ⟨s, hs⟩ := Nat.exists_eq_succ_of_ne_zero h0
    refine ⟨s, ?_, ?_⟩
    · exact not_lt.mp (Nat.find_min hex (by omega))
    · rw [hs] at h1; exact h1
  have hsρ : s ≤ ρ := (hRs s).trans hs1
  have hrem : ρ - R s ≤ b := by have := hstep s; omega
  have hNle : ∀ m ∈ Finset.Ico μ b, cq_N s μ q m ≤ ρ := fun m hm =>
    (Finset.single_le_sum (f := fun m => cq_N s μ q m) (fun i _ => Nat.zero_le _) hm).trans hs1
  have hfil : ∀ m ∈ Finset.Ico μ b, (Finset.Icc 1 ρ).filter (fun k => k ≤ cq_N s μ q m) = Finset.Icc 1 (cq_N s μ q m) := by
    intro m hm
    have := hNle m hm
    ext k
    simp only [Finset.mem_filter, Finset.mem_Icc]
    omega
  refine ⟨fun k => ((Finset.Ico μ b).filter (fun m => k ≤ cq_N s μ q m)).card + (if k = 1 then ρ - R s else 0), ?_, ?_⟩
  · rw [Finset.sum_add_distrib, Finset.sum_ite_eq' (Finset.Icc 1 ρ) 1 (fun _ => ρ - R s),
      if_pos (Finset.mem_Icc.mpr ⟨le_refl 1, hρ⟩)]
    have h1 : ∑ k ∈ Finset.Icc 1 ρ, ((Finset.Ico μ b).filter (fun m => k ≤ cq_N s μ q m)).card = R s := by
      simp only [Finset.card_filter]
      rw [Finset.sum_comm]
      simp only [hR]
      apply Finset.sum_congr rfl
      intro m hm
      rw [← Finset.card_filter, hfil m hm, Nat.card_Icc]
      omega
    rw [h1]; omega
  · intro x
    obtain ⟨y, hy0, hyπ, hxy⟩ := cq_reduce x
    have h1 : ∑ k ∈ Finset.Icc 1 ρ, ((((Finset.Ico μ b).filter (fun m => k ≤ cq_N s μ q m)).card +
        (if k = 1 then ρ - R s else 0) : ℕ) : ℝ) * Real.cos ((k : ℝ) * x) =
        ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * y) +
          ((ρ - R s : ℕ) : ℝ) * Real.cos y := by
      have h2 : ∀ k ∈ Finset.Icc 1 ρ, ((((Finset.Ico μ b).filter (fun m => k ≤ cq_N s μ q m)).card +
          (if k = 1 then ρ - R s else 0) : ℕ) : ℝ) * Real.cos ((k : ℝ) * x) =
          ∑ m ∈ Finset.Ico μ b, (if k ≤ cq_N s μ q m then Real.cos ((k : ℝ) * y) else 0) +
            (if k = 1 then ((ρ - R s : ℕ) : ℝ) * Real.cos y else 0) := by
        intro k _
        rw [hxy k, Nat.cast_add, add_mul, ← Finset.sum_filter, Finset.sum_const, nsmul_eq_mul]
        congr 1
        split_ifs with hk
        · rw [hk, Nat.cast_one, one_mul]
        · simp
      rw [Finset.sum_congr rfl h2, Finset.sum_add_distrib, Finset.sum_comm,
        Finset.sum_ite_eq' (Finset.Icc 1 ρ) 1 (fun _ => ((ρ - R s : ℕ) : ℝ) * Real.cos y),
        if_pos (Finset.mem_Icc.mpr ⟨le_refl 1, hρ⟩)]
      congr 1
      apply Finset.sum_congr rfl
      intro m hm
      rw [← Finset.sum_filter, hfil m hm]
    have h3 : -(b : ℝ) ≤ ((ρ - R s : ℕ) : ℝ) * Real.cos y := by
      have h4 : ((ρ - R s : ℕ) : ℝ) ≤ b := by exact_mod_cast hrem
      have h5 : (0 : ℝ) ≤ ((ρ - R s : ℕ) : ℝ) := Nat.cast_nonneg _
      nlinarith [Real.neg_one_le_cos y]
    have hbR : (0 : ℝ) ≤ b := Nat.cast_nonneg b
    have h6 : -(4 * (b : ℝ)) ≤ ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * y) := by
      have hnonneg : (∀ k : ℕ, Real.cos ((k : ℝ) * y) = 1) ∨ s = 0 →
          0 ≤ ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * y) := by
        rintro (h | h)
        · exact Finset.sum_nonneg fun m _ => Finset.sum_nonneg fun k _ => by rw [h k]; norm_num
        · have : ∀ m, cq_N s μ q m = 0 := by intro m; rw [h]; simp [cq_N]
          simp [this]
      rcases hy0.eq_or_lt with h | h
      · have := hnonneg (Or.inl fun k => by rw [← h]; simp)
        linarith
      · rcases Nat.eq_zero_or_pos s with hs0 | hs0
        · have := hnonneg (Or.inr hs0)
          linarith
        · refine cq_poly q hq μ hμ hmain s b hs0 hb.le ?_ y h hyπ
          calc 4 ^ q * (s * μ ^ q) ≤ 4 ^ q * (ν ^ q * μ ^ q) :=
                Nat.mul_le_mul_left _ (Nat.mul_le_mul_right _ (hsρ.trans hρν))
            _ = b ^ q := by rw [hbdef, mul_pow, mul_pow]; ring
    have h7 : (b : ℝ) * 5 = ((20 * μ : ℕ) : ℝ) * ν := by rw [hbdef]; push_cast; ring
    simp only
    rw [h1]
    linarith

private lemma ra_exp (q : ℕ) (hq : 2 ≤ q) : 1 / 2 ≤ 1 - 1 / (q : ℝ) := by
  have h : (2 : ℝ) ≤ q := by exact_mod_cast hq
  have : 1 / (q : ℝ) ≤ 1 / 2 := one_div_le_one_div_of_le (by norm_num) h
  linarith

/-- `n ↦ n^(-(1-1/q))` as a completely multiplicative function. -/
private noncomputable def ra_f (q : ℕ) (hq : 2 ≤ q) : ℕ →*₀ ℝ where
  toFun n := (n : ℝ) ^ (-(1 - 1 / (q : ℝ)))
  map_zero' := by
    rw [Nat.cast_zero]
    have := ra_exp q hq
    exact Real.zero_rpow (by linarith)
  map_one' := by simp
  map_mul' a b := by
    simp only [Nat.cast_mul]
    exact Real.mul_rpow (Nat.cast_nonneg a) (Nat.cast_nonneg b)

private lemma ra_f_apply (q : ℕ) (hq : 2 ≤ q) (n : ℕ) : ra_f q hq n = (n : ℝ) ^ (-(1 - 1 / (q : ℝ))) := rfl

private lemma ra_f_nonneg (q : ℕ) (hq : 2 ≤ q) (n : ℕ) : 0 ≤ ra_f q hq n := by
  rw [ra_f_apply]; positivity

private lemma ra_f_le (q : ℕ) (hq : 2 ≤ q) (p : ℕ) (hp : 2 ≤ p) : ra_f q hq p ≤ 3 / 4 := by
  rw [ra_f_apply]
  have h0 := ra_exp q hq
  have h1 : (p : ℝ) ^ (-(1 - 1 / (q : ℝ))) ≤ (2 : ℝ) ^ (-(1 - 1 / (q : ℝ))) :=
    Real.rpow_le_rpow_of_nonpos (by norm_num) (by exact_mod_cast hp) (by linarith)
  have h2 : (2 : ℝ) ^ (-(1 - 1 / (q : ℝ))) ≤ (2 : ℝ) ^ (-(1 / 2 : ℝ)) :=
    Real.rpow_le_rpow_of_exponent_le (by norm_num) (by linarith)
  have h3 : (2 : ℝ) ^ (-(1 / 2 : ℝ)) ≤ 3 / 4 := by
    have h4 : ((2 : ℝ) ^ (-(1 / 2 : ℝ))) ^ (2 : ℕ) ≤ (3 / 4 : ℝ) ^ (2 : ℕ) := by
      rw [← Real.rpow_natCast ((2 : ℝ) ^ (-(1 / 2 : ℝ))) 2, ← Real.rpow_mul (by norm_num)]
      have e : (-(1 / 2 : ℝ)) * ((2 : ℕ) : ℝ) = ((-1 : ℤ) : ℝ) := by norm_num
      rw [e, Real.rpow_intCast]
      norm_num
    exact le_of_pow_le_pow_left₀ (by norm_num) (by norm_num) h4
  exact h1.trans (h2.trans h3)

private lemma ra_euler (q : ℕ) (hq : 2 ≤ q) (m : ℕ) (hm : 0 < m) (D : Finset ℕ) (hD : ∀ d ∈ D, d ∣ m) :
    ∑ d ∈ D, ra_f q hq d ≤ ∏ p ∈ m.primeFactors, (1 - ra_f q hq p)⁻¹ := by
  have hs : ∀ {p : ℕ}, p.Prime → ‖ra_f q hq p‖ < 1 := by
    intro p hp
    rw [Real.norm_of_nonneg (ra_f_nonneg q hq p)]
    linarith [ra_f_le q hq p hp.two_le]
  have h := (EulerProduct.summable_and_hasSum_factoredNumbers_prod_filter_prime_geometric
    (f := (ra_f q hq).toMonoidHom) hs m.primeFactors).2
  have hfil : m.primeFactors.filter Nat.Prime = m.primeFactors :=
    Finset.filter_true_of_mem (fun p hp => Nat.prime_of_mem_primeFactors hp)
  rw [hfil] at h
  have hmem : ∀ d ∈ D, d ∈ Nat.factoredNumbers m.primeFactors := by
    intro d hd
    rw [Nat.mem_factoredNumbers_iff_primeFactors_subset]
    exact ⟨ne_zero_of_dvd_ne_zero hm.ne' (hD d hd), Nat.primeFactors_mono (hD d hd) hm.ne'⟩
  have h2 := sum_le_hasSum (D.subtype (fun d => d ∈ Nat.factoredNumbers m.primeFactors))
    (fun i _ => ra_f_nonneg q hq i) h
  rw [Finset.sum_subtype_of_mem (fun d => ra_f q hq d) hmem] at h2
  exact h2

private lemma ra_inv_le (y : ℝ) (h0 : 0 ≤ y) (h1 : y ≤ 3 / 4) : (1 - y)⁻¹ ≤ Real.exp (4 * y) := by
  have hpos : 0 < 1 - y := by linarith
  have h2 : (1 - y)⁻¹ ≤ 1 + 4 * y := by
    rw [inv_le_iff_one_le_mul₀ hpos]
    nlinarith
  have := Real.add_one_le_exp (4 * y)
  linarith

private lemma ra_prod (q : ℕ) (hq : 2 ≤ q) (m : ℕ) :
    ∏ p ∈ m.primeFactors, (1 - ra_f q hq p)⁻¹ ≤ Real.exp (4 * ∑ p ∈ m.primeFactors, ra_f q hq p) := by
  rw [Finset.mul_sum, Real.exp_sum]
  apply Finset.prod_le_prod
  · intro p hp
    have := ra_f_le q hq p (Nat.prime_of_mem_primeFactors hp).two_le
    exact inv_nonneg.mpr (by linarith)
  · intro p hp
    exact ra_inv_le _ (ra_f_nonneg q hq p) (ra_f_le q hq p (Nat.prime_of_mem_primeFactors hp).two_le)

private lemma ra_powdiff (a b : ℝ) (hb : 0 ≤ b) (hab : b ≤ a) (n : ℕ) :
    a ^ (n + 1) - b ^ (n + 1) ≤ ((n : ℝ) + 1) * a ^ n * (a - b) := by
  have ha : 0 ≤ a := hb.trans hab
  induction n with
  | zero => simp
  | succ n ih =>
    have h1 : b ^ (n + 1) ≤ a ^ (n + 1) := pow_le_pow_left₀ hb hab _
    have h2 := mul_le_mul_of_nonneg_left ih ha
    have h3 := mul_le_mul_of_nonneg_right h1 (sub_nonneg.mpr hab)
    have e1 : a ^ (n + 1 + 1) - b ^ (n + 1 + 1) = a * (a ^ (n + 1) - b ^ (n + 1)) + b ^ (n + 1) * (a - b) := by ring
    have e2 : (((n + 1 : ℕ) : ℝ) + 1) * a ^ (n + 1) * (a - b) =
        a * (((n : ℝ) + 1) * a ^ n * (a - b)) + a ^ (n + 1) * (a - b) := by push_cast; ring
    rw [e1, e2]
    linarith

/-- For `t > 0`: `(t^q)^(-(1-1/q)) = (t^(q-1))⁻¹`, written with `q = r + 1`. -/
private lemma ra_root (r : ℕ) (t : ℝ) (ht : 0 < t) :
    (t ^ (r + 1)) ^ (-(1 - 1 / ((r + 1 : ℕ) : ℝ))) = (t ^ r)⁻¹ := by
  have h : ((r : ℝ) + 1) ≠ 0 := by positivity
  rw [← Real.rpow_natCast t (r + 1), ← Real.rpow_mul ht.le, ← Real.rpow_natCast t r, ← Real.rpow_neg ht.le]
  congr 1
  push_cast
  field_simp
  ring

/-- `∑_{j=1}^N j^(-(1-1/q)) ≤ q N^(1/q)`. -/
private lemma ra_zeta (r : ℕ) (hq : 2 ≤ r + 1) (N : ℕ) :
    ∑ j ∈ Finset.Icc 1 N, ra_f (r + 1) hq j ≤ ((r : ℝ) + 1) * (N : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) := by
  induction N with
  | zero =>
    have : 0 ≤ ((r : ℝ) + 1) * ((0 : ℕ) : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) := by positivity
    simpa using this
  | succ N ih =>
    rw [Finset.sum_Icc_succ_top (by omega)]
    set a : ℝ := ((N + 1 : ℕ) : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) with ha
    set b : ℝ := (N : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) with hb
    have hb0 : 0 ≤ b := by positivity
    have ha0 : 0 < a := by positivity
    have haq : a ^ (r + 1) = ((N + 1 : ℕ) : ℝ) := by
      rw [ha, ← Real.rpow_natCast, ← Real.rpow_mul (by positivity), one_div_mul_cancel (by positivity), Real.rpow_one]
    have hbq : b ^ (r + 1) = (N : ℝ) := by
      rw [hb, ← Real.rpow_natCast, ← Real.rpow_mul (by positivity), one_div_mul_cancel (by positivity), Real.rpow_one]
    have hab : b ≤ a := Real.rpow_le_rpow (by positivity) (by push_cast; linarith) (by positivity)
    have hf : ra_f (r + 1) hq (N + 1) = (a ^ r)⁻¹ := by
      rw [ra_f_apply, ← haq]
      exact ra_root r a ha0
    have key : (a ^ r)⁻¹ ≤ ((r : ℝ) + 1) * (a - b) := by
      rw [inv_le_iff_one_le_mul₀ (by positivity)]
      have h := ra_powdiff a b hb0 hab r
      rw [haq, hbq] at h
      push_cast at h
      linarith
    rw [hf]
    linarith

/-- A set of at most `t^q` positive integers has `∑ s^(-(1-1/q)) ≤ (q+1) t`. -/
private lemma ra_sum (r : ℕ) (hq : 2 ≤ r + 1) (S : Finset ℕ) (t : ℝ) (ht : 0 < t) (hS : ∀ s ∈ S, 1 ≤ s)
    (hc : (S.card : ℝ) ≤ t ^ (r + 1)) : ∑ s ∈ S, ra_f (r + 1) hq s ≤ ((r : ℝ) + 2) * t := by
  set N : ℕ := ⌊t ^ (r + 1)⌋₊ with hN
  have hNle : (N : ℝ) ≤ t ^ (r + 1) := Nat.floor_le (by positivity)
  have hroot : (t ^ (r + 1)) ^ (1 / ((r + 1 : ℕ) : ℝ)) = t := by
    rw [← Real.rpow_natCast, ← Real.rpow_mul ht.le, mul_one_div_cancel (by positivity), Real.rpow_one]
  have hσ := ra_exp (r + 1) hq
  rw [← Finset.sum_filter_add_sum_filter_not S (fun s => s ≤ N)]
  have h1 : ∑ s ∈ S.filter (fun s => s ≤ N), ra_f (r + 1) hq s ≤ ((r : ℝ) + 1) * t := by
    calc ∑ s ∈ S.filter (fun s => s ≤ N), ra_f (r + 1) hq s ≤ ∑ j ∈ Finset.Icc 1 N, ra_f (r + 1) hq j := by
          apply Finset.sum_le_sum_of_subset_of_nonneg
          · intro s hs
            rw [Finset.mem_filter] at hs
            exact Finset.mem_Icc.mpr ⟨hS s hs.1, hs.2⟩
          · intro i _ _; exact ra_f_nonneg _ _ i
      _ ≤ ((r : ℝ) + 1) * (N : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) := ra_zeta r hq N
      _ ≤ ((r : ℝ) + 1) * (t ^ (r + 1)) ^ (1 / ((r + 1 : ℕ) : ℝ)) :=
          mul_le_mul_of_nonneg_left (Real.rpow_le_rpow (by positivity) hNle (by positivity)) (by positivity)
      _ = ((r : ℝ) + 1) * t := by rw [hroot]
  have h2 : ∑ s ∈ S.filter (fun s => ¬ s ≤ N), ra_f (r + 1) hq s ≤ t := by
    calc ∑ s ∈ S.filter (fun s => ¬ s ≤ N), ra_f (r + 1) hq s
        ≤ ∑ s ∈ S.filter (fun s => ¬ s ≤ N), (t ^ r)⁻¹ := by
          apply Finset.sum_le_sum
          intro s hs
          rw [Finset.mem_filter, not_le] at hs
          have hlt : t ^ (r + 1) < s := (Nat.floor_lt (by positivity)).mp hs.2
          rw [ra_f_apply, ← ra_root r t ht]
          exact Real.rpow_le_rpow_of_nonpos (by positivity) hlt.le (by linarith)
      _ = ((S.filter (fun s => ¬ s ≤ N)).card : ℝ) * (t ^ r)⁻¹ := by
          rw [Finset.sum_const, nsmul_eq_mul]
      _ ≤ t ^ (r + 1) * (t ^ r)⁻¹ := by
          apply mul_le_mul_of_nonneg_right _ (by positivity)
          refine le_trans ?_ hc
          exact_mod_cast Finset.card_filter_le _ _
      _ = t := by rw [pow_succ]; field_simp
  linarith

/-- `ω(m) ≤ 2 log m`. -/
private lemma ra_omega (m : ℕ) (hm : 0 < m) : (m.primeFactors.card : ℝ) ≤ 2 * Real.log m := by
  have h1 : 2 ^ m.primeFactors.card ≤ m := by
    calc 2 ^ m.primeFactors.card = ∏ _p ∈ m.primeFactors, 2 := by rw [Finset.prod_const]
      _ ≤ ∏ p ∈ m.primeFactors, p :=
          Finset.prod_le_prod' (fun p hp => (Nat.prime_of_mem_primeFactors hp).two_le)
      _ ≤ m := Nat.le_of_dvd hm (Nat.prod_primeFactors_dvd m)
  have h2 : Real.log ((2 : ℝ) ^ m.primeFactors.card) ≤ Real.log m :=
    Real.log_le_log (by positivity) (by exact_mod_cast h1)
  rw [Real.log_pow] at h2
  have h3 := Real.log_two_gt_d9
  have h4 : (0 : ℝ) ≤ m.primeFactors.card := Nat.cast_nonneg _
  nlinarith

private lemma ra_count (r : ℕ) (hq : 2 ≤ r + 1) (m k : ℕ) (hm : 0 < m) (hk : 0 < k) (t : ℝ) (ht : 0 < t)
    (hw : (m.primeFactors.card : ℝ) ≤ t ^ (r + 1)) :
    (Finset.card (Finset.filter (fun (d : ℕ) => d ∣ m) (Finset.Ioc k ((2 : ℕ) * k))) : ℝ)
      ≤ (2 * (k : ℝ)) ^ (1 - 1 / ((r + 1 : ℕ) : ℝ)) * Real.exp (4 * (((r : ℝ) + 2) * t)) := by
  set D := Finset.filter (fun (d : ℕ) => d ∣ m) (Finset.Ioc k ((2 : ℕ) * k)) with hDdef
  have hkR : (0 : ℝ) < k := by exact_mod_cast hk
  have hσ := ra_exp (r + 1) hq
  have h1 : (D.card : ℝ) * ((2 * (k : ℝ)) ^ (-(1 - 1 / ((r + 1 : ℕ) : ℝ)))) ≤ ∑ d ∈ D, ra_f (r + 1) hq d := by
    rw [← nsmul_eq_mul, ← Finset.sum_const]
    apply Finset.sum_le_sum
    intro d hd
    rw [hDdef, Finset.mem_filter, Finset.mem_Ioc] at hd
    rw [ra_f_apply]
    apply Real.rpow_le_rpow_of_nonpos _ _ (by linarith)
    · have : 0 < d := by omega
      exact_mod_cast this
    · exact_mod_cast hd.1.2
  have h2 := ra_euler (r + 1) hq m hm D (fun d hd => (Finset.mem_filter.mp hd).2)
  have h3 := ra_prod (r + 1) hq m
  have h4 := ra_sum r hq m.primeFactors t ht (fun p hp => (Nat.prime_of_mem_primeFactors hp).one_le) hw
  have h5 : Real.exp (4 * ∑ p ∈ m.primeFactors, ra_f (r + 1) hq p) ≤ Real.exp (4 * (((r : ℝ) + 2) * t)) :=
    Real.exp_le_exp.mpr (by linarith)
  have h6 := h1.trans (h2.trans (h3.trans h5))
  have hX : 0 < (2 * (k : ℝ)) ^ (1 - 1 / ((r + 1 : ℕ) : ℝ)) := by positivity
  rw [Real.rpow_neg (by positivity), ← div_eq_mul_inv, div_le_iff₀ hX] at h6
  linarith

private theorem h_divA : ∀ (A : ℕ), (1 : ℕ) ≤ A → ∃ (K₀ : ℕ), ∀ (m k : ℕ), K₀ ≤ k → (0 : ℕ) < m →
    Real.log (↑m : ℝ) ≤ Real.log (↑k : ℝ) ^ A →
    (Finset.card (Finset.filter (fun (d : ℕ) => d ∣ m) (Finset.Ioc k ((2 : ℕ) * k)))) ^ (A + (2 : ℕ)) < k ^ (A + (1 : ℕ)) := by
  intro A hA
  have hq : 2 ≤ A + 1 := by omega
  have hA1 : (1 : ℝ) ≤ A := by exact_mod_cast hA
  obtain ⟨C, hC⟩ : ∃ C : ℝ, C = 8 * ((A : ℝ) + 2) ^ (3 : ℕ) := ⟨_, rfl⟩
  have hC0 : 0 < C := by rw [hC]; positivity
  refine ⟨⌈Real.exp (2 * C ^ (A + 1) + 2 * ((A : ℝ) + 1) ^ (2 : ℕ) + 1)⌉₊, ?_⟩
  intro m k hk hm hlog
  have hk1 := Nat.le_of_ceil_le hk
  have hkR : (0 : ℝ) < k := lt_of_lt_of_le (Real.exp_pos _) hk1
  have hk0 : 0 < k := by exact_mod_cast hkR
  have hL := (Real.le_log_iff_exp_le hkR).mpr hk1
  set L := Real.log (k : ℝ) with hLdef
  have hCp : 0 < C ^ (A + 1) := by positivity
  have hAp : 0 < ((A : ℝ) + 1) ^ (2 : ℕ) := by positivity
  have hLpos : 0 < L := by linarith
  have hw : (m.primeFactors.card : ℝ) ≤ (L / C) ^ (A + 1) := by
    have h1 := ra_omega m hm
    have h4 : 0 < L ^ A := by positivity
    rw [div_pow, le_div_iff₀ hCp, pow_succ L A]
    have h5 : (m.primeFactors.card : ℝ) * C ^ (A + 1) ≤ (2 * L ^ A) * C ^ (A + 1) :=
      mul_le_mul_of_nonneg_right (by linarith) hCp.le
    have h6 : L ^ A * (2 * C ^ (A + 1)) ≤ L ^ A * L := mul_le_mul_of_nonneg_left (by linarith) h4.le
    linarith
  have hcount := ra_count A hq m k hm hk0 (L / C) (by positivity) hw
  set D := Finset.card (Finset.filter (fun (d : ℕ) => d ∣ m) (Finset.Ioc k ((2 : ℕ) * k))) with hDdef
  set σ : ℝ := 1 - 1 / ((A + 1 : ℕ) : ℝ) with hσdef
  have hσ : 1 / 2 ≤ σ := ra_exp (A + 1) hq
  have hkexp : (k : ℝ) = Real.exp L := (Real.exp_log hkR).symm
  have h2k : (2 * (k : ℝ)) ^ σ ≤ Real.exp (σ * (1 + L)) := by
    have h2 : 2 * (k : ℝ) ≤ Real.exp (1 + L) := by
      rw [Real.exp_add, ← hkexp]
      have := Real.add_one_le_exp (1 : ℝ)
      nlinarith
    calc (2 * (k : ℝ)) ^ σ ≤ (Real.exp (1 + L)) ^ σ :=
          Real.rpow_le_rpow (by positivity) h2 (by linarith)
      _ = Real.exp (σ * (1 + L)) := by rw [← Real.exp_mul, mul_comm]
  have hD : (D : ℝ) ≤ Real.exp (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C))) := by
    rw [Real.exp_add]
    exact hcount.trans (mul_le_mul_of_nonneg_right h2k (Real.exp_pos _).le)
  have hlt : ((A : ℝ) + 2) * (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C))) < ((A : ℝ) + 1) * L := by
    have e1 : 4 * (((A : ℝ) + 2) * (L / C)) = L / (2 * ((A : ℝ) + 2) ^ (2 : ℕ)) := by
      rw [hC]; field_simp; ring
    have e2 : ((A : ℝ) + 2) * σ = ((A : ℝ) + 1) - 1 / ((A : ℝ) + 1) := by
      rw [hσdef]; push_cast; field_simp; ring
    have e3 : ((A : ℝ) + 2) * (L / (2 * ((A : ℝ) + 2) ^ (2 : ℕ))) = L / (2 * ((A : ℝ) + 2)) := by
      field_simp
    have e4 : L / (2 * ((A : ℝ) + 2)) ≤ L / (2 * ((A : ℝ) + 1)) :=
      div_le_div_of_nonneg_left hLpos.le (by positivity) (by linarith)
    have e5 : L / (2 * ((A : ℝ) + 1)) = 1 / ((A : ℝ) + 1) * L / 2 := by field_simp
    have e6 : (A : ℝ) + 1 < 1 / ((A : ℝ) + 1) * L / 2 := by
      rw [← e5, lt_div_iff₀ (by positivity)]
      nlinarith
    have e7 : 0 < 1 / ((A : ℝ) + 1) := by positivity
    calc ((A : ℝ) + 2) * (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C)))
        = ((A : ℝ) + 2) * σ * (1 + L) + ((A : ℝ) + 2) * (L / (2 * ((A : ℝ) + 2) ^ (2 : ℕ))) := by rw [e1]; ring
      _ = ((A : ℝ) + 1 - 1 / ((A : ℝ) + 1)) * (1 + L) + L / (2 * ((A : ℝ) + 2)) := by rw [e2, e3]
      _ < ((A : ℝ) + 1) * L := by nlinarith
  have hfin : ((D ^ (A + 2) : ℕ) : ℝ) < ((k ^ (A + 1) : ℕ) : ℝ) := by
    push_cast
    calc (D : ℝ) ^ (A + 2) ≤ Real.exp (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C))) ^ (A + 2) :=
          pow_le_pow_left₀ (Nat.cast_nonneg _) hD _
      _ = Real.exp (((A : ℝ) + 2) * (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C)))) := by
          rw [← Real.exp_nat_mul]; push_cast; rfl
      _ < Real.exp (((A : ℝ) + 1) * L) := Real.exp_lt_exp.mpr hlt
      _ = (k : ℝ) ^ (A + 1) := by
          rw [hkexp, ← Real.exp_nat_mul]; push_cast; rfl
  exact_mod_cast hfin

private theorem t5_dense_lit : ∀ (A : ℕ), ∃ (u₀ K₀ : ℕ), ∀ (n k : ℕ), K₀ ≤ k → k ^ u₀ ≤ n →
      Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ A →
      k ^ (A + (1 : ℕ)) ≤ (Finset.card (Finset.filter (fun (d : ℕ) => n % d < k) (Finset.Ioc k ((2 : ℕ) * k)))) ^ (A + (2 : ℕ)) :=
  t5_dense (t5_cosq cos_q)

theorem erdos_1094__h2__h2__h5__h2__h2__h1__h1__h2 : ∀ (c : ℝ),
  (0 : ℝ) < c →
    ∀ (c₁ : ℝ),
      (0 : ℝ) < c₁ →
        ∃ (K : ℕ),
          ∀ (n k : ℕ),
            (0 : ℕ) < k →
              k * k * k ≤ n →
                Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) →
                  (n.choose k).minFac > n / k →
                    (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) →
                      ∏ m ∈ Finset.Ioc (n - k) n,
                            ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p =
                          k.factorial →
                        (∀ (m : ℕ),
                            n - k < m →
                              m ≤ n →
                                ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨
                                  ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) →
                          n / k ∣ (k - (1 : ℕ)).choose (n % k) →
                            n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k →
                              (∃ (x : ℕ),
                                  n - k < x ∧
                                    x ≤ n ∧
                                      x ≠ k * (n / k) ∧
                                        ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                          x) →
                                (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k →
                                  (∑ x ∈ Finset.Ioc (n - k) n with
                                          ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                            x,
                                          (1 : ℕ)) ^
                                        (10 : ℕ) <
                                      k →
                                    c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) <
                                        Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) →
                                      k ≤ K := by
  -- approach: Run certificates built from nonnegative cosine polynomials with a small constant term show that k^0.99 moduli of (k,2k] have a multiple in the window whenever log n ≤ log⁷k. Each divides a smooth element, which has under k^(8/9) divisors there, so there are at least k^0.1 smooth elements.
  -- licence: CC-BY-4.0
  -- label: under k^0.1 smooth elements, log n above log⁷k
  -- claim: $\forall c>0\ \forall c_1>0\ \exists K\ \forall n,k:\ 0<k,\ k^3\le n,\ \exp(c\log^2 k)<n,\ \operatorname{lpf}\binom nk>\lfloor n/k\rfloor,\ \bigl(\forall p \text{ prime},\ k<p\le\lfloor n/k\rfloor,\ \forall m\in(n-k,n],\ p\nmid m\bigr),\ \prod_{m=n-k+1}^{n} s_k(m) = k!,\ \bigl(\forall m\in(n-k,n]:\ s_k(m)=m \text{ or } s_k(m)<k\bigr),\ \lfloor n/k\rfloor\mid\binom{k-1}{n \bmod k},\ n<k\,2^{k-1}+k,\ \bigl(\exists x\in(n-k,n]:\ x\ne k\lfloor n/k\rfloor,\ s_k(x)=x\bigr),\ (n-k)^2<k\prod_{p\le k \text{ prime}}p^{\lfloor\log_p k\rfloor},\ N^{10}<k,\ c_1\log^4 k<\log n\,(\log\log k)^6,\ \log^7 k<\log n \Rightarrow k\le K,\quad N=\#\{x\in(n-k,n]:\ s_k(x)=x\}$
  -- Here $s_k(m)=\prod_{p\le k \text{ prime}}p^{v_p(m)}$ is the $k$-smooth part and $N$ the number of $k$-smooth elements of the window. The parent statement with one more hypothesis, the last: $\log n$ exceeds $\log^7 k$. In the Lean, `n / k` is the floor, `n % k` the remainder, `n - k` truncated subtraction (here $k\le n$), `Nat.log p k` is $\lfloor\log_p k\rfloor$, `minFac` the least prime factor, `Finset.Ioc (n - k) n` the window, and $N$ is written as a sum of ones.
  have h_few7 :
      ∀ (c : ℝ),
        (0 : ℝ) < c →
          ∀ (c₁ : ℝ),
            (0 : ℝ) < c₁ →
              ∃ (K : ℕ),
                ∀ (n k : ℕ),
                  (0 : ℕ) < k →
                    k * k * k ≤ n →
                      Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) →
                        (n.choose k).minFac > n / k →
                          (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) →
                            ∏ m ∈ Finset.Ioc (n - k) n,
                                  ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p =
                                k.factorial →
                              (∀ (m : ℕ),
                                  n - k < m →
                                    m ≤ n →
                                      ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨
                                        ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) →
                                n / k ∣ (k - (1 : ℕ)).choose (n % k) →
                                  n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k →
                                    (∃ (x : ℕ),
                                        n - k < x ∧
                                          x ≤ n ∧
                                            x ≠ k * (n / k) ∧
                                              ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                                x) →
                                      (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k →
                                        (∑ x ∈ Finset.Ioc (n - k) n with
                                                ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                                  x,
                                                (1 : ℕ)) ^
                                              (10 : ℕ) <
                                            k →
                                          c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) <
                                              Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) →
        Real.log (↑k : ℝ) ^ (7 : ℕ) < Real.log (↑n : ℝ) →
        k ≤ K := by
    sorry
  intro c hc c₁ hc₁
  obtain ⟨u₀, K₀, hdense⟩ := t5_dense_lit 98
  obtain ⟨K₀', hdiv⟩ := h_divA 7 (by norm_num)
  obtain ⟨KF, hKF⟩ := h_few7 c hc c₁ hc₁
  refine ⟨KF + K₀ + K₀' + ⌈Real.exp ((u₀ : ℝ) / c)⌉₊ + 2 + ⌈Real.exp 1⌉₊, ?_⟩
  intro n k hk h3 hexp hmin hfree hsmooth hsplit hD1 hbd hex hA hN10 hrange
  by_contra hcon
  have hgt := not_le.mp hcon
  have hk2 : 2 ≤ k := by omega
  have hsq : k * k ≤ n :=
    calc k * k = k * k * 1 := (Nat.mul_one _).symm
      _ ≤ k * k * k := Nat.mul_le_mul_left _ hk
      _ ≤ n := h3
  have hkn : k ≤ n := le_trans (Nat.le_mul_self k) hsq
  have hl1 : 1 ≤ Real.log (k : ℝ) := by
    have h1 : Real.exp 1 ≤ (k : ℝ) :=
      le_trans (Nat.le_ceil _) (by exact_mod_cast (show ⌈Real.exp 1⌉₊ ≤ k by omega))
    exact (Real.le_log_iff_exp_le (by exact_mod_cast hk)).mpr h1
  by_cases hnear : Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ (7 : ℕ)
  · have hbig : k ≤ (Finset.filter (fun (x : ℕ) => ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p,
        p ^ (x.factorization : ℕ → ℕ) p = x) (Finset.Ioc (n - k) n)).card ^ (10 : ℕ) := by
      have hu : k ^ u₀ ≤ n := t1_kpow c hc u₀ n k hk (by omega) hexp
      have hN := hdense n k (by omega) hu (le_trans hnear (pow_le_pow_right₀ hl1 (by norm_num)))
      refine many_count7 _ _ k ?_ ?_ hN hk
      · intro d hd
        rw [Finset.mem_filter, Finset.mem_Ioc] at hd
        have hdk : d * k ≤ n :=
          calc d * k ≤ (2 * k) * k := Nat.mul_le_mul_right k hd.1.2
            _ ≤ (k * k) * k := Nat.mul_le_mul_right k (Nat.mul_le_mul_right k hk2)
            _ ≤ n := h3
        have hlt := many_landing n k hk hkn hfree d hd.1.1 ((Nat.le_div_iff_mul_le hk).mpr hdk) hd.2
        have hmle : n % d ≤ n := Nat.mod_le n d
        have hx1 : n - k < n - n % d := by omega
        have hx2 : n - n % d ≤ n := Nat.sub_le _ _
        refine ⟨n - n % d, Finset.mem_filter.mpr ⟨Finset.mem_Ioc.mpr ⟨hx1, hx2⟩, ?_⟩, Nat.dvd_sub_mod n⟩
        rcases hsplit (n - n % d) hx1 hx2 with h | h
        · exact h
        · omega
      · intro x hx
        have hxw := Finset.mem_Ioc.mp (Finset.mem_filter.mp hx).1
        have hx0 : 0 < x := by omega
        have hlog : Real.log ((x : ℕ) : ℝ) ≤ Real.log (n : ℝ) :=
          Real.log_le_log (by exact_mod_cast hx0) (by exact_mod_cast hxw.2)
        refine lt_of_le_of_lt (Nat.pow_le_pow_left (Finset.card_le_card ?_) 9)
          (hdiv x k (by omega) hx0 (le_trans hlog hnear))
        exact Finset.filter_subset_filter _ (Finset.filter_subset _ _)
    rw [Finset.card_eq_sum_ones] at hbig
    exact absurd hN10 (not_lt.mpr hbig)
  · have := hKF n k hk h3 hexp hmin hfree hsmooth hsplit hD1 hbd hex hA hN10 hrange (not_le.mp hnear)
    omega
Closing it through its holes

Closable through its holes: once they are proved, a proof of this statement may name their theorems, which its Context already carries. A direct proof is accepted at any time.

What is known

Work on it

Work on this statement →

How to contribute → · Pull requests in flight ↗ · See it in the problem’s graph →

Record

Statement: the Lean file and its words (1 version)

Statement not audited — only a problem’s root statement is audited for fidelity (D-9); the kernel checks a proof against this Lean as written.

import Mathlib
import Nodes.«erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2».Context

open scoped Nat

/-! Hole `h_few` of a merged partial proof, as a node (D-12 #5, D-29).
Statement generated by the gate from the assembly; immutable (D-3). -/

theorem erdos_1094__h2__h2__h5__h2__h2__h1__h1__h2 : ∀ (c : ℝ),
  (0 : ℝ) < c →
    ∀ (c₁ : ℝ),
      (0 : ℝ) < c₁ →
        ∃ (K : ℕ),
          ∀ (n k : ℕ),
            (0 : ℕ) < k →
              k * k * k ≤ n →
                Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) →
                  (n.choose k).minFac > n / k →
                    (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) →
                      ∏ m ∈ Finset.Ioc (n - k) n,
                            ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p =
                          k.factorial →
                        (∀ (m : ℕ),
                            n - k < m →
                              m ≤ n →
                                ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨
                                  ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) →
                          n / k ∣ (k - (1 : ℕ)).choose (n % k) →
                            n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k →
                              (∃ (x : ℕ),
                                  n - k < x ∧
                                    x ≤ n ∧
                                      x ≠ k * (n / k) ∧
                                        ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                          x) →
                                (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k →
                                  (∑ x ∈ Finset.Ioc (n - k) n with
                                          ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                            x,
                                          (1 : ℕ)) ^
                                        (10 : ℕ) <
                                      k →
                                    c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) <
                                        Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) →
                                      k ≤ K := by
  sorry

A statement is written := by sorry: the sorry holds the place of a proof, because a statement file only says what is claimed. A proof the gate checked is its own file, Proof.lean. How to read a proof page →

In words, unverified — what this statement says; written by t1203r2-k, drafted with claude-fable-5-1, claude-opus-5-5, 2026-12-03; no one has read it against the Lean and signed it.

Draft · drafted, by the model named above

$\displaystyle \forall c>0\ \forall c_1>0\ \exists K\ \forall n,k:\ 0<k,\ k^3\le n,\ \exp(c\log^2 k)<n,\ \operatorname{lpf}\binom nk>\lfloor n/k\rfloor,\ \bigl(\forall p \text{ prime},\ k<p\le\lfloor n/k\rfloor,\ \forall m\in(n-k,n],\ p\nmid m\bigr),\ \prod_{m=n-k+1}^{n} s_k(m) = k!,\ \bigl(\forall m\in(n-k,n]:\ s_k(m)=m \text{ or } s_k(m)<k\bigr),\ \lfloor n/k\rfloor\mid\binom{k-1}{n \bmod k},\ n<k\,2^{k-1}+k,\ \bigl(\exists x\in(n-k,n]:\ x\ne k\lfloor n/k\rfloor,\ s_k(x)=x\bigr),\ (n-k)^2<k\prod_{p\le k \text{ prime}}p^{\lfloor\log_p k\rfloor},\ N^{10}<k,\ c_1\log^4 k<\log n\,(\log\log k)^6 \Rightarrow k\le K,\quad N=\#\{x\in(n-k,n]:\ s_k(x)=x\}$

Here $s_k(m)=\prod_{p\le k \text{ prime}}p^{v_p(m)}$ is the $k$-smooth part and $N$ the number of $k$-smooth elements of the window. The parent statement with two more hypotheses, the last two: $N<k^{1/10}$, and $\log n\,(\log\log k)^6$ exceeds $c_1\log^4 k$ for a second constant $c_1>0$ quantified before $K$. In the Lean, n / k is the floor, n % k the remainder, n - k truncated subtraction (here $k\le n$), Nat.log p k is $\lfloor\log_p k\rfloor$, minFac the least prime factor, Finset.Ioc (n - k) n the window, and the number of smooth elements is written as a sum of ones.

Rendered from gloss/8bb868af298d….md · Improve these words →

Rendered from targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/Statement.lean. Origin: compiler-derived.

Depends on (1), each in words

Depends on, each in words where someone has written them:

  • erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2--h1: $\forall c>0\ \forall c_1>0\ \exists K\ \forall n,k:\ 0<k,\ k^3\le n,\ \exp(c\log^2 k)<n,\ \operatorname{lpf}\binom nk>\lfloor n/k\rfloor,\ \bigl(\forall p \text{ prime},\ k<p\le\lfloor n/k\rfloor,\ \forall m\in(n-k,n],\ p\nmid m\bigr),\ \prod_{m=n-k+1}^{n} s_k(m) = k!,\ \bigl(\forall m\in(n-k,n]:\ s_k(m)=m \text{ or } s_k(m)<k\bigr),\ \lfloor n/k\rfloor\mid\binom{k-1}{n \bmod k},\ n<k\,2^{k-1}+k,\ \bigl(\exists x\in(n-k,n]:\ x\ne k\lfloor n/k\rfloor,\ s_k(x)=x\bigr),\ (n-k)^2<k\prod_{p\le k \text{ prime}}p^{\lfloor\log_p k\rfloor},\ N^{10}<k,\ c_1\log^4 k<\log n\,(\log\log k)^6,\ \log^7 k<\log n \Rightarrow k\le K,\quad N=\#\{x\in(n-k,n]:\ s_k(x)=x\}$
Attestation: none of a proof

No proof of this statement has been merged. A partial assembly merged under attestations/002858.json: it records an attempt, not a proof (see Attempts below).

Witness

Not checked — no run on this page records step 7 passing for it.

Witness.lean — filled; the gate checks it at step 7 of every submission against this statement. Rendered from targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/Witness.lean, sha256 d957155d579e.
import Mathlib

open scoped Nat

theorem witness : ∃ (c c₁ : ℝ), (0 : ℝ) < c ∧ (0 : ℝ) < c₁ := ⟨1, 1, one_pos, one_pos⟩

No gloss yet: no one has written in words what this witness says (D-3 v3.30); the Lean above is the witness. How to write one →

Attempts (1)

1 recorded. Routes refuted: none. Failure classes: none.

Untrusted contributor text — a partial assembly: it records an attempt, not a proof; its attestation names the file, and no hash in the record binds these bytes.

Untrusted: partial assembly (D-12 #5), by t1203r2-k; merged in f5e3e8f95fc8 under attestations/002858.json, whose step 2 took this file as a partial. A partial records an attempt, not a proof: the gate replayed it with its holes left as sorry. Rendered from targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean.

Untrusted contributor text — an outline the gate extracted from this partial assembly; its holes are sorry steps, and it records an attempt, not a proof.

Outline of 20261203T140457Z-t1203r2-k-partial.lean, extracted by the gate at 029533477f5e from the proof’s elaboration: each step is a claim the proof establishes and the goal it leaves. Open a step for what it uses and its Lean lines.

  1. h_few7 hole ∀ (c : ℝ), (0 : ℝ) < c → ∀ (c₁ : ℝ), (0 : ℝ) < c₁ → ∃ (K : ℕ), ∀ (n k : ℕ), (0 : ℕ) < k → k * k * k ≤ n → Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) → (n.choose k).minFac > n / k → (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) → ∏ m ∈ Finset.Ioc (n - k) n, ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = k ! → (∀ (m : ℕ), n - k < m → m ≤ n → ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) → n / k ∣ (k - (1 : ℕ)).choose (n % k) → n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k → (∃ (x : ℕ), n - k < x ∧ x ≤ n ∧ x ≠ k * (n / k) ∧ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x) → (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k → (∑ x ∈ Finset.Ioc (n - k) n with ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x, (1 : ℕ)) ^ (10 : ℕ) < k → c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) < (truncated) hole

    Leaves ∀ (c : ℝ), (0 : ℝ) < c → ∀ (c₁ : ℝ), (0 : ℝ) < c₁ → ∃ (K : ℕ), ∀ (n k : ℕ), (0 : ℕ) < k → k * k * k ≤ n → Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) → (n.choose k).minFac > n / k → (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) → ∏ m ∈ Finset.Ioc (n - k) n, ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = k ! → (∀ (m : ℕ), n - k < m → m ≤ n → ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) → n / k ∣ (k - (1 : ℕ)).choose (n % k) → n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k → (∃ (x : ℕ), n - k < x ∧ x ≤ n ∧ x ≠ k * (n / k) ∧ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x) → (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k → (∑ x ∈ Finset.Ioc (n - k) n with ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x, (1 : ℕ)) ^ (10 : ℕ) < k → c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) < (truncated), with h_few7 : ∀ (c : ℝ), (0 : ℝ) < c → ∀ (c₁ : ℝ), (0 : ℝ) < c₁ → ∃ (K : ℕ), ∀ (n k : ℕ), (0 : ℕ) < k → k * k * k ≤ n → Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) → (n.choose k).minFac > n / k → (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) → ∏ m ∈ Finset.Ioc (n - k) n, ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = k ! → (∀ (m : ℕ), n - k < m → m ≤ n → ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) → n / k ∣ (k - (1 : ℕ)).choose (n % k) → n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k → (∃ (x : ℕ), n - k < x ∧ x ≤ n ∧ x ≠ k * (n / k) ∧ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x) → (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k → (∑ x ∈ Finset.Ioc (n - k) n with ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x, (1 : ℕ)) ^ (10 : ℕ) < k → c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) < (truncated).

    Uses library Real.exp The real exponential function, defined as the real part of the complex exponential , Real.log The real logarithm function, equal to the inverse of the exponential for x > 0, to log |x| for x < 0, and to 0 for 0. , Nat.choose choose n k is the number of k-element subsets in an n-element set. , Nat.minFac Returns the smallest prime factor of n ≠ 1. , Nat.Prime Nat.Prime p means that p is a prime number, that is, a natural number at least 2 whose only divisors are p and 1. , Finset.Ioc The finset $(a, b]$ of elements x such that a < x and x ≤ b. , Finset.Icc The finset $[a, b]$ of elements x such that a ≤ x and x ≤ b. , Nat.factorization n.factorization is the finitely supported function ℕ →₀ ℕ mapping each prime factor of n to its multiplicity in n. , Nat.factorial Nat.factorial n is the factorial of n. , Nat.log log b n, is the logarithm of natural number n in base b. .

    Left open as a hole; its statement is erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2--h1 (open).

    Lean, lines 2578–2617
      have h_few7 :
          ∀ (c : ℝ),
            (0 : ℝ) < c →
              ∀ (c₁ : ℝ),
                (0 : ℝ) < c₁ →
                  ∃ (K : ℕ),
                    ∀ (n k : ℕ),
                      (0 : ℕ) < k →
                        k * k * k ≤ n →
                          Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) →
                            (n.choose k).minFac > n / k →
                              (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) →
                                ∏ m ∈ Finset.Ioc (n - k) n,
                                      ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p =
                                    k.factorial →
                                  (∀ (m : ℕ),
                                      n - k < m →
                                        m ≤ n →
                                          ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨
                                            ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) →
                                    n / k ∣ (k - (1 : ℕ)).choose (n % k) →
                                      n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k →
                                        (∃ (x : ℕ),
                                            n - k < x ∧
                                              x ≤ n ∧
                                                x ≠ k * (n / k) ∧
                                                  ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                                    x) →
                                          (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k →
                                            (∑ x ∈ Finset.Ioc (n - k) n with
                                                    ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                                      x,
                                                    (1 : ℕ)) ^
                                                  (10 : ℕ) <
                                                k →
                                              c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) <
                                                  Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) →
            Real.log (↑k : ℝ) ^ (7 : ℕ) < Real.log (↑n : ℝ) →
            k ≤ K := by
        sorry

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2578–2617 at 95be5d21dd3d ↗

  2. s2 obtain ∃ (u₀ : ℕ) (K₀ : ℕ), ∀ (n k : ℕ), K₀ ≤ k → k ^ u₀ ≤ n → Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ (98 : ℕ) → k ^ ((98 : ℕ) + (1 : ℕ)) ≤ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k}.card ^ ((98 : ℕ) + (2 : ℕ))

    Leaves ∃ (K : ℕ), ∀ (n k : ℕ), (0 : ℕ) < k → k * k * k ≤ n → Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) → (n.choose k).minFac > n / k → (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) → ∏ m ∈ Finset.Ioc (n - k) n, ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = k ! → (∀ (m : ℕ), n - k < m → m ≤ n → ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) → n / k ∣ (k - (1 : ℕ)).choose (n % k) → n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k → (∃ (x : ℕ), n - k < x ∧ x ≤ n ∧ x ≠ k * (n / k) ∧ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x) → (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k → (∑ x ∈ Finset.Ioc (n - k) n with ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x, (1 : ℕ)) ^ (10 : ℕ) < k → c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) < Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) → k ≤ K, with u₀ : ℕ, K₀ : ℕ, hdense : ∀ (n k : ℕ), K₀ ≤ k → k ^ u₀ ≤ n → Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ (98 : ℕ) → k ^ ((98 : ℕ) + (1 : ℕ)) ≤ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k}.card ^ ((98 : ℕ) + (2 : ℕ)).

    Lean, line 2619
      obtain ⟨u₀, K₀, hdense⟩ := t5_dense_lit 98

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2619 at 95be5d21dd3d ↗

  3. s3 obtain ∃ (K₀ : ℕ), ∀ (m k : ℕ), K₀ ≤ k → (0 : ℕ) < m → Real.log (↑m : ℝ) ≤ Real.log (↑k : ℝ) ^ (7 : ℕ) → {d ∈ Finset.Ioc k ((2 : ℕ) * k) | d ∣ m}.card ^ ((7 : ℕ) + (2 : ℕ)) < k ^ ((7 : ℕ) + (1 : ℕ))

    Leaves ∃ (K : ℕ), ∀ (n k : ℕ), (0 : ℕ) < k → k * k * k ≤ n → Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) → (n.choose k).minFac > n / k → (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) → ∏ m ∈ Finset.Ioc (n - k) n, ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = k ! → (∀ (m : ℕ), n - k < m → m ≤ n → ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) → n / k ∣ (k - (1 : ℕ)).choose (n % k) → n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k → (∃ (x : ℕ), n - k < x ∧ x ≤ n ∧ x ≠ k * (n / k) ∧ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x) → (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k → (∑ x ∈ Finset.Ioc (n - k) n with ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x, (1 : ℕ)) ^ (10 : ℕ) < k → c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) < Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) → k ≤ K, with K₀' : ℕ, hdiv : ∀ (m k : ℕ), K₀' ≤ k → (0 : ℕ) < m → Real.log (↑m : ℝ) ≤ Real.log (↑k : ℝ) ^ (7 : ℕ) → {d ∈ Finset.Ioc k ((2 : ℕ) * k) | d ∣ m}.card ^ ((7 : ℕ) + (2 : ℕ)) < k ^ ((7 : ℕ) + (1 : ℕ)).

    Lean, line 2620
      obtain ⟨K₀', hdiv⟩ := h_divA 7 (by norm_num)

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2620 at 95be5d21dd3d ↗

  4. s4 obtain ∃ (K : ℕ), ∀ (n k : ℕ), (0 : ℕ) < k → k * k * k ≤ n → Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) → (n.choose k).minFac > n / k → (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) → ∏ m ∈ Finset.Ioc (n - k) n, ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = k ! → (∀ (m : ℕ), n - k < m → m ≤ n → ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) → n / k ∣ (k - (1 : ℕ)).choose (n % k) → n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k → (∃ (x : ℕ), n - k < x ∧ x ≤ n ∧ x ≠ k * (n / k) ∧ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x) → (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k → (∑ x ∈ Finset.Ioc (n - k) n with ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x, (1 : ℕ)) ^ (10 : ℕ) < k → c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) < Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) → Real.log (↑k : ℝ) ^ (7 : ℕ) < Real.log (↑n : ℝ) → k ≤ K

    Leaves ∃ (K : ℕ), ∀ (n k : ℕ), (0 : ℕ) < k → k * k * k ≤ n → Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) → (n.choose k).minFac > n / k → (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) → ∏ m ∈ Finset.Ioc (n - k) n, ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = k ! → (∀ (m : ℕ), n - k < m → m ≤ n → ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) → n / k ∣ (k - (1 : ℕ)).choose (n % k) → n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k → (∃ (x : ℕ), n - k < x ∧ x ≤ n ∧ x ≠ k * (n / k) ∧ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x) → (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k → (∑ x ∈ Finset.Ioc (n - k) n with ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x, (1 : ℕ)) ^ (10 : ℕ) < k → c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) < Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) → k ≤ K, with KF : ℕ, hKF : ∀ (n k : ℕ), (0 : ℕ) < k → k * k * k ≤ n → Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) → (n.choose k).minFac > n / k → (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) → ∏ m ∈ Finset.Ioc (n - k) n, ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = k ! → (∀ (m : ℕ), n - k < m → m ≤ n → ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) → n / k ∣ (k - (1 : ℕ)).choose (n % k) → n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k → (∃ (x : ℕ), n - k < x ∧ x ≤ n ∧ x ≠ k * (n / k) ∧ ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x) → (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k → (∑ x ∈ Finset.Ioc (n - k) n with ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x, (1 : ℕ)) ^ (10 : ℕ) < k → c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) < Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) → Real.log (↑k : ℝ) ^ (7 : ℕ) < Real.log (↑n : ℝ) → k ≤ KF.

    Lean, line 2621
      obtain ⟨KF, hKF⟩ := h_few7 c hc c₁ hc₁

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2621 at 95be5d21dd3d ↗

  5. hgt have KF + K₀ + K₀' + ⌈Real.exp ((↑u₀ : ℝ) / c)⌉₊ + (2 : ℕ) + ⌈Real.exp (1 : ℝ)⌉₊ < k

    Leaves False, with hgt : KF + K₀ + K₀' + ⌈Real.exp ((↑u₀ : ℝ) / c)⌉₊ + (2 : ℕ) + ⌈Real.exp (1 : ℝ)⌉₊ < k.

    Uses library not_le, Iff.mp Modus ponens for if and only if. .

    Lean, line 2625
      have hgt := not_le.mp hcon

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2625 at 95be5d21dd3d ↗

  6. hk2 have (2 : ℕ) ≤ k routine: omega
  7. hsq have k * k ≤ n

    Leaves False, with hsq : k * k ≤ n.

    Lean, lines 2627–2630
      have hsq : k * k ≤ n :=
        calc k * k = k * k * 1 := (Nat.mul_one _).symm
          _ ≤ k * k * k := Nat.mul_le_mul_left _ hk
          _ ≤ n := h3

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2627–2630 at 95be5d21dd3d ↗

    1. hsq.s1 calc k * k = k * k * (1 : ℕ)

      Closes the goal.

      Uses library Nat.mul_one, Eq.symm Equality is symmetric: if a = b then b = a. .

      Lean, line 2628
          calc k * k = k * k * 1 := (Nat.mul_one _).symm

      targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2628 at 95be5d21dd3d ↗

    2. hsq.s2 calc k * k * (1 : ℕ) ≤ k * k * k

      Closes the goal.

      Uses library Nat.mul_le_mul_left.

      Lean, line 2629
            _ ≤ k * k * k := Nat.mul_le_mul_left _ hk

      targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2629 at 95be5d21dd3d ↗

    3. hsq.s3 calc k * k * k ≤ n
  8. hkn have k ≤ n

    Leaves False, with hkn : k ≤ n.

    Uses library le_trans The relation ≤ on a preorder is transitive. , Nat.le_mul_self.

    Lean, line 2631
      have hkn : k ≤ n := le_trans (Nat.le_mul_self k) hsq

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2631 at 95be5d21dd3d ↗

  9. hl1 have (1 : ℝ) ≤ Real.log (↑k : ℝ)

    Leaves False, with hl1 : (1 : ℝ) ≤ Real.log (↑k : ℝ).

    Uses library Real.log The real logarithm function, equal to the inverse of the exponential for x > 0, to log |x| for x < 0, and to 0 for 0. , Real.le_log_iff_exp_le, Iff.mpr Modus ponens for if and only if, reversed. .

    Lean, lines 2632–2635
      have hl1 : 1 ≤ Real.log (k : ℝ) := by
        have h1 : Real.exp 1 ≤ (k : ℝ) :=
          le_trans (Nat.le_ceil _) (by exact_mod_cast (show ⌈Real.exp 1⌉₊ ≤ k by omega))
        exact (Real.le_log_iff_exp_le (by exact_mod_cast hk)).mpr h1

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2632–2635 at 95be5d21dd3d ↗

    1. hl1.h1 have Real.exp (1 : ℝ) ≤ (↑k : ℝ)

      Leaves (1 : ℝ) ≤ Real.log (↑k : ℝ), with h1 : Real.exp (1 : ℝ) ≤ (↑k : ℝ).

      Uses library Real.exp The real exponential function, defined as the real part of the complex exponential , le_trans The relation ≤ on a preorder is transitive. , Nat.le_ceil.

      Lean, lines 2633–2634
          have h1 : Real.exp 1 ≤ (k : ℝ) :=
            le_trans (Nat.le_ceil _) (by exact_mod_cast (show ⌈Real.exp 1⌉₊ ≤ k by omega))

      targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2633–2634 at 95be5d21dd3d ↗

  10. s10 case

    Leaves False, with hnear : Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ (7 : ℕ).

    Uses library Finset.card_eq_sum_ones, absurd Anything follows from two contradictory hypotheses. , not_lt, Iff.mpr Modus ponens for if and only if, reversed. .

    Lean, lines 2637–2665
      · have hbig : k ≤ (Finset.filter (fun (x : ℕ) => ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p,
            p ^ (x.factorization : ℕ → ℕ) p = x) (Finset.Ioc (n - k) n)).card ^ (10 : ℕ) := by
          have hu : k ^ u₀ ≤ n := t1_kpow c hc u₀ n k hk (by omega) hexp
          have hN := hdense n k (by omega) hu (le_trans hnear (pow_le_pow_right₀ hl1 (by norm_num)))
          refine many_count7 _ _ k ?_ ?_ hN hk
          · intro d hd
            rw [Finset.mem_filter, Finset.mem_Ioc] at hd
            have hdk : d * k ≤ n :=
              calc d * k ≤ (2 * k) * k := Nat.mul_le_mul_right k hd.1.2
                _ ≤ (k * k) * k := Nat.mul_le_mul_right k (Nat.mul_le_mul_right k hk2)
                _ ≤ n := h3
            have hlt := many_landing n k hk hkn hfree d hd.1.1 ((Nat.le_div_iff_mul_le hk).mpr hdk) hd.2
            have hmle : n % d ≤ n := Nat.mod_le n d
            have hx1 : n - k < n - n % d := by omega
            have hx2 : n - n % d ≤ n := Nat.sub_le _ _
            refine ⟨n - n % d, Finset.mem_filter.mpr ⟨Finset.mem_Ioc.mpr ⟨hx1, hx2⟩, ?_⟩, Nat.dvd_sub_mod n⟩
            rcases hsplit (n - n % d) hx1 hx2 with h | h
            · exact h
            · omega
          · intro x hx
            have hxw := Finset.mem_Ioc.mp (Finset.mem_filter.mp hx).1
            have hx0 : 0 < x := by omega
            have hlog : Real.log ((x : ℕ) : ℝ) ≤ Real.log (n : ℝ) :=
              Real.log_le_log (by exact_mod_cast hx0) (by exact_mod_cast hxw.2)
            refine lt_of_le_of_lt (Nat.pow_le_pow_left (Finset.card_le_card ?_) 9)
              (hdiv x k (by omega) hx0 (le_trans hlog hnear))
            exact Finset.filter_subset_filter _ (Finset.filter_subset _ _)
        rw [Finset.card_eq_sum_ones] at hbig
        exact absurd hN10 (not_lt.mpr hbig)

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2637–2665 at 95be5d21dd3d ↗

    1. s10.hbig have k ≤ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}.card ^ (10 : ℕ)

      Leaves False, with hbig : k ≤ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}.card ^ (10 : ℕ).

      Uses library Finset.filter Finset.filter p s is the set of elements of s that satisfy p. , Finset.Icc The finset $[a, b]$ of elements x such that a ≤ x and x ≤ b. , Nat.Prime Nat.Prime p means that p is a prime number, that is, a natural number at least 2 whose only divisors are p and 1. , Nat.factorization n.factorization is the finitely supported function ℕ →₀ ℕ mapping each prime factor of n to its multiplicity in n. , Finset.Ioc The finset $(a, b]$ of elements x such that a < x and x ≤ b. , Finset.card s.card is the number of elements of s, aka its cardinality. .

      Lean, lines 2637–2663
        · have hbig : k ≤ (Finset.filter (fun (x : ℕ) => ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p,
              p ^ (x.factorization : ℕ → ℕ) p = x) (Finset.Ioc (n - k) n)).card ^ (10 : ℕ) := by
            have hu : k ^ u₀ ≤ n := t1_kpow c hc u₀ n k hk (by omega) hexp
            have hN := hdense n k (by omega) hu (le_trans hnear (pow_le_pow_right₀ hl1 (by norm_num)))
            refine many_count7 _ _ k ?_ ?_ hN hk
            · intro d hd
              rw [Finset.mem_filter, Finset.mem_Ioc] at hd
              have hdk : d * k ≤ n :=
                calc d * k ≤ (2 * k) * k := Nat.mul_le_mul_right k hd.1.2
                  _ ≤ (k * k) * k := Nat.mul_le_mul_right k (Nat.mul_le_mul_right k hk2)
                  _ ≤ n := h3
              have hlt := many_landing n k hk hkn hfree d hd.1.1 ((Nat.le_div_iff_mul_le hk).mpr hdk) hd.2
              have hmle : n % d ≤ n := Nat.mod_le n d
              have hx1 : n - k < n - n % d := by omega
              have hx2 : n - n % d ≤ n := Nat.sub_le _ _
              refine ⟨n - n % d, Finset.mem_filter.mpr ⟨Finset.mem_Ioc.mpr ⟨hx1, hx2⟩, ?_⟩, Nat.dvd_sub_mod n⟩
              rcases hsplit (n - n % d) hx1 hx2 with h | h
              · exact h
              · omega
            · intro x hx
              have hxw := Finset.mem_Ioc.mp (Finset.mem_filter.mp hx).1
              have hx0 : 0 < x := by omega
              have hlog : Real.log ((x : ℕ) : ℝ) ≤ Real.log (n : ℝ) :=
                Real.log_le_log (by exact_mod_cast hx0) (by exact_mod_cast hxw.2)
              refine lt_of_le_of_lt (Nat.pow_le_pow_left (Finset.card_le_card ?_) 9)
                (hdiv x k (by omega) hx0 (le_trans hlog hnear))
              exact Finset.filter_subset_filter _ (Finset.filter_subset _ _)

      targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2637–2663 at 95be5d21dd3d ↗

      1. s10.hbig.hu have k ^ u₀ ≤ n

        Leaves k ≤ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}.card ^ (10 : ℕ), with hu : k ^ u₀ ≤ n.

        Lean, line 2639
              have hu : k ^ u₀ ≤ n := t1_kpow c hc u₀ n k hk (by omega) hexp

        targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2639 at 95be5d21dd3d ↗

      2. s10.hbig.hN have k ^ ((98 : ℕ) + (1 : ℕ)) ≤ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k}.card ^ ((98 : ℕ) + (2 : ℕ))

        Leaves k ≤ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}.card ^ (10 : ℕ), with hN : k ^ ((98 : ℕ) + (1 : ℕ)) ≤ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k}.card ^ ((98 : ℕ) + (2 : ℕ)).

        Uses library le_trans The relation ≤ on a preorder is transitive. , pow_le_pow_right₀.

        Lean, line 2640
              have hN := hdense n k (by omega) hu (le_trans hnear (pow_le_pow_right₀ hl1 (by norm_num)))

        targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2640 at 95be5d21dd3d ↗

      3. s10.hbig.s3 case

        Leaves ∀ d ∈ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k}, ∃ x ∈ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}, d ∣ x.

        Uses library Finset.mem_filter, Finset.mem_Ioc, Exists.intro Existential introduction. , Iff.mpr Modus ponens for if and only if, reversed. , And.intro And.intro : a → b → a ∧ b is the constructor for the And operation. , Nat.dvd_sub_mod.

        Lean, lines 2642–2655
              · intro d hd
                rw [Finset.mem_filter, Finset.mem_Ioc] at hd
                have hdk : d * k ≤ n :=
                  calc d * k ≤ (2 * k) * k := Nat.mul_le_mul_right k hd.1.2
                    _ ≤ (k * k) * k := Nat.mul_le_mul_right k (Nat.mul_le_mul_right k hk2)
                    _ ≤ n := h3
                have hlt := many_landing n k hk hkn hfree d hd.1.1 ((Nat.le_div_iff_mul_le hk).mpr hdk) hd.2
                have hmle : n % d ≤ n := Nat.mod_le n d
                have hx1 : n - k < n - n % d := by omega
                have hx2 : n - n % d ≤ n := Nat.sub_le _ _
                refine ⟨n - n % d, Finset.mem_filter.mpr ⟨Finset.mem_Ioc.mpr ⟨hx1, hx2⟩, ?_⟩, Nat.dvd_sub_mod n⟩
                rcases hsplit (n - n % d) hx1 hx2 with h | h
                · exact h
                · omega

        targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2642–2655 at 95be5d21dd3d ↗

        1. s10.hbig.s3.hdk have d * k ≤ n

          Leaves ∃ x ∈ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}, d ∣ x, with hdk : d * k ≤ n.

          Lean, lines 2644–2647
                  have hdk : d * k ≤ n :=
                    calc d * k ≤ (2 * k) * k := Nat.mul_le_mul_right k hd.1.2
                      _ ≤ (k * k) * k := Nat.mul_le_mul_right k (Nat.mul_le_mul_right k hk2)
                      _ ≤ n := h3

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2644–2647 at 95be5d21dd3d ↗

          1. s10.hbig.s3.hdk.s1 calc d * k ≤ (2 : ℕ) * k * k

            Closes the goal.

            Uses library Nat.mul_le_mul_right.

            Lean, line 2645
                      calc d * k ≤ (2 * k) * k := Nat.mul_le_mul_right k hd.1.2

            targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2645 at 95be5d21dd3d ↗

          2. s10.hbig.s3.hdk.s2 calc (2 : ℕ) * k * k ≤ k * k * k

            Closes the goal.

            Uses library Nat.mul_le_mul_right.

            Lean, line 2646
                        _ ≤ (k * k) * k := Nat.mul_le_mul_right k (Nat.mul_le_mul_right k hk2)

            targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2646 at 95be5d21dd3d ↗

          3. s10.hbig.s3.hdk.s3 calc k * k * k ≤ n
        2. s10.hbig.s3.hlt have k < ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ ((n - n % d).factorization : ℕ → ℕ) p

          Leaves ∃ x ∈ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}, d ∣ x, with hlt : k < ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ ((n - n % d).factorization : ℕ → ℕ) p.

          Uses library Nat.le_div_iff_mul_le, Iff.mpr Modus ponens for if and only if, reversed. .

          Lean, line 2648
                  have hlt := many_landing n k hk hkn hfree d hd.1.1 ((Nat.le_div_iff_mul_le hk).mpr hdk) hd.2

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2648 at 95be5d21dd3d ↗

        3. s10.hbig.s3.hmle have n % d ≤ n

          Leaves ∃ x ∈ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}, d ∣ x, with hmle : n % d ≤ n.

          Uses library Nat.mod_le.

          Lean, line 2649
                  have hmle : n % d ≤ n := Nat.mod_le n d

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2649 at 95be5d21dd3d ↗

        4. s10.hbig.s3.hx1 have n - k < n - n % d routine: omega

          Leaves ∃ x ∈ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}, d ∣ x, with hx1 : n - k < n - n % d.

          Lean, line 2650
                  have hx1 : n - k < n - n % d := by omega

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2650 at 95be5d21dd3d ↗

        5. s10.hbig.s3.hx2 have n - n % d ≤ n

          Leaves ∃ x ∈ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}, d ∣ x, with hx2 : n - n % d ≤ n.

          Uses library Nat.sub_le.

          Lean, line 2651
                  have hx2 : n - n % d ≤ n := Nat.sub_le _ _

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2651 at 95be5d21dd3d ↗

        6. s10.hbig.s3.s6 case

          Leaves ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ ((n - n % d).factorization : ℕ → ℕ) p = n - n % d, with h : ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ ((n - n % d).factorization : ℕ → ℕ) p = n - n % d.

          Lean, line 2654
                  · exact h

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2654 at 95be5d21dd3d ↗

        7. s10.hbig.s3.s7 case routine: omega

          Leaves ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ ((n - n % d).factorization : ℕ → ℕ) p = n - n % d, with h : ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ ((n - n % d).factorization : ℕ → ℕ) p < k.

          Lean, line 2655
                  · omega

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2655 at 95be5d21dd3d ↗

      4. s10.hbig.s4 case

        Leaves ∀ x ∈ {x ∈ Finset.Ioc (n - k) n | ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p = x}, {d ∈ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k} | d ∣ x}.card ^ (9 : ℕ) < k ^ (8 : ℕ).

        Uses library lt_of_le_of_lt, Nat.pow_le_pow_left, Finset.card_le_card, le_trans The relation ≤ on a preorder is transitive. , Finset.filter_subset_filter, Finset.filter_subset.

        Lean, lines 2656–2663
              · intro x hx
                have hxw := Finset.mem_Ioc.mp (Finset.mem_filter.mp hx).1
                have hx0 : 0 < x := by omega
                have hlog : Real.log ((x : ℕ) : ℝ) ≤ Real.log (n : ℝ) :=
                  Real.log_le_log (by exact_mod_cast hx0) (by exact_mod_cast hxw.2)
                refine lt_of_le_of_lt (Nat.pow_le_pow_left (Finset.card_le_card ?_) 9)
                  (hdiv x k (by omega) hx0 (le_trans hlog hnear))
                exact Finset.filter_subset_filter _ (Finset.filter_subset _ _)

        targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2656–2663 at 95be5d21dd3d ↗

        1. s10.hbig.s4.hxw have n - k < x ∧ x ≤ n

          Leaves {d ∈ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k} | d ∣ x}.card ^ (9 : ℕ) < k ^ (8 : ℕ), with hxw : n - k < x ∧ x ≤ n.

          Uses library Finset.mem_Ioc, Iff.mp Modus ponens for if and only if. , Finset.mem_filter.

          Lean, line 2657
                  have hxw := Finset.mem_Ioc.mp (Finset.mem_filter.mp hx).1

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2657 at 95be5d21dd3d ↗

        2. s10.hbig.s4.hx0 have (0 : ℕ) < x routine: omega

          Leaves {d ∈ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k} | d ∣ x}.card ^ (9 : ℕ) < k ^ (8 : ℕ), with hx0 : (0 : ℕ) < x.

          Lean, line 2658
                  have hx0 : 0 < x := by omega

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2658 at 95be5d21dd3d ↗

        3. s10.hbig.s4.hlog have Real.log (↑x : ℝ) ≤ Real.log (↑n : ℝ)

          Leaves {d ∈ {d ∈ Finset.Ioc k ((2 : ℕ) * k) | n % d < k} | d ∣ x}.card ^ (9 : ℕ) < k ^ (8 : ℕ), with hlog : Real.log (↑x : ℝ) ≤ Real.log (↑n : ℝ).

          Uses library Real.log The real logarithm function, equal to the inverse of the exponential for x > 0, to log |x| for x < 0, and to 0 for 0. , Real.log_le_log.

          Lean, lines 2659–2660
                  have hlog : Real.log ((x : ℕ) : ℝ) ≤ Real.log (n : ℝ) :=
                    Real.log_le_log (by exact_mod_cast hx0) (by exact_mod_cast hxw.2)

          targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2659–2660 at 95be5d21dd3d ↗

  11. s11 case

    Leaves False, with hnear : ¬Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ (7 : ℕ).

    Lean, lines 2666–2667
      · have := hKF n k hk h3 hexp hmin hfree hsmooth hsplit hD1 hbd hex hA hN10 hrange (not_le.mp hnear)
        omega

    targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, lines 2666–2667 at 95be5d21dd3d ↗

    1. s11.this have k ≤ KF

      Leaves False, with this : k ≤ KF.

      Uses library not_le, Iff.mp Modus ponens for if and only if. .

      Lean, line 2666
        · have := hKF n k hk h3 hexp hmin hfree hsmooth hsplit hD1 hbd hex hA hN10 hrange (not_le.mp hnear)

      targets/erdos-1094/nodes/erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2/attempts/20261203T140457Z-t1203r2-k-partial.lean, line 2666 at 95be5d21dd3d ↗

import Mathlib
import Nodes.«erdos-1094--h2--h2--h5--h2--h2--h1--h1--h2».Context

open scoped Nat

/-! Hole `h_few` of a merged partial proof, as a node (D-12 #5, D-29).
Statement generated by the gate from the assembly; immutable (D-3). -/

/-! `many_landing` is t1203-b's t1_landing without the deficiency-one hypothesis; `t1_kpow` is t1203-b's. -/

private theorem many_landing (n k : ℕ) (hk : 0 < k) (hkn : k ≤ n)
    (hfree : ∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m)
    (d : ℕ) (hd1 : k < d) (hd2 : d ≤ n / k) (hd3 : n % d < k) :
    k < ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ ((n - n % d).factorization : ℕ → ℕ) p := by
  have hd0 : 0 < d := by omega
  -- the multiple of d in the window
  have hxd : d ∣ n - n % d := Nat.dvd_sub_mod n
  have hmodle : n % d ≤ n := Nat.mod_le n d
  have hx1 : n - k < n - n % d := by omega
  have hx2 : n - n % d ≤ n := Nat.sub_le _ _
  have hx0 : n - n % d ≠ 0 := by omega
  generalize hxdef : n - n % d = x at hxd hx1 hx2 hx0
  set S := (Finset.Icc 1 k).filter Nat.Prime with hS
  set s := ∏ p ∈ S, p ^ (x.factorization p) with hsdef
  set r := ∏ p ∈ x.primeFactors.filter (fun p => k < p), p ^ (x.factorization p) with hr
  -- x = s * r
  have hsplit : x = s * r := by
    have h1 : x = ∏ p ∈ x.primeFactors, p ^ (x.factorization p) :=
      (Nat.prod_factorization_pow_eq_self hx0).symm.trans
        (by rw [Nat.prod_factorization_eq_prod_primeFactors])
    have h2 : (∏ p ∈ x.primeFactors, p ^ (x.factorization p)) =
        (∏ p ∈ x.primeFactors.filter (fun p => p ≤ k), p ^ (x.factorization p)) *
        (∏ p ∈ x.primeFactors.filter (fun p => ¬ p ≤ k), p ^ (x.factorization p)) :=
      (Finset.prod_filter_mul_prod_filter_not _ _ _).symm
    have h3 : (∏ p ∈ x.primeFactors.filter (fun p => p ≤ k), p ^ (x.factorization p)) = s := by
      rw [hsdef]
      apply Finset.prod_subset
      · intro p hp
        rw [Finset.mem_filter, Nat.mem_primeFactors] at hp
        exact Finset.mem_filter.mpr ⟨Finset.mem_Icc.mpr ⟨hp.1.1.one_le, hp.2⟩, hp.1.1⟩
      · intro p hpS hpn
        rw [Finset.mem_filter, Nat.mem_primeFactors] at hpn
        have hp : p.Prime := (Finset.mem_filter.mp hpS).2
        have hpk : p ≤ k := (Finset.mem_Icc.mp (Finset.mem_filter.mp hpS).1).2
        have : ¬ p ∣ x := fun h => hpn ⟨⟨hp, h, hx0⟩, hpk⟩
        rw [Nat.factorization_eq_zero_of_not_dvd this, pow_zero]
    have h4 : x.primeFactors.filter (fun p => ¬ p ≤ k) = x.primeFactors.filter (fun p => k < p) := by
      apply Finset.filter_congr; intro p _; exact not_le
    rw [h1, h2, h3, h4]
  -- d is coprime to r: a common prime would be a prime of (k, n/k] dividing x
  have hcop : Nat.Coprime d r := by
    apply Nat.coprime_of_dvd
    intro q hq hqd hqr
    have hkq : k < q := by
      rw [hr] at hqr
      obtain ⟨p, hp, hqp⟩ := (Prime.dvd_finsetProd_iff hq.prime _).mp hqr
      have hpk : k < p := (Finset.mem_filter.mp hp).2
      have hpp : p.Prime := (Nat.mem_primeFactors.mp (Finset.mem_filter.mp hp).1).1
      have : q = p := (Nat.prime_dvd_prime_iff_eq hq hpp).mp (hq.dvd_of_dvd_pow hqp)
      omega
    have hqle : q ≤ n / k := le_trans (Nat.le_of_dvd hd0 hqd) hd2
    exact hfree q hq hkq hqle x hx1 hx2 (hqd.trans hxd)
  have hds : d ∣ s := hcop.dvd_of_dvd_mul_right (hsplit ▸ hxd)
  have hspos : 0 < s := by
    rw [hsdef]
    apply Finset.prod_pos
    intro p hp
    exact pow_pos (Finset.mem_filter.mp hp).2.pos _
  have hle : d ≤ s := Nat.le_of_dvd hspos hds
  omega

/-- For `k ≥ exp(u₀/c)`, `exp(c·log²k) < n` gives `k^u₀ ≤ n`. -/
private theorem t1_kpow (c : ℝ) (hc : 0 < c) (u₀ n k : ℕ) (hk : 0 < k)
    (hbig : ⌈Real.exp ((u₀ : ℝ) / c)⌉₊ ≤ k)
    (hexp : Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ)) : k ^ u₀ ≤ n := by
  have hkpos : (0 : ℝ) < (k : ℝ) := by exact_mod_cast hk
  have hk' : Real.exp ((u₀ : ℝ) / c) ≤ (k : ℝ) := le_trans (Nat.le_ceil _) (by exact_mod_cast hbig)
  have hlog : (u₀ : ℝ) / c ≤ Real.log (k : ℝ) := by
    have := Real.log_le_log (Real.exp_pos _) hk'
    rwa [Real.log_exp] at this
  have h21 : (u₀ : ℝ) ≤ c * Real.log (k : ℝ) := by
    rw [div_le_iff₀ hc] at hlog
    linarith
  have hlk : 0 ≤ Real.log (k : ℝ) := Real.log_nonneg (by exact_mod_cast hk)
  have h1 : ((k : ℝ)) ^ u₀ = Real.exp ((u₀ : ℝ) * Real.log (k : ℝ)) := by
    rw [Real.exp_nat_mul, Real.exp_log hkpos]
  have h2 : Real.exp ((u₀ : ℝ) * Real.log (k : ℝ)) ≤ Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) := by
    apply Real.exp_le_exp.mpr
    nlinarith [mul_le_mul_of_nonneg_right h21 hlk]
  have h3 : ((k : ℝ)) ^ u₀ < (n : ℝ) := by
    rw [h1]
    exact lt_of_le_of_lt h2 hexp
  exact_mod_cast h3.le



/-- The covering count with general exponents, at A = 7 and M = 100. -/
private lemma many_count7 (S T : Finset ℕ) (k : ℕ)
    (hT : ∀ d ∈ T, ∃ x ∈ S, d ∣ x)
    (hD : ∀ x ∈ S, (Finset.card (Finset.filter (fun (d : ℕ) => d ∣ x) T)) ^ (9 : ℕ) < k ^ (8 : ℕ))
    (hN : k ^ (99 : ℕ) ≤ T.card ^ (100 : ℕ)) (hk : 0 < k) : k ≤ S.card ^ (10 : ℕ) := by
  classical
  have hsub : T ⊆ S.biUnion (fun x => Finset.filter (fun (d : ℕ) => d ∣ x) T) := by
    intro d hd
    obtain ⟨x, hx, hdx⟩ := hT d hd
    exact Finset.mem_biUnion.mpr ⟨x, hx, Finset.mem_filter.mpr ⟨hd, hdx⟩⟩
  set D := S.sup (fun x => Finset.card (Finset.filter (fun (d : ℕ) => d ∣ x) T)) with hDdef
  have h1 : T.card ≤ S.card * D := by
    refine (Finset.card_le_card hsub).trans (Finset.card_biUnion_le_card_mul _ _ _ ?_)
    intro x hx
    exact Finset.le_sup (f := fun x => Finset.card (Finset.filter (fun (d : ℕ) => d ∣ x) T)) hx
  have h2 : D ^ 9 ≤ k ^ 8 := by
    rcases S.eq_empty_or_nonempty with h | h
    · simp [hDdef, h]
    · obtain ⟨x, hx, he⟩ := Finset.exists_mem_eq_sup S h
        (fun x => Finset.card (Finset.filter (fun (d : ℕ) => d ∣ x) T))
      rw [hDdef, he]
      exact (hD x hx).le
  generalize S.card = s at *
  generalize T.card = t at *
  have h3 : k ^ 800 * k ^ 91 ≤ k ^ 800 * s ^ 900 :=
    calc k ^ 800 * k ^ 91 = (k ^ 99) ^ 9 := by rw [← pow_add, ← pow_mul]
      _ ≤ (t ^ 100) ^ 9 := Nat.pow_le_pow_left hN 9
      _ ≤ ((s * D) ^ 100) ^ 9 := Nat.pow_le_pow_left (Nat.pow_le_pow_left h1 100) 9
      _ = (D ^ 9) ^ 100 * s ^ 900 := by rw [mul_pow, mul_pow, ← pow_mul, ← pow_mul, ← pow_mul, mul_comm]
      _ ≤ (k ^ 8) ^ 100 * s ^ 900 := Nat.mul_le_mul_right _ (Nat.pow_le_pow_left h2 100)
      _ = k ^ 800 * s ^ 900 := by rw [← pow_mul]
  have h4 : k ^ 91 ≤ s ^ 900 := Nat.le_of_mul_le_mul_left h3 (Nat.pow_pos hk)
  by_contra hc
  have h5 : s ^ 10 + 1 ≤ k := by omega
  rcases Nat.eq_zero_or_pos s with h0 | h0
  · subst h0
    have : 0 < k ^ 91 := Nat.pow_pos hk
    rw [zero_pow (by norm_num)] at h4
    omega
  · have h6 : (s ^ 10) ^ 91 < k ^ 91 := Nat.pow_lt_pow_left (by omega) (by norm_num)
    have h7 : s ^ 900 ≤ (s ^ 10) ^ 91 := by
      rw [← pow_mul]
      exact Nat.pow_le_pow_right h0 (by norm_num)
    exact absurd (lt_of_le_of_lt (h4.trans h7) h6) (lt_irrefl _)

/-! T1 for every power of log k: run certificates (t1203-a, t1203-b), general window and density (agent K1), cosine polynomials and divisor count (agent K3). -/

/-! NOT FILED. Joined file for one fast check: A/t1_general.lean (agent A, t1203-a, copied verbatim at 12:19Z, sha256 be54ca1f6015…)
followed by B/t1_analytic.lean (agent B, t1203-b) and the joining theorem at the end. -/


/-!
Part (a) of B's T1 (`B/T1-proof.md` §1–§2 and the scaling (1.3)) for ANY list of steps, on the
fast check only. No node. Repeated steps are allowed.

Notation: `S : List ℕ` the steps `s_1, …, s_ρ`; `c = ∏ (1 - z^s)` with coefficients `coefL S i`;
`N = S.sum`, `ρ = S.length`, `β = S.prod`, norm `A = Σ_{i ≤ N} |coefL S i|`;
`gfun ρ x = ρ!/(x(x+1)⋯(x+ρ))`; `d` is bad for `(n, k)` when `k ≤ n % d`.

* `cert_general`: Lemma 2 for an ARBITRARY integer vector `c` on `0..N` with `Σ c i = 0` (the
  product structure is not used there).
* `lemma1_shifts`, `lemma1_bounds`: Lemma 1 and (2.1) without the integral: the iterated
  difference `Dl S (1/x) = Δ_{s_1} … Δ_{s_ρ} (1/x)` is a sum, over a list of `β` shifts `j` with
  `j + ρ ≤ N`, of `gfun ρ (x + j)`; hence `β·g_ρ(x+N-ρ) ≤ Dl S (1/x) ≤ β·g_ρ(x)`.
* `coefL_sum`, `Dl_seqD`: `Σ_i coefL S i / (x + i) = Dl S (1/x)`.
* `coefL_eq_coeff`: `coefL S i` is the coefficient of `X^i` in `∏ (1 - X^s) : Polynomial ℤ`.
* `no_bad_translate`: Proposition 3.
* `coefL_scale`, `norm_scale`: (1.3), scaling the steps by `g` keeps the norm.
* `no_bad_translate_scaled`: Proposition 3 after `z ↦ z^g`, with the unscaled norm.

Not here: any bound on the norm `A` of a concrete family (§3A (A4), §3B (B0)), the choice of
`ρ` and `g` (Lemma 5, 5′), Rankin's count (Lemma 7).
-/

/-- Lemma 2 of `B/T1-proof.md` for an arbitrary coefficient vector with sum zero: if `m + i` is
bad (`k ≤ n % (m + i)`) whenever `c i ≠ 0`, the integer `Σ c i · (⌊n/(m+i)⌋ + 1)` is within
`(Σ|c i|)/2 · (m+N-k)/(m+N)` of `n · Σ c i/(m+i)`. -/
private theorem cert_general (n k m N : ℕ) (c : ℕ → ℤ) (hk : 0 < k) (hm : k < m)
    (hsum : ∑ i ∈ Finset.range (N + 1), c i = 0)
    (hbad : ∀ i ≤ N, c i ≠ 0 → k ≤ n % (m + i)) :
    |((∑ i ∈ Finset.range (N + 1), c i * ((n / (m + i) + 1 : ℕ) : ℤ) : ℤ) : ℚ)
        - (n : ℚ) * ∑ i ∈ Finset.range (N + 1), (c i : ℚ) / ((m : ℚ) + (i : ℚ))|
      ≤ (∑ i ∈ Finset.range (N + 1), |(c i : ℚ)|) / 2
          * (((m + N - k : ℕ) : ℚ) / ((m + N : ℕ) : ℚ)) := by
  have hmq : (0 : ℚ) < (m : ℚ) := by exact_mod_cast (by omega : 0 < m)
  have hkq : (0 : ℚ) < (k : ℚ) := by exact_mod_cast hk
  have hkm : (k : ℚ) < (m : ℚ) := by exact_mod_cast hm
  obtain ⟨e, he_def⟩ : ∃ e : ℕ → ℚ, ∀ i : ℕ,
      e i = ((n / (m + i) + 1 : ℕ) : ℚ) - (n : ℚ) / ((m : ℚ) + (i : ℚ)) := ⟨_, fun _ => rfl⟩
  obtain ⟨ε, hε⟩ : ∃ ε : ℚ, ε = 1 - (k : ℚ) / ((m : ℚ) + (N : ℚ)) := ⟨_, rfl⟩
  have he : ∀ i : ℕ, i ≤ N → c i ≠ 0 → 0 < e i ∧ e i ≤ ε := by
    intro i hi hci
    have hd : 0 < m + i := by omega
    have hdq : (0 : ℚ) < (m : ℚ) + (i : ℚ) := by positivity
    have hρ1 : ((k : ℕ) : ℚ) ≤ ((n % (m + i) : ℕ) : ℚ) := by exact_mod_cast hbad i hi hci
    have hρ2 : ((n % (m + i) : ℕ) : ℚ) < (m : ℚ) + (i : ℚ) := by
      exact_mod_cast Nat.mod_lt n hd
    have hn : (n : ℚ) = ((m : ℚ) + (i : ℚ)) * ((n / (m + i) : ℕ) : ℚ) + ((n % (m + i) : ℕ) : ℚ) := by
      exact_mod_cast (Nat.div_add_mod n (m + i)).symm
    have hnd : (n : ℚ) / ((m : ℚ) + (i : ℚ))
        = ((n / (m + i) : ℕ) : ℚ) + ((n % (m + i) : ℕ) : ℚ) / ((m : ℚ) + (i : ℚ)) := by
      rw [div_eq_iff hdq.ne', add_mul, div_mul_cancel₀ _ hdq.ne']
      linarith
    have hee : e i = 1 - ((n % (m + i) : ℕ) : ℚ) / ((m : ℚ) + (i : ℚ)) := by
      rw [he_def i, hnd]
      push_cast
      ring
    have h1 : ((n % (m + i) : ℕ) : ℚ) / ((m : ℚ) + (i : ℚ)) < 1 := (div_lt_one hdq).mpr hρ2
    have h2 : (k : ℚ) / ((m : ℚ) + (i : ℚ)) ≤ ((n % (m + i) : ℕ) : ℚ) / ((m : ℚ) + (i : ℚ)) :=
      div_le_div_of_nonneg_right hρ1 hdq.le
    have hir : (m : ℚ) + (i : ℚ) ≤ (m : ℚ) + (N : ℚ) := by
      have : (i : ℚ) ≤ (N : ℚ) := by exact_mod_cast hi
      linarith
    have h3 : (k : ℚ) / ((m : ℚ) + (N : ℚ)) ≤ (k : ℚ) / ((m : ℚ) + (i : ℚ)) :=
      div_le_div_of_nonneg_left hkq.le hdq hir
    rw [hee, hε]
    constructor <;> linarith
  have hsumq : ∑ i ∈ Finset.range (N + 1), (c i : ℚ) = 0 := by exact_mod_cast hsum
  have hkey : ((∑ i ∈ Finset.range (N + 1), c i * ((n / (m + i) + 1 : ℕ) : ℤ) : ℤ) : ℚ)
        - (n : ℚ) * ∑ i ∈ Finset.range (N + 1), (c i : ℚ) / ((m : ℚ) + (i : ℚ))
      = ∑ i ∈ Finset.range (N + 1), (c i : ℚ) * (e i - ε / 2) := by
    have h1 : ∀ i ∈ Finset.range (N + 1), (c i : ℚ) * (e i - ε / 2)
        = (c i : ℚ) * ((n / (m + i) + 1 : ℕ) : ℚ)
          - (n : ℚ) * ((c i : ℚ) / ((m : ℚ) + (i : ℚ)))
          - ε / 2 * (c i : ℚ) := by
      intro i _
      rw [he_def i]
      ring
    have hTcast : ((∑ i ∈ Finset.range (N + 1), c i * ((n / (m + i) + 1 : ℕ) : ℤ) : ℤ) : ℚ)
        = ∑ i ∈ Finset.range (N + 1), (c i : ℚ) * ((n / (m + i) + 1 : ℕ) : ℚ) := by
      rw [Int.cast_sum]
      apply Finset.sum_congr rfl
      intro i _
      rw [Int.cast_mul, Int.cast_natCast]
    rw [Finset.sum_congr rfl h1, Finset.sum_sub_distrib, Finset.sum_sub_distrib,
      ← Finset.mul_sum, ← Finset.mul_sum, hsumq, hTcast]
    ring
  rw [hkey]
  have hterm : ∀ i ∈ Finset.range (N + 1),
      |(c i : ℚ) * (e i - ε / 2)| ≤ |(c i : ℚ)| * (ε / 2) := by
    intro i hi
    have hiN : i ≤ N := Nat.lt_succ_iff.mp (Finset.mem_range.mp hi)
    by_cases hci : c i = 0
    · simp [hci]
    · obtain ⟨h1, h2⟩ := he i hiN hci
      have h3 : |e i - ε / 2| ≤ ε / 2 := abs_le.mpr ⟨by linarith, by linarith⟩
      rw [abs_mul]
      exact mul_le_mul_of_nonneg_left h3 (abs_nonneg _)
  have hrhs : (∑ i ∈ Finset.range (N + 1), |(c i : ℚ)|) / 2
        * (((m + N - k : ℕ) : ℚ) / ((m + N : ℕ) : ℚ))
      = ∑ i ∈ Finset.range (N + 1), |(c i : ℚ)| * (ε / 2) := by
    have hmNq : (0 : ℚ) < (m : ℚ) + (N : ℚ) := by positivity
    rw [← Finset.sum_mul, hε, Nat.cast_sub (by omega)]
    push_cast
    field_simp
  rw [hrhs]
  exact (Finset.abs_sum_le_sum_abs _ _).trans (Finset.sum_le_sum hterm)

/-- `gfun ρ x = ρ!/(x(x+1)⋯(x+ρ))`. -/
private def gfun (ρ : ℕ) (x : ℚ) : ℚ := (ρ.factorial : ℚ) / ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ))

/-- One difference raises the order: `g_ρ(x) - g_ρ(x+1) = g_{ρ+1}(x)`. -/
private theorem gfun_step (ρ : ℕ) (x : ℚ) (hx : 0 < x) :
    gfun ρ x - gfun ρ (x + 1) = gfun (ρ + 1) x := by
  unfold gfun
  have hM : (0 : ℚ) < ∏ i ∈ Finset.range ρ, (x + 1 + (i : ℚ)) :=
    Finset.prod_pos (fun i _ => by positivity)
  have hP1 : ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ))
      = (∏ i ∈ Finset.range ρ, (x + 1 + (i : ℚ))) * x := by
    rw [Finset.prod_range_succ']
    congr 1
    · apply Finset.prod_congr rfl
      intro i _
      push_cast
      ring
    · simp
  have hP2 : ∏ i ∈ Finset.range (ρ + 1), (x + 1 + (i : ℚ))
      = (∏ i ∈ Finset.range ρ, (x + 1 + (i : ℚ))) * (x + 1 + (ρ : ℚ)) :=
    Finset.prod_range_succ _ _
  have hP3 : ∏ i ∈ Finset.range (ρ + 1 + 1), (x + (i : ℚ))
      = (∏ i ∈ Finset.range ρ, (x + 1 + (i : ℚ))) * x * (x + 1 + (ρ : ℚ)) := by
    rw [Finset.prod_range_succ, hP1]
    push_cast
    ring
  rw [hP1, hP2, hP3, Nat.factorial_succ]
  have hxr : (0 : ℚ) < x + 1 + (ρ : ℚ) := by positivity
  push_cast
  field_simp
  ring

/-- Telescoping: `g_ρ(x) - g_ρ(x+s) = Σ_{i<s} g_{ρ+1}(x+i)`. -/
private theorem gfun_tele (ρ s : ℕ) (x : ℚ) (hx : 0 < x) :
    gfun ρ x - gfun ρ (x + (s : ℚ)) = ∑ i ∈ Finset.range s, gfun (ρ + 1) (x + (i : ℚ)) := by
  induction s with
  | zero => simp
  | succ s ih =>
    have hxs : 0 < x + (s : ℚ) := by positivity
    have e1 : x + ((s + 1 : ℕ) : ℚ) = x + (s : ℚ) + 1 := by
      push_cast
      ring
    rw [Finset.sum_range_succ, ← ih, ← gfun_step ρ (x + (s : ℚ)) hxs, e1]
    ring

private theorem gfun_pos (ρ : ℕ) (x : ℚ) (hx : 0 < x) : 0 < gfun ρ x := by
  unfold gfun
  have hP : (0 : ℚ) < ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) :=
    Finset.prod_pos (fun i _ => by positivity)
  have hf : (0 : ℚ) < (ρ.factorial : ℚ) := by exact_mod_cast Nat.factorial_pos ρ
  exact div_pos hf hP

private theorem gfun_anti (ρ : ℕ) (x y : ℚ) (hx : 0 < x) (hxy : x ≤ y) : gfun ρ y ≤ gfun ρ x := by
  unfold gfun
  have hP : (0 : ℚ) < ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) :=
    Finset.prod_pos (fun i _ => by positivity)
  have hle : ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) ≤ ∏ i ∈ Finset.range (ρ + 1), (y + (i : ℚ)) :=
    Finset.prod_le_prod (fun i _ => by positivity) (fun i _ => by linarith)
  exact div_le_div_of_nonneg_left (Nat.cast_nonneg _) hP hle

/-- The iterated difference `Δ_{s_1} … Δ_{s_ρ} f`, where `Δ_s f (x) = f x - f (x + s)`. -/
private def Dl : List ℕ → (ℚ → ℚ) → ℚ → ℚ
  | [], f => f
  | s :: S, f => fun x => Dl S f x - Dl S f (x + (s : ℚ))

private theorem sum_flatMap_aux (L : List ℕ) (F : ℕ → List ℕ) (h : ℕ → ℚ) :
    ((L.flatMap F).map h).sum = (L.map (fun j => ((F j).map h).sum)).sum := by
  induction L with
  | nil => simp
  | cons a L ih => simp [List.flatMap_cons, ih]

private theorem sum_map_sub_aux (L : List ℕ) (a b : ℕ → ℚ) :
    (L.map a).sum - (L.map b).sum = (L.map (fun j => a j - b j)).sum := by
  induction L with
  | nil => simp
  | cons x L ih =>
    simp only [List.map_cons, List.sum_cons]
    rw [← ih]
    ring

private theorem sum_range_list (s : ℕ) (h : ℕ → ℚ) :
    ((List.range s).map h).sum = ∑ i ∈ Finset.range s, h i := by
  induction s with
  | zero => simp
  | succ s ih =>
    rw [List.range_succ, List.map_append, List.sum_append, ih, Finset.sum_range_succ]
    simp

private theorem list_sum_bounds (L : List ℕ) (h : ℕ → ℚ) (a b : ℚ)
    (hb : ∀ j ∈ L, a ≤ h j ∧ h j ≤ b) :
    (L.length : ℚ) * a ≤ (L.map h).sum ∧ (L.map h).sum ≤ (L.length : ℚ) * b := by
  induction L with
  | nil => simp
  | cons j L ih =>
    have h1 := hb j (by simp)
    have h2 := ih (fun i hi => hb i (by simp [hi]))
    simp only [List.map_cons, List.sum_cons, List.length_cons]
    push_cast
    constructor <;> nlinarith [h1.1, h1.2, h2.1, h2.2]

/-- Lemma 1 of `B/T1-proof.md` without the integral: `Δ_{s_1} … Δ_{s_ρ} (1/x)` is a sum of
`∏ s_t` shifted copies of `g_ρ`, every shift `j` having `j + ρ ≤ Σ s_t`. -/
private theorem lemma1_shifts (S : List ℕ) :
    ∃ L : List ℕ, L.length = S.prod ∧ (∀ j ∈ L, j + S.length ≤ S.sum) ∧
      ∀ x : ℚ, 0 < x →
        Dl S (fun y => 1 / y) x = (L.map (fun j : ℕ => gfun S.length (x + (j : ℚ)))).sum := by
  induction S with
  | nil =>
    refine ⟨[0], by simp, by simp, ?_⟩
    intro x hx
    simp [Dl, gfun]
  | cons s S ih =>
    obtain ⟨L, hlen, hbd, hval⟩ := ih
    refine ⟨L.flatMap (fun j => (List.range s).map (fun i => j + i)), ?_, ?_, ?_⟩
    · rw [List.length_flatMap]
      simp [hlen, mul_comm]
    · intro j hj
      rw [List.mem_flatMap] at hj
      obtain ⟨j0, hj0, hj1⟩ := hj
      rw [List.mem_map] at hj1
      obtain ⟨i, hi, rfl⟩ := hj1
      have hi' : i < s := List.mem_range.mp hi
      have := hbd j0 hj0
      simp only [List.length_cons, List.sum_cons]
      omega
    · intro x hx
      have hxs : 0 < x + (s : ℚ) := by positivity
      show Dl S (fun y => 1 / y) x - Dl S (fun y => 1 / y) (x + (s : ℚ)) = _
      rw [hval x hx, hval (x + (s : ℚ)) hxs, sum_map_sub_aux, sum_flatMap_aux]
      congr 1
      apply List.map_congr_left
      intro j _
      rw [List.map_map, sum_range_list]
      have hxj : 0 < x + (j : ℚ) := by positivity
      have h := gfun_tele S.length s (x + (j : ℚ)) hxj
      have e1 : x + (s : ℚ) + (j : ℚ) = x + (j : ℚ) + (s : ℚ) := by ring
      rw [e1, h]
      apply Finset.sum_congr rfl
      intro i _
      simp only [Function.comp_apply, List.length_cons]
      congr 1
      push_cast
      ring

/-- (2.1) of `B/T1-proof.md`: with `ρ` the number of steps, `β` their product and `N` their sum,
`β · g_ρ(x + N - ρ) ≤ Δ_{s_1} … Δ_{s_ρ} (1/x) ≤ β · g_ρ(x)` for `x > 0`. -/
private theorem lemma1_bounds (S : List ℕ) (x : ℚ) (hx : 0 < x) :
    (S.prod : ℚ) * gfun S.length (x + ((S.sum - S.length : ℕ) : ℚ)) ≤ Dl S (fun y => 1 / y) x ∧
      Dl S (fun y => 1 / y) x ≤ (S.prod : ℚ) * gfun S.length x := by
  obtain ⟨L, hlen, hbd, hval⟩ := lemma1_shifts S
  rw [hval x hx, ← hlen]
  apply list_sum_bounds
  intro j hj
  have hj' := hbd j hj
  have hjq : (j : ℚ) ≤ ((S.sum - S.length : ℕ) : ℚ) := by exact_mod_cast (by omega : j ≤ S.sum - S.length)
  have hj0 : (0 : ℚ) ≤ (j : ℚ) := Nat.cast_nonneg j
  constructor
  · exact gfun_anti S.length (x + (j : ℚ)) (x + ((S.sum - S.length : ℕ) : ℚ)) (by positivity) (by linarith)
  · exact gfun_anti S.length x (x + (j : ℚ)) hx (by linarith)

/-- Coefficients of `∏ (1 - z^s)` over the list of steps: `coefL S i` is the coefficient of
`z^i`. -/
private def coefL : List ℕ → ℕ → ℤ
  | [], i => if i = 0 then 1 else 0
  | s :: S, i => coefL S i - (if s ≤ i then coefL S (i - s) else 0)

/-- The iterated difference on sequences: `seqD S f = Σ_i (coefL S i) · f i`. -/
private def seqD : List ℕ → (ℕ → ℚ) → ℚ
  | [], f => f 0
  | s :: S, f => seqD S f - seqD S (fun i => f (i + s))

private theorem coefL_sum (S : List ℕ) : ∀ (f : ℕ → ℚ) (N : ℕ), S.sum ≤ N →
    ∑ i ∈ Finset.range (N + 1), (coefL S i : ℚ) * f i = seqD S f := by
  induction S with
  | nil =>
    intro f N _
    rw [Finset.sum_eq_single 0]
    · simp [coefL, seqD]
    · intro i _ hi
      simp [coefL, hi]
    · intro h
      exact absurd (Finset.mem_range.mpr (Nat.succ_pos N)) h
  | cons s S ih =>
    intro f N hN
    rw [List.sum_cons] at hN
    have h1 : ∀ i ∈ Finset.range (N + 1), (coefL (s :: S) i : ℚ) * f i
        = (coefL S i : ℚ) * f i - (if s ≤ i then (coefL S (i - s) : ℚ) * f i else 0) := by
      intro i _
      rw [show coefL (s :: S) i = coefL S i - (if s ≤ i then coefL S (i - s) else 0) from rfl]
      split_ifs <;> push_cast <;> ring
    rw [Finset.sum_congr rfl h1, Finset.sum_sub_distrib, ih f N (by omega)]
    have h2 : ∑ i ∈ Finset.range (N + 1), (if s ≤ i then (coefL S (i - s) : ℚ) * f i else 0)
        = ∑ j ∈ Finset.range (N - s + 1), (coefL S j : ℚ) * f (j + s) := by
      have hNs : N + 1 = s + (N - s + 1) := by omega
      rw [hNs, Finset.sum_range_add]
      have hz : ∑ i ∈ Finset.range s, (if s ≤ i then (coefL S (i - s) : ℚ) * f i else 0) = 0 := by
        apply Finset.sum_eq_zero
        intro i hi
        have hlt : ¬ s ≤ i := by
          have := Finset.mem_range.mp hi
          omega
        rw [if_neg hlt]
      rw [hz, zero_add]
      apply Finset.sum_congr rfl
      intro j _
      have hle : s ≤ s + j := Nat.le_add_right s j
      rw [if_pos hle, Nat.add_sub_cancel_left, add_comm s j]
    rw [h2, ih (fun i => f (i + s)) (N - s) (by omega)]
    rfl

private theorem Dl_seqD (S : List ℕ) : ∀ (g : ℚ → ℚ) (x : ℚ),
    Dl S g x = seqD S (fun i => g (x + (i : ℚ))) := by
  induction S with
  | nil =>
    intro g x
    simp [Dl, seqD]
  | cons s S ih =>
    intro g x
    show Dl S g x - Dl S g (x + (s : ℚ))
      = seqD S (fun i => g (x + (i : ℚ))) - seqD S (fun i => g (x + ((i + s : ℕ) : ℚ)))
    rw [ih g x, ih g (x + (s : ℚ))]
    have e : (fun i : ℕ => g (x + (s : ℚ) + (i : ℚ))) = (fun i : ℕ => g (x + ((i + s : ℕ) : ℚ))) := by
      funext i
      congr 1
      push_cast
      ring
    rw [e]

private theorem seqD_const (s : ℕ) (S : List ℕ) (a : ℚ) : seqD (s :: S) (fun _ => a) = 0 := by
  show seqD S (fun _ => a) - seqD S (fun _ => a) = 0
  exact sub_self _

/-- Proposition 3 of `B/T1-proof.md` for any nonempty list of steps `S` (repeats allowed), with
`N = S.sum`, `ρ = S.length`, `β = S.prod` and the norm `A = Σ |coefL S i|`: if
`2·A·(m+N-k) < m+N`, `1/4 ≤ n·β·g_ρ(m+N-ρ)` and `n·β·g_ρ(m) ≤ 3/4`, then some `m + i` with
`coefL S i ≠ 0`, `i ≤ N`, is not bad. -/
private theorem no_bad_translate (n k m : ℕ) (S : List ℕ) (hS : S ≠ []) (hk : 0 < k) (hm : k < m)
    (hsmall : 2 * (∑ i ∈ Finset.range (S.sum + 1), |(coefL S i : ℚ)|) * ((m + S.sum - k : ℕ) : ℚ)
      < ((m + S.sum : ℕ) : ℚ))
    (hlo : 1 / 4 ≤ (n : ℚ) * ((S.prod : ℚ) * gfun S.length ((m : ℚ) + ((S.sum - S.length : ℕ) : ℚ))))
    (hhi : (n : ℚ) * ((S.prod : ℚ) * gfun S.length (m : ℚ)) ≤ 3 / 4) :
    ∃ i ≤ S.sum, coefL S i ≠ 0 ∧ n % (m + i) < k := by
  by_contra hcon
  have hbad : ∀ i ≤ S.sum, coefL S i ≠ 0 → k ≤ n % (m + i) :=
    fun i hi hc => not_lt.mp (fun h => hcon ⟨i, hi, hc, h⟩)
  have hmq : (0 : ℚ) < (m : ℚ) := by exact_mod_cast (by omega : 0 < m)
  have hsum : ∑ i ∈ Finset.range (S.sum + 1), coefL S i = 0 := by
    obtain ⟨s, S', rfl⟩ := List.exists_cons_of_ne_nil hS
    have h := coefL_sum (s :: S') (fun _ => (1 : ℚ)) (s :: S').sum le_rfl
    rw [seqD_const] at h
    have h' : ((∑ i ∈ Finset.range ((s :: S').sum + 1), coefL (s :: S') i : ℤ) : ℚ) = 0 := by
      rw [Int.cast_sum]
      simpa using h
    exact_mod_cast h'
  have hC := cert_general n k m S.sum (coefL S) hk hm hsum hbad
  have hΦ : ∑ i ∈ Finset.range (S.sum + 1), (coefL S i : ℚ) / ((m : ℚ) + (i : ℚ))
      = Dl S (fun y => 1 / y) (m : ℚ) := by
    rw [Dl_seqD S (fun y => 1 / y) (m : ℚ),
      ← coefL_sum S (fun i => 1 / ((m : ℚ) + (i : ℚ))) S.sum le_rfl]
    apply Finset.sum_congr rfl
    intro i _
    ring
  rw [hΦ] at hC
  obtain ⟨hb1, hb2⟩ := lemma1_bounds S (m : ℚ) hmq
  have hn : (0 : ℚ) ≤ (n : ℚ) := Nat.cast_nonneg n
  have hV1 : 1 / 4 ≤ (n : ℚ) * Dl S (fun y => 1 / y) (m : ℚ) :=
    hlo.trans (mul_le_mul_of_nonneg_left hb1 hn)
  have hV2 : (n : ℚ) * Dl S (fun y => 1 / y) (m : ℚ) ≤ 3 / 4 :=
    (mul_le_mul_of_nonneg_left hb2 hn).trans hhi
  have hmNq : (0 : ℚ) < ((m + S.sum : ℕ) : ℚ) := by exact_mod_cast (by omega : 0 < m + S.sum)
  have hE : (∑ i ∈ Finset.range (S.sum + 1), |(coefL S i : ℚ)|) / 2
      * (((m + S.sum - k : ℕ) : ℚ) / ((m + S.sum : ℕ) : ℚ)) < 1 / 4 := by
    rw [div_mul_div_comm, div_lt_iff₀ (by linarith : (0 : ℚ) < 2 * ((m + S.sum : ℕ) : ℚ))]
    linarith
  have habs := lt_of_le_of_lt hC hE
  rw [abs_lt] at habs
  obtain ⟨T, hT⟩ : ∃ T : ℤ, T = ∑ i ∈ Finset.range (S.sum + 1), coefL S i * ((n / (m + i) + 1 : ℕ) : ℤ) :=
    ⟨_, rfl⟩
  rw [← hT] at habs
  rcases le_or_gt T 0 with hT0 | hT1
  · have : (T : ℚ) ≤ 0 := by exact_mod_cast hT0
    linarith [habs.1]
  · have : (1 : ℚ) ≤ (T : ℚ) := by exact_mod_cast hT1
    linarith [habs.2]

/-- `coefL S` is the coefficient sequence of the polynomial `∏_{s ∈ S} (1 - X^s)` over `ℤ`: the
interface to any bound on the norm `Σ |coefL S i|` proved with `Polynomial`. -/
private theorem coefL_eq_coeff (S : List ℕ) : ∀ i : ℕ,
    coefL S i = ((S.map (fun s => (1 - Polynomial.X ^ s : Polynomial ℤ))).prod).coeff i := by
  induction S with
  | nil =>
    intro i
    simp [coefL, Polynomial.coeff_one]
  | cons s S ih =>
    intro i
    rw [show coefL (s :: S) i = coefL S i - (if s ≤ i then coefL S (i - s) else 0) from rfl]
    rw [List.map_cons, List.prod_cons, sub_mul, one_mul, Polynomial.coeff_sub,
      Polynomial.coeff_X_pow_mul', ih i]
    split_ifs with h
    · rw [ih (i - s)]
    · rfl

/-- (1.3) of `B/T1-proof.md`, coefficients: scaling every step by `g` spreads the coefficients
to the multiples of `g`. -/
private theorem coefL_scale (g : ℕ) (hg : 0 < g) (S : List ℕ) : ∀ i : ℕ,
    coefL (S.map (fun s => g * s)) i = if g ∣ i then coefL S (i / g) else 0 := by
  induction S with
  | nil =>
    intro i
    simp only [List.map_nil, coefL]
    by_cases hi : i = 0
    · subst hi
      simp
    · by_cases hd : g ∣ i
      · have hq : i / g ≠ 0 := by
          obtain ⟨t, rfl⟩ := hd
          have ht : t ≠ 0 := by
            rintro rfl
            simp at hi
          rw [Nat.mul_div_cancel_left _ hg]
          exact ht
        simp [hi, hd, hq]
      · simp [hi, hd]
  | cons s S ih =>
    intro i
    rw [List.map_cons,
      show coefL (g * s :: S.map (fun s => g * s)) i
        = coefL (S.map (fun s => g * s)) i
          - (if g * s ≤ i then coefL (S.map (fun s => g * s)) (i - g * s) else 0) from rfl,
      ih i]
    by_cases hd : g ∣ i
    · obtain ⟨t, rfl⟩ := hd
      rw [if_pos (Dvd.intro t rfl), if_pos (Dvd.intro t rfl), Nat.mul_div_cancel_left _ hg,
        show coefL (s :: S) t = coefL S t - (if s ≤ t then coefL S (t - s) else 0) from rfl]
      by_cases hst : s ≤ t
      · have h1 : g * s ≤ g * t := Nat.mul_le_mul_left g hst
        have h2 : g * t - g * s = g * (t - s) := (Nat.mul_sub g t s).symm
        rw [if_pos h1, if_pos hst, h2, ih (g * (t - s)), if_pos (Dvd.intro _ rfl),
          Nat.mul_div_cancel_left _ hg]
      · have h1 : ¬ g * s ≤ g * t := fun h => hst (Nat.le_of_mul_le_mul_left h hg)
        rw [if_neg h1, if_neg hst]
    · rw [if_neg hd, if_neg hd]
      by_cases h1 : g * s ≤ i
      · have h3 : ¬ g ∣ i - g * s := by
          intro h
          apply hd
          have e : i = (i - g * s) + g * s := (Nat.sub_add_cancel h1).symm
          rw [e]
          exact dvd_add h (Dvd.intro s rfl)
        rw [if_pos h1, ih (i - g * s), if_neg h3]
        simp
      · rw [if_neg h1]
        simp

/-- (1.3) of `B/T1-proof.md`, norm: scaling every step by `g ≥ 1` does not change `Σ |c_i|`. -/
private theorem norm_scale (g : ℕ) (hg : 0 < g) (S : List ℕ) (N : ℕ) :
    ∑ i ∈ Finset.range (g * N + 1), |(coefL (S.map (fun s => g * s)) i : ℚ)|
      = ∑ j ∈ Finset.range (N + 1), |(coefL S j : ℚ)| := by
  induction N with
  | zero => simp [coefL_scale g hg S]
  | succ N ih =>
    have e : g * (N + 1) + 1 = (g * N + 1) + g := by ring
    rw [e, Finset.sum_range_add, ih, Finset.sum_range_succ (fun j => |(coefL S j : ℚ)|) (N + 1)]
    congr 1
    rw [Finset.sum_eq_single (g - 1)]
    · have e2 : g * N + 1 + (g - 1) = g * (N + 1) := by
        rw [Nat.mul_succ]
        omega
      rw [e2, coefL_scale g hg S, if_pos (Dvd.intro _ rfl), Nat.mul_div_cancel_left _ hg]
    · intro t ht hne
      have ht' := Finset.mem_range.mp ht
      have hnd : ¬ g ∣ g * N + 1 + t := by
        intro h
        rw [Nat.add_assoc] at h
        have h' : g ∣ 1 + t := (Nat.dvd_add_right (Dvd.intro N rfl : g ∣ g * N)).mp h
        have := Nat.le_of_dvd (by omega) h'
        omega
      rw [coefL_scale g hg S, if_neg hnd]
      simp
    · intro h
      exact absurd (Finset.mem_range.mpr (by omega)) h

private theorem sum_map_scale (g : ℕ) (S : List ℕ) : (S.map (fun s => g * s)).sum = g * S.sum := by
  induction S with
  | nil => simp
  | cons s S ih => rw [List.map_cons, List.sum_cons, ih, List.sum_cons, Nat.mul_add]

private theorem prod_map_scale (g : ℕ) (S : List ℕ) :
    (S.map (fun s => g * s)).prod = g ^ S.length * S.prod := by
  induction S with
  | nil => simp
  | cons s S ih =>
    rw [List.map_cons, List.prod_cons, ih, List.length_cons, List.prod_cons, pow_succ]
    ring

/-- Proposition 3 after the substitution `z ↦ z^g` (the tuning of `B/T1-proof.md` §3): the
moduli are `m + g·j`, the degree is `g·N`, `β` becomes `g^ρ·β`, and the norm is that of the
UNSCALED product. -/
private theorem no_bad_translate_scaled (n k m g : ℕ) (S : List ℕ) (hS : S ≠ []) (hg : 0 < g)
    (hk : 0 < k) (hm : k < m)
    (hsmall : 2 * (∑ j ∈ Finset.range (S.sum + 1), |(coefL S j : ℚ)|)
        * ((m + g * S.sum - k : ℕ) : ℚ)
      < ((m + g * S.sum : ℕ) : ℚ))
    (hlo : 1 / 4 ≤ (n : ℚ) * (((g ^ S.length * S.prod : ℕ) : ℚ)
      * gfun S.length ((m : ℚ) + ((g * S.sum - S.length : ℕ) : ℚ))))
    (hhi : (n : ℚ) * (((g ^ S.length * S.prod : ℕ) : ℚ) * gfun S.length (m : ℚ)) ≤ 3 / 4) :
    ∃ j ≤ S.sum, coefL S j ≠ 0 ∧ n % (m + g * j) < k := by
  have hS' : S.map (fun s => g * s) ≠ [] := by
    intro h
    exact hS (List.map_eq_nil_iff.mp h)
  have h := no_bad_translate n k m (S.map (fun s => g * s)) hS' hk hm
  rw [sum_map_scale, prod_map_scale, List.length_map, norm_scale g hg S S.sum] at h
  obtain ⟨i, hi, hci, hbad⟩ := h hsmall hlo hhi
  rw [coefL_scale g hg S i] at hci
  by_cases hd : g ∣ i
  · rw [if_pos hd] at hci
    obtain ⟨j, rfl⟩ := hd
    rw [Nat.mul_div_cancel_left _ hg] at hci
    exact ⟨j, Nat.le_of_mul_le_mul_left hi hg, hci, hbad⟩
  · rw [if_neg hd] at hci
    exact absurd rfl hci

/-! ### From here: B/t1_analytic.lean -/

/-- T1, (A2)/(B0) of B/T1-proof.md in its general form: for any finite set `S` of integers the
cosine sum over ALL ordered differences is nonnegative, because it is `|Σ e^{isθ}|²`. (The diagonal
contributes `|S|`, and each positive difference twice: `Σ_{t} cos(s_t θ) ≥ -|S|/2`.) -/
private theorem t1_cos_differences_nonneg (S : Finset ℕ) (θ : ℝ) :
    0 ≤ ∑ s ∈ S, ∑ t ∈ S, Real.cos (((s : ℝ) - (t : ℝ)) * θ) := by
  have h : ∀ s t : ℕ, Real.cos (((s : ℝ) - (t : ℝ)) * θ) =
      Real.cos ((s : ℝ) * θ) * Real.cos ((t : ℝ) * θ) + Real.sin ((s : ℝ) * θ) * Real.sin ((t : ℝ) * θ) := by
    intro s t
    rw [sub_mul, Real.cos_sub]
  simp_rw [h, Finset.sum_add_distrib, ← Finset.sum_mul_sum]
  nlinarith [mul_self_nonneg (∑ s ∈ S, Real.cos ((s : ℝ) * θ)), mul_self_nonneg (∑ s ∈ S, Real.sin ((s : ℝ) * θ))]

/-- T1, (A3): on the unit circle, shrinking the radius to `r` loses at most the factor `r`. -/
private theorem t1_shrink (w : ℂ) (hw : ‖w‖ = 1) (r : ℝ) (hr0 : 0 < r) (hr1 : r ≤ 1) :
    r * ‖1 - w‖ ≤ ‖1 - (r : ℂ) * w‖ := by
  have hw2 : w.re * w.re + w.im * w.im = 1 := by
    have h := Complex.sq_norm w
    rw [hw, Complex.normSq_apply] at h
    linarith
  have ha : w.re ≤ 1 := by nlinarith [mul_self_nonneg w.im, mul_self_nonneg (w.re - 1)]
  have h1 : (r * ‖1 - w‖) ^ 2 ≤ ‖1 - (r : ℂ) * w‖ ^ 2 := by
    rw [mul_pow, Complex.sq_norm, Complex.sq_norm, Complex.normSq_apply, Complex.normSq_apply]
    simp only [Complex.sub_re, Complex.sub_im, Complex.one_re, Complex.one_im, Complex.mul_re,
      Complex.mul_im, Complex.ofReal_re, Complex.ofReal_im]
    have e : r ^ 2 * (w.im * w.im) = r ^ 2 * (1 - w.re * w.re) := by
      rw [show w.im * w.im = 1 - w.re * w.re by linarith]
    have hp : 0 ≤ (1 - r) * (1 + r - 2 * r * w.re) := by
      apply mul_nonneg (by linarith)
      nlinarith
    nlinarith [e, hp]
  exact (pow_le_pow_iff_left₀ (by positivity) (norm_nonneg _) two_ne_zero).mp h1

/-- T1, (A4)/(B0), the analytic core: if a family of exponents `s t` (repeats allowed) has
`Σ_t Re(w^(s t)) ≥ -M` at every point `w` of the unit circle (that is, `Σ_t cos(s_t θ) ≥ -M`), then
for every `z` on the circle and every `0 < r < 1`,
`r^ρ · |∏_t (1 - z^(s t))| ≤ (1/(1-r))^M`, `ρ` the number of factors.
(With `r = 1 - 1/ρ` this is `sup |∏| ≤ r^(-ρ) · ρ^M`.) -/
private theorem t1_product_bound {ι : Type} (T : Finset ι) (s : ι → ℕ) (M : ℝ)
    (hM : ∀ w : ℂ, ‖w‖ = 1 → -M ≤ ∑ t ∈ T, (w ^ s t).re)
    (z : ℂ) (hz : ‖z‖ = 1) (r : ℝ) (hr0 : 0 < r) (hr1 : r < 1) :
    r ^ T.card * ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ Real.exp (M * (-Real.log (1 - r))) := by
  have hzs : ∀ t, ‖z ^ s t‖ = 1 := by intro t; rw [norm_pow, hz, one_pow]
  have hu : ∀ t, ‖(r : ℂ) * z ^ s t‖ < 1 := by
    intro t
    rw [norm_mul, hzs t, mul_one, Complex.norm_real, Real.norm_eq_abs, abs_of_pos hr0]
    exact hr1
  -- step 1: shrink every factor
  have h1 : r ^ T.card * ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ ∏ t ∈ T, ‖1 - (r : ℂ) * z ^ s t‖ := by
    rw [norm_prod, ← Finset.prod_const, ← Finset.prod_mul_distrib]
    apply Finset.prod_le_prod
    · intro t _; exact mul_nonneg hr0.le (norm_nonneg _)
    · intro t _; exact t1_shrink (z ^ s t) (hzs t) r hr0 hr1.le
  -- step 2: each factor as an exponential
  have hne : ∀ t, 1 - (r : ℂ) * z ^ s t ≠ 0 := by
    intro t h
    have h' : (r : ℂ) * z ^ s t = 1 := (sub_eq_zero.mp h).symm
    have := hu t
    rw [h', norm_one] at this
    exact lt_irrefl _ this
  have h2 : ∏ t ∈ T, ‖1 - (r : ℂ) * z ^ s t‖ =
      Real.exp (∑ t ∈ T, (Complex.log (1 - (r : ℂ) * z ^ s t)).re) := by
    rw [Real.exp_sum]
    apply Finset.prod_congr rfl
    intro t _
    rw [Complex.log_re, Real.exp_log (norm_pos_iff.mpr (hne t))]
  -- step 3: the series
  have h3 : HasSum (fun m : ℕ => ∑ t ∈ T, (((r : ℂ) * z ^ s t) ^ m / (m : ℂ)).re)
      (∑ t ∈ T, (-Complex.log (1 - (r : ℂ) * z ^ s t)).re) := by
    apply hasSum_sum
    intro t _
    exact Complex.hasSum_re (Complex.hasSum_taylorSeries_neg_log (hu t))
  have hr' : ‖(r : ℂ)‖ < 1 := by
    rw [Complex.norm_real, Real.norm_eq_abs, abs_of_pos hr0]; exact hr1
  have h4 : HasSum (fun m : ℕ => -M * (r ^ m / (m : ℝ))) (-M * (-Real.log (1 - r))) := by
    have h := Complex.hasSum_re (Complex.hasSum_taylorSeries_neg_log hr')
    have e1 : ∀ m : ℕ, (((r : ℂ)) ^ m / (m : ℂ)).re = r ^ m / (m : ℝ) := by
      intro m
      rw [← Complex.ofReal_pow, ← Complex.ofReal_natCast, ← Complex.ofReal_div, Complex.ofReal_re]
    have e2 : (-Complex.log (1 - (r : ℂ))).re = -Real.log (1 - r) := by
      rw [Complex.neg_re, Complex.log_re, ← Complex.ofReal_one, ← Complex.ofReal_sub,
        Complex.norm_real, Real.norm_eq_abs, abs_of_pos (by linarith)]
    simp only [e1] at h
    rw [e2] at h
    exact h.mul_left (-M)
  have hterm : ∀ m : ℕ, -M * (r ^ m / (m : ℝ)) ≤ ∑ t ∈ T, (((r : ℂ) * z ^ s t) ^ m / (m : ℂ)).re := by
    intro m
    have e : ∀ t, (((r : ℂ) * z ^ s t) ^ m / (m : ℂ)).re = (r ^ m / (m : ℝ)) * ((z ^ m) ^ s t).re := by
      intro t
      have : ((r : ℂ) * z ^ s t) ^ m / (m : ℂ) = ((r ^ m / (m : ℝ) : ℝ) : ℂ) * (z ^ m) ^ s t := by
        push_cast
        ring
      rw [this, Complex.re_ofReal_mul]
    simp_rw [e, ← Finset.mul_sum]
    have hnn : 0 ≤ r ^ m / (m : ℝ) := by positivity
    have hw := hM (z ^ m) (by rw [norm_pow, hz, one_pow])
    nlinarith [mul_le_mul_of_nonneg_left hw hnn]
  have h5 := hasSum_le hterm h4 h3
  have h6 : ∑ t ∈ T, (Complex.log (1 - (r : ℂ) * z ^ s t)).re ≤ M * (-Real.log (1 - r)) := by
    simp only [Complex.neg_re, Finset.sum_neg_distrib] at h5
    linarith
  calc r ^ T.card * ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ ∏ t ∈ T, ‖1 - (r : ℂ) * z ^ s t‖ := h1
    _ = Real.exp (∑ t ∈ T, (Complex.log (1 - (r : ℂ) * z ^ s t)).re) := h2
    _ ≤ Real.exp (M * (-Real.log (1 - r))) := Real.exp_le_exp.mpr h6

/-- T1, (A4)/(B0) as used: `ρ ≥ 2` factors with cosine sum `≥ -M` have
`sup_{|z|=1} |∏ (1 - z^(s t))| ≤ 8·ρ^M`. (The paper has the constant 4; `e² < 8` is what the short
proof of `(1 - 1/ρ)^ρ ≥ e^(-2)` gives.) -/
private theorem t1_sup_bound {ι : Type} (T : Finset ι) (s : ι → ℕ) (M : ℝ)
    (hM : ∀ w : ℂ, ‖w‖ = 1 → -M ≤ ∑ t ∈ T, (w ^ s t).re)
    (hρ : 2 ≤ T.card) (z : ℂ) (hz : ‖z‖ = 1) :
    ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ 8 * (T.card : ℝ) ^ M := by
  have hρ2 : (2 : ℝ) ≤ (T.card : ℝ) := by exact_mod_cast hρ
  have hρpos : (0 : ℝ) < (T.card : ℝ) := by linarith
  obtain ⟨x, hxdef⟩ : ∃ x : ℝ, x = 1 / (T.card : ℝ) := ⟨_, rfl⟩
  have hx0 : 0 < x := by rw [hxdef]; positivity
  have hx1 : x ≤ 1 / 2 := by
    rw [hxdef]
    exact one_div_le_one_div_of_le (by norm_num) hρ2
  have hxρ : (T.card : ℝ) * x = 1 := by rw [hxdef]; field_simp
  have hb := t1_product_bound T s M hM z hz (1 - x) (by linarith) (by linarith)
  have e1 : 1 - (1 - x) = ((T.card : ℝ))⁻¹ := by rw [hxdef]; ring
  rw [e1, Real.log_inv, neg_neg] at hb
  have e2 : Real.exp (M * Real.log (T.card : ℝ)) = (T.card : ℝ) ^ M := by
    rw [Real.rpow_def_of_pos hρpos, mul_comm]
  rw [e2] at hb
  -- (1 - x)^ρ ≥ e^(-2)
  have h1 : 1 + 2 * x ≤ Real.exp (2 * x) := by
    have := Real.add_one_le_exp (2 * x)
    linarith
  have h2 : Real.exp (-(2 * x)) ≤ 1 - x := by
    rw [Real.exp_neg]
    calc (Real.exp (2 * x))⁻¹ ≤ (1 + 2 * x)⁻¹ := inv_anti₀ (by positivity) h1
      _ ≤ 1 - x := by
        rw [inv_le_iff_one_le_mul₀ (by positivity)]
        nlinarith [mul_nonneg hx0.le (by linarith : (0 : ℝ) ≤ 1 - 2 * x)]
  have h3 : Real.exp (-(2 * x)) ^ T.card ≤ (1 - x) ^ T.card :=
    pow_le_pow_left₀ (Real.exp_pos _).le h2 _
  have h4 : Real.exp (-(2 * x)) ^ T.card = Real.exp (-2) := by
    rw [← Real.exp_nat_mul]
    congr 1
    linarith
  rw [h4] at h3
  have hP : 0 ≤ ‖∏ t ∈ T, (1 - z ^ s t)‖ := norm_nonneg _
  have h5 : Real.exp (-2) * ‖∏ t ∈ T, (1 - z ^ s t)‖ ≤ (T.card : ℝ) ^ M :=
    le_trans (mul_le_mul_of_nonneg_right h3 hP) hb
  have h6 : ‖∏ t ∈ T, (1 - z ^ s t)‖ = Real.exp 2 * (Real.exp (-2) * ‖∏ t ∈ T, (1 - z ^ s t)‖) := by
    rw [← mul_assoc, ← Real.exp_add]
    norm_num
  have h7 : Real.exp 2 ≤ 8 := by
    have h : Real.exp 2 = Real.exp 1 * Real.exp 1 := by
      rw [← Real.exp_add]; norm_num
    nlinarith [Real.exp_one_lt_d9, Real.exp_pos 1]
  have hpow : 0 ≤ (T.card : ℝ) ^ M := Real.rpow_nonneg hρpos.le M
  rw [h6]
  calc Real.exp 2 * (Real.exp (-2) * ‖∏ t ∈ T, (1 - z ^ s t)‖)
      ≤ Real.exp 2 * (T.card : ℝ) ^ M := mul_le_mul_of_nonneg_left h5 (Real.exp_pos _).le
    _ ≤ 8 * (T.card : ℝ) ^ M := mul_le_mul_of_nonneg_right h7 hpow

/-! ### A concrete family with no outside input: all differences `j - i`, `0 ≤ i < j < p`
(the exponent `d` with multiplicity `p - d`: the Fejér kernel). -/

private theorem t1_unit_shift (w : ℂ) (hw : ‖w‖ = 1) (i d : ℕ) :
    (w ^ i * (starRingEnd ℂ) (w ^ (i + d))).re = (w ^ d).re := by
  have hww : w ^ i * (starRingEnd ℂ) (w ^ i) = 1 := by
    rw [Complex.mul_conj, Complex.normSq_eq_norm_sq, norm_pow, hw]
    simp
  rw [pow_add, map_mul, ← mul_assoc, hww, one_mul, Complex.conj_re]

private theorem t1_fejer_normSq (w : ℂ) (hw : ‖w‖ = 1) (p : ℕ) :
    Complex.normSq (∑ i ∈ Finset.range p, w ^ i) =
      (p : ℝ) + 2 * ∑ j ∈ Finset.range p, ∑ i ∈ Finset.range j, (w ^ (j - i)).re := by
  induction p with
  | zero => simp
  | succ p ih =>
    rw [Finset.sum_range_succ (fun i => w ^ i),
      Finset.sum_range_succ (fun j => ∑ i ∈ Finset.range j, (w ^ (j - i)).re),
      Complex.normSq_add, ih]
    have h1 : Complex.normSq (w ^ p) = 1 := by
      rw [Complex.normSq_eq_norm_sq, norm_pow, hw]
      simp
    have h2 : ((∑ i ∈ Finset.range p, w ^ i) * (starRingEnd ℂ) (w ^ p)).re =
        ∑ i ∈ Finset.range p, (w ^ (p - i)).re := by
      rw [Finset.sum_mul, Complex.re_sum]
      apply Finset.sum_congr rfl
      intro i hi
      have hi' : i ≤ p := (Finset.mem_range.mp hi).le
      have h := t1_unit_shift w hw i (p - i)
      rw [Nat.add_sub_of_le hi'] at h
      exact h
    rw [h1, h2]
    push_cast
    ring

/-- The cosine sum of the family is at least `-p/2`. -/
private theorem t1_fejer_cos (w : ℂ) (hw : ‖w‖ = 1) (p : ℕ) :
    -((p : ℝ) / 2) ≤ ∑ j ∈ Finset.range p, ∑ i ∈ Finset.range j, (w ^ (j - i)).re := by
  have h := t1_fejer_normSq w hw p
  have h0 := Complex.normSq_nonneg (∑ i ∈ Finset.range p, w ^ i)
  linarith

private theorem t1_fejer_card (p : ℕ) :
    ((Finset.range p).sigma (fun j => Finset.range j)).card * 2 = p * (p - 1) := by
  rw [Finset.card_sigma]
  simp only [Finset.card_range]
  exact Finset.sum_range_id_mul_two p

/-- T1's small-norm product, with no citation: for `p ≥ 3` the product of `1 - z^(j-i)` over
`0 ≤ i < j < p` (`ρ = p(p-1)/2` factors, exponents below `p`) is at most `8·ρ^(p/2)` on the unit circle,
against `2^ρ` for `(1 - z)^ρ`. -/
private theorem t1_fejer_sup (p : ℕ) (hp : 3 ≤ p) (z : ℂ) (hz : ‖z‖ = 1) :
    ‖∏ t ∈ (Finset.range p).sigma (fun j => Finset.range j), (1 - z ^ (t.1 - t.2))‖ ≤
      8 * ((((Finset.range p).sigma (fun j => Finset.range j)).card : ℕ) : ℝ) ^ ((p : ℝ) / 2) := by
  apply t1_sup_bound ((Finset.range p).sigma (fun j => Finset.range j)) (fun t => t.1 - t.2) ((p : ℝ) / 2)
  · intro w hw
    rw [Finset.sum_sigma]
    exact t1_fejer_cos w hw p
  · have hc := t1_fejer_card p
    have h6 : 3 * 2 ≤ p * (p - 1) := Nat.mul_le_mul hp (by omega)
    omega
  · exact hz

/-- T1: a coefficient of a polynomial is at most its sup on the unit circle (average over the
`n`-th roots of unity, `n = deg + 1`). This is the bridge from a sup bound for `∏(1 - z^s)` to the
ℓ¹ norm the certificate needs: `‖c‖₁ ≤ (N + 1)·sup`. -/
private theorem t1_coeff_le_sup (P : Polynomial ℂ) (B : ℝ) (hB : ∀ z : ℂ, ‖z‖ = 1 → ‖P.eval z‖ ≤ B) (i : ℕ) :
    ‖P.coeff i‖ ≤ B := by
  have hB0 : 0 ≤ B := le_trans (norm_nonneg _) (hB 1 (by simp))
  by_cases hi' : P.natDegree < i
  · rw [Polynomial.coeff_eq_zero_of_natDegree_lt hi']
    simpa using hB0
  have hi : i ≤ P.natDegree := not_lt.mp hi'
  obtain ⟨n, hn⟩ : ∃ n : ℕ, n = P.natDegree + 1 := ⟨_, rfl⟩
  have hn0 : n ≠ 0 := by omega
  obtain ⟨ζ, hprim⟩ : ∃ ζ : ℂ, IsPrimitiveRoot ζ n := ⟨_, Complex.isPrimitiveRoot_exp n hn0⟩
  have hζ1 : ‖ζ‖ = 1 := hprim.norm'_eq_one hn0
  have key : ∑ j ∈ Finset.range n, P.eval (ζ ^ j) * (ζ ^ j) ^ (n - i) = (n : ℂ) * P.coeff i := by
    simp_rw [Polynomial.eval_eq_sum_range, Finset.sum_mul]
    rw [Finset.sum_comm]
    have inner : ∀ m ∈ Finset.range (P.natDegree + 1),
        ∑ j ∈ Finset.range n, P.coeff m * (ζ ^ j) ^ m * (ζ ^ j) ^ (n - i) =
          if m = i then (n : ℂ) * P.coeff i else 0 := by
      intro m hm
      have hm' : m < n := by rw [hn]; exact Finset.mem_range.mp hm
      have e : ∀ j : ℕ, P.coeff m * (ζ ^ j) ^ m * (ζ ^ j) ^ (n - i) =
          P.coeff m * (ζ ^ (m + (n - i))) ^ j := by
        intro j; ring
      simp_rw [e, ← Finset.mul_sum]
      split_ifs with hmi
      · rw [hmi]
        have h1 : ζ ^ (i + (n - i)) = 1 := by
          rw [Nat.add_sub_of_le (by omega : i ≤ n)]
          exact hprim.pow_eq_one
        rw [h1]
        simp only [one_pow, Finset.sum_const, Finset.card_range, nsmul_eq_mul, mul_one]
        ring
      · have hne : ζ ^ (m + (n - i)) ≠ 1 := by
          intro h
          obtain ⟨q, hq⟩ := (hprim.pow_eq_one_iff_dvd _).mp h
          rcases q with _ | _ | q
          · simp at hq; omega
          · simp at hq; omega
          · have h2 : n * 2 ≤ n * (q + 1 + 1) := Nat.mul_le_mul_left n (by omega)
            omega
        have hs : ∑ j ∈ Finset.range n, (ζ ^ (m + (n - i))) ^ j = 0 := by
          have h := mul_geom_sum (ζ ^ (m + (n - i))) n
          have hpow : (ζ ^ (m + (n - i))) ^ n = 1 := by
            rw [← pow_mul, mul_comm, pow_mul, hprim.pow_eq_one, one_pow]
          rw [hpow, sub_self] at h
          exact (mul_eq_zero.mp h).resolve_left (sub_ne_zero.mpr hne)
        rw [hs, mul_zero]
    rw [Finset.sum_congr rfl inner, Finset.sum_ite_eq']
    exact if_pos (Finset.mem_range.mpr (Nat.lt_succ_of_le hi))
  have hnorm : ‖(n : ℂ) * P.coeff i‖ ≤ n * B := by
    rw [← key]
    calc ‖∑ j ∈ Finset.range n, P.eval (ζ ^ j) * (ζ ^ j) ^ (n - i)‖
        ≤ ∑ j ∈ Finset.range n, ‖P.eval (ζ ^ j) * (ζ ^ j) ^ (n - i)‖ := norm_sum_le _ _
      _ ≤ ∑ _j ∈ Finset.range n, B := by
          apply Finset.sum_le_sum
          intro j _
          rw [norm_mul, norm_pow, norm_pow, hζ1, one_pow, one_pow, mul_one]
          exact hB _ (by rw [norm_pow, hζ1, one_pow])
      _ = n * B := by rw [Finset.sum_const, Finset.card_range, nsmul_eq_mul]
  rw [norm_mul, Complex.norm_natCast] at hnorm
  have hnpos : (0 : ℝ) < n := by exact_mod_cast Nat.pos_of_ne_zero hn0
  exact le_of_mul_le_mul_left hnorm hnpos

/-- T1: the ℓ¹ norm of the certificate polynomial `∏ (1 - X^(s t))` over its first `N + 1`
coefficients, from the cosine-sum hypothesis: at most `(N + 1)·8·ρ^M`. -/
private theorem t1_l1_product {ι : Type} (T : Finset ι) (s : ι → ℕ) (M : ℝ)
    (hM : ∀ w : ℂ, ‖w‖ = 1 → -M ≤ ∑ t ∈ T, (w ^ s t).re)
    (hρ : 2 ≤ T.card) (N : ℕ) :
    ∑ i ∈ Finset.range (N + 1), ‖(∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℂ) ^ s t)).coeff i‖ ≤
      ((N : ℝ) + 1) * (8 * (T.card : ℝ) ^ M) := by
  have hB : ∀ z : ℂ, ‖z‖ = 1 →
      ‖(∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℂ) ^ s t)).eval z‖ ≤ 8 * (T.card : ℝ) ^ M := by
    intro z hz
    rw [Polynomial.eval_prod]
    simp only [Polynomial.eval_sub, Polynomial.eval_one, Polynomial.eval_pow, Polynomial.eval_X]
    exact t1_sup_bound T s M hM hρ z hz
  calc ∑ i ∈ Finset.range (N + 1), ‖(∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℂ) ^ s t)).coeff i‖
      ≤ ∑ _i ∈ Finset.range (N + 1), (8 * (T.card : ℝ) ^ M) :=
        Finset.sum_le_sum (fun i _ => t1_coeff_le_sup _ _ hB i)
    _ = ((N : ℝ) + 1) * (8 * (T.card : ℝ) ^ M) := by
        rw [Finset.sum_const, Finset.card_range, nsmul_eq_mul]
        push_cast
        ring

/-- The same for the integer polynomial (the form `A/t1_general.lean`'s `coefL_eq_coeff` speaks). -/
private theorem t1_l1_product_int {ι : Type} (T : Finset ι) (s : ι → ℕ) (M : ℝ)
    (hM : ∀ w : ℂ, ‖w‖ = 1 → -M ≤ ∑ t ∈ T, (w ^ s t).re)
    (hρ : 2 ≤ T.card) (N : ℕ) :
    ∑ i ∈ Finset.range (N + 1),
        |(((∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℤ) ^ s t)).coeff i : ℤ) : ℝ)| ≤
      ((N : ℝ) + 1) * (8 * (T.card : ℝ) ^ M) := by
  have hmap : (∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℤ) ^ s t)).map (Int.castRingHom ℂ) =
      ∏ t ∈ T, (1 - (Polynomial.X : Polynomial ℂ) ^ s t) := by
    rw [Polynomial.map_prod]
    simp
  have h := t1_l1_product T s M hM hρ N
  rw [← hmap] at h
  simp only [Polynomial.coeff_map, eq_intCast, Complex.norm_intCast] at h
  exact h

/-- For the family of all differences `j - i`, `0 ≤ i < j < p`: ℓ¹ norm at most `(N+1)·8·ρ^(p/2)`,
with nothing assumed. -/
private theorem t1_fejer_l1 (p : ℕ) (hp : 3 ≤ p) (N : ℕ) :
    ∑ i ∈ Finset.range (N + 1),
        |(((∏ t ∈ (Finset.range p).sigma (fun j => Finset.range j),
            (1 - (Polynomial.X : Polynomial ℤ) ^ (t.1 - t.2))).coeff i : ℤ) : ℝ)| ≤
      ((N : ℝ) + 1) *
        (8 * ((((Finset.range p).sigma (fun j => Finset.range j)).card : ℕ) : ℝ) ^ ((p : ℝ) / 2)) := by
  apply t1_l1_product_int ((Finset.range p).sigma (fun j => Finset.range j)) (fun t => t.1 - t.2)
    ((p : ℝ) / 2)
  · intro w hw
    rw [Finset.sum_sigma]
    exact t1_fejer_cos w hw p
  · have hc := t1_fejer_card p
    have h6 : 3 * 2 ≤ p * (p - 1) := Nat.mul_le_mul hp (by omega)
    omega

/-! ### Part (a) of T1 for the small-norm family, nothing assumed (agent B, 12:25Z)

`A/t1_general.lean` (agent A, above) and `B/t1_analytic.lean` (agent B, above) joined: Proposition 3
of B/T1-proof.md for the product over all differences `j - i`, `0 ≤ i < j < p`, scaled by `g`, with
the ℓ¹ norm replaced by the proved bound `(N + 1)·8·ρ^(p/2)`. -/

/-- The exponents of the family, as a list: all `j - i` with `0 ≤ i < j < p`. -/
private noncomputable def fejerList (p : ℕ) : List ℕ :=
  ((Finset.range p).sigma (fun j => Finset.range j)).toList.map (fun t => t.1 - t.2)

private theorem fejerList_length (p : ℕ) :
    (fejerList p).length = ((Finset.range p).sigma (fun j => Finset.range j)).card := by
  unfold fejerList
  rw [List.length_map, Finset.length_toList]

private theorem fejerList_prod_poly (p : ℕ) :
    ((fejerList p).map (fun s => (1 - Polynomial.X ^ s : Polynomial ℤ))).prod =
      ∏ t ∈ (Finset.range p).sigma (fun j => Finset.range j),
        (1 - (Polynomial.X : Polynomial ℤ) ^ (t.1 - t.2)) := by
  unfold fejerList
  rw [List.map_map]
  exact Finset.prod_map_toList _ _

private theorem t1_fejer_no_bad_translate (n k m g p : ℕ) (hp : 3 ≤ p) (hg : 0 < g) (hk : 0 < k) (hm : k < m)
    (hsmall : 2 * ((((fejerList p).sum : ℝ) + 1) *
          (8 * ((fejerList p).length : ℝ) ^ ((p : ℝ) / 2)))
        * ((m + g * (fejerList p).sum - k : ℕ) : ℝ)
      < ((m + g * (fejerList p).sum : ℕ) : ℝ))
    (hlo : 1 / 4 ≤ (n : ℚ) * (((g ^ (fejerList p).length * (fejerList p).prod : ℕ) : ℚ)
      * gfun (fejerList p).length ((m : ℚ) + ((g * (fejerList p).sum - (fejerList p).length : ℕ) : ℚ))))
    (hhi : (n : ℚ) * (((g ^ (fejerList p).length * (fejerList p).prod : ℕ) : ℚ)
      * gfun (fejerList p).length (m : ℚ)) ≤ 3 / 4) :
    ∃ j ≤ (fejerList p).sum, coefL (fejerList p) j ≠ 0 ∧ n % (m + g * j) < k := by
  have hlen := fejerList_length p
  have hcard := t1_fejer_card p
  have h6 : 3 * 2 ≤ p * (p - 1) := Nat.mul_le_mul hp (by omega)
  have hne : fejerList p ≠ [] := by
    intro h
    have h0 : (fejerList p).length = 0 := by rw [h]; rfl
    omega
  have hnorm : ∑ j ∈ Finset.range ((fejerList p).sum + 1), |((coefL (fejerList p) j : ℤ) : ℝ)| ≤
      (((fejerList p).sum : ℝ) + 1) * (8 * ((fejerList p).length : ℝ) ^ ((p : ℝ) / 2)) := by
    have h := t1_fejer_l1 p hp (fejerList p).sum
    rw [hlen]
    simp only [coefL_eq_coeff, fejerList_prod_poly]
    exact h
  have hx : (0 : ℝ) ≤ ((m + g * (fejerList p).sum - k : ℕ) : ℝ) := Nat.cast_nonneg _
  refine no_bad_translate_scaled n k m g (fejerList p) hne hg hk hm ?_ hlo hhi
  have hR : ((2 * (∑ j ∈ Finset.range ((fejerList p).sum + 1), |(coefL (fejerList p) j : ℚ)|)
        * ((m + g * (fejerList p).sum - k : ℕ) : ℚ) : ℚ) : ℝ)
      < (((m + g * (fejerList p).sum : ℕ) : ℚ) : ℝ) := by
    simp only [Rat.cast_mul, Rat.cast_sum, Rat.cast_abs, Rat.cast_intCast, Rat.cast_natCast,
      Rat.cast_ofNat]
    nlinarith [mul_le_mul_of_nonneg_right hnorm hx]
  exact_mod_cast hR
private theorem t1_tuning (c : ℝ) (hc : 0 < c) (ρ G : ℕ) (hρ : 1 ≤ ρ) (hρG : ρ ≤ G)
    (hlo : c * (ρ : ℝ) ^ ρ ≤ 3 / 4) (hhi : 26 / 100 ≤ c * (G : ℝ) ^ ρ) :
    ∃ g : ℕ, ρ ≤ g ∧ g ≤ G ∧ 26 / 100 ≤ c * (g : ℝ) ^ ρ ∧ c * (g : ℝ) ^ ρ ≤ 3 / 4 := by
  classical
  have hex : ∃ g : ℕ, ρ ≤ g ∧ 26 / 100 ≤ c * (g : ℝ) ^ ρ := ⟨G, hρG, hhi⟩
  obtain ⟨hg1, hg2⟩ := Nat.find_spec hex
  have hgG : Nat.find hex ≤ G := Nat.find_min' hex ⟨hρG, hhi⟩
  refine ⟨Nat.find hex, hg1, hgG, hg2, ?_⟩
  rcases Nat.eq_or_lt_of_le hg1 with heq | hlt
  · rw [← heq]; exact hlo
  · -- the step before the first one
    obtain ⟨h, hh⟩ : ∃ h : ℕ, Nat.find hex = h + 1 := ⟨Nat.find hex - 1, by omega⟩
    have hρh : ρ ≤ h := by omega
    have hnot : ¬ (ρ ≤ h ∧ 26 / 100 ≤ c * (h : ℝ) ^ ρ) := Nat.find_min hex (by omega)
    have hsmall : c * (h : ℝ) ^ ρ < 26 / 100 := by
      by_contra hcon
      exact hnot ⟨hρh, not_lt.mp hcon⟩
    have hpos : (0 : ℝ) < h := by exact_mod_cast (by omega : 0 < h)
    have h1 : (h : ℝ) + 1 ≤ h * Real.exp (1 / h) := by
      have hexp := Real.add_one_le_exp (1 / (h : ℝ))
      calc (h : ℝ) + 1 = h * (1 / h + 1) := by field_simp; ring
        _ ≤ h * Real.exp (1 / h) := mul_le_mul_of_nonneg_left hexp hpos.le
    have h2 : ((h : ℝ) + 1) ^ ρ ≤ (h * Real.exp (1 / h)) ^ ρ :=
      pow_le_pow_left₀ (by positivity) h1 ρ
    have h3 : ((h : ℝ) * Real.exp (1 / h)) ^ ρ = (h : ℝ) ^ ρ * Real.exp ((ρ : ℝ) / h) := by
      rw [mul_pow, ← Real.exp_nat_mul]
      congr 2
      ring
    have h4 : Real.exp ((ρ : ℝ) / h) ≤ Real.exp 1 := by
      apply Real.exp_le_exp.mpr
      rw [div_le_one hpos]
      exact_mod_cast hρh
    have h5 : Real.exp 1 < 2.7182818286 := Real.exp_one_lt_d9
    have hpow : (0 : ℝ) ≤ (h : ℝ) ^ ρ := by positivity
    have hcast : ((Nat.find hex : ℕ) : ℝ) = (h : ℝ) + 1 := by rw [hh]; push_cast; ring
    rw [hcast]
    have hstep : c * ((h : ℝ) + 1) ^ ρ ≤ c * (h : ℝ) ^ ρ * Real.exp 1 := by
      calc c * ((h : ℝ) + 1) ^ ρ ≤ c * ((h : ℝ) ^ ρ * Real.exp ((ρ : ℝ) / h)) := by
            rw [← h3]; exact mul_le_mul_of_nonneg_left h2 hc.le
        _ = c * (h : ℝ) ^ ρ * Real.exp ((ρ : ℝ) / h) := by ring
        _ ≤ c * (h : ℝ) ^ ρ * Real.exp 1 :=
            mul_le_mul_of_nonneg_left h4 (mul_nonneg hc.le hpow)
    have hq : (0 : ℝ) ≤ c * (h : ℝ) ^ ρ := mul_nonneg hc.le hpow
    nlinarith [hstep, hsmall, h5, hq, Real.exp_pos 1]

/-- The family's parameters: `ρ` steps, each in `[1, p]`. -/
private theorem t1_fejer_params (p : ℕ) :
    (fejerList p).length * 2 = p * (p - 1) ∧ (fejerList p).length ≤ (fejerList p).sum ∧
      (fejerList p).sum ≤ (fejerList p).length * p ∧ 1 ≤ (fejerList p).prod ∧
      (fejerList p).prod ≤ p ^ (fejerList p).length := by
  have hmem : ∀ s ∈ fejerList p, 1 ≤ s ∧ s ≤ p := by
    intro s hs
    unfold fejerList at hs
    rw [List.mem_map] at hs
    obtain ⟨t, ht, rfl⟩ := hs
    rw [Finset.mem_toList, Finset.mem_sigma, Finset.mem_range, Finset.mem_range] at ht
    omega
  refine ⟨by rw [fejerList_length]; exact t1_fejer_card p, ?_, ?_, ?_, ?_⟩
  · have h := List.card_nsmul_le_sum (fejerList p) 1 (fun s hs => (hmem s hs).1)
    simpa using h
  · have h := List.sum_le_card_nsmul (fejerList p) p (fun s hs => (hmem s hs).2)
    simpa using h
  · exact List.one_le_prod_of_one_le (fun s hs => (hmem s hs).1)
  · exact List.prod_le_pow_card (fejerList p) p (fun s hs => (hmem s hs).2)

private theorem t1_gfun_le (ρ : ℕ) (x y : ℚ) (hy : 0 < y) (hxy : y ≤ x) :
    gfun ρ x ≤ (ρ.factorial : ℚ) / y ^ (ρ + 1) := by
  unfold gfun
  have h : y ^ (ρ + 1) ≤ ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) := by
    have := Finset.prod_le_prod (s := Finset.range (ρ + 1)) (f := fun _ : ℕ => y)
      (g := fun i : ℕ => x + (i : ℚ)) (fun i _ => hy.le)
      (fun i _ => le_add_of_le_of_nonneg hxy (Nat.cast_nonneg i))
    simpa using this
  exact div_le_div_of_nonneg_left (by positivity) (by positivity) h

private theorem t1_le_gfun (ρ : ℕ) (x y : ℚ) (hx : 0 < x) (hxy : x + (ρ : ℚ) ≤ y) :
    (ρ.factorial : ℚ) / y ^ (ρ + 1) ≤ gfun ρ x := by
  unfold gfun
  have h : ∏ i ∈ Finset.range (ρ + 1), (x + (i : ℚ)) ≤ y ^ (ρ + 1) := by
    have := Finset.prod_le_prod (s := Finset.range (ρ + 1)) (f := fun i : ℕ => x + (i : ℚ))
      (g := fun _ : ℕ => y) (fun i _ => by positivity)
      (fun i hi => by
        have : (i : ℚ) ≤ (ρ : ℚ) := by exact_mod_cast Nat.lt_succ_iff.mp (Finset.mem_range.mp hi)
        linarith)
    simpa using this
  exact div_le_div_of_nonneg_left (by positivity) (Finset.prod_pos (fun i _ => by positivity)) h

/-- Pure counting: `M` consecutive blocks of `L` integers after `k`, each holding a `d` with `P d`. -/
private theorem t1_blocks (P : ℕ → Prop) [DecidablePred P] (k L M : ℕ) (hLM : L * M ≤ k)
    (h : ∀ m, k < m → m ≤ k + L * M → ∃ d, m ≤ d ∧ d < m + L ∧ P d) (hL : 0 < L) :
    M ≤ (Finset.filter P (Finset.Ioc k (2 * k))).card := by
  have h' : ∀ i, i < M → ∃ d, k + 1 + i * L ≤ d ∧ d < k + 1 + i * L + L ∧ P d := by
    intro i hi
    apply h
    · omega
    · have : (i + 1) * L ≤ M * L := Nat.mul_le_mul_right L hi
      nlinarith
  choose! f hf using h'
  have hmaps : ∀ i ∈ Finset.range M, f i ∈ Finset.filter P (Finset.Ioc k (2 * k)) := by
    intro i hi
    rw [Finset.mem_range] at hi
    obtain ⟨h1, h2, h3⟩ := hf i hi
    have : (i + 1) * L ≤ M * L := Nat.mul_le_mul_right L hi
    rw [Finset.mem_filter, Finset.mem_Ioc]
    refine ⟨⟨by omega, by nlinarith⟩, h3⟩
  have hinj : Set.InjOn f (Finset.range M : Set ℕ) := by
    have key : ∀ i j, i < M → j < M → i < j → f i < f j := by
      intro i j hi hj hij
      obtain ⟨_, h2, _⟩ := hf i hi
      obtain ⟨h1, _, _⟩ := hf j hj
      have : (i + 1) * L ≤ j * L := Nat.mul_le_mul_right L hij
      nlinarith
    intro i hi j hj hij
    have hi' : i < M := by simpa using hi
    have hj' : j < M := by simpa using hj
    rcases lt_trichotomy i j with hlt | heq | hgt
    · exact absurd hij (key i j hi' hj' hlt).ne
    · exact heq
    · exact absurd hij (key j i hj' hi' hgt).ne'
  have := Finset.card_le_card_of_injOn f hmaps hinj
  simpa using this

/-- `b` is the integer hundredth root of `k`. -/
private theorem t1_root (B k : ℕ) (hB : 2 ^ 100 ≤ B) (hk : B ^ 100 ≤ k) :
    ∃ b, B ≤ b ∧ b ^ 100 ≤ k ∧ k ≤ b ^ 101 := by
  have hex : ∃ x, k < (x + 1) ^ 100 := ⟨k, by
    calc k < k + 1 := Nat.lt_succ_self k
      _ ≤ (k + 1) ^ 100 := Nat.le_self_pow (by norm_num) _⟩
  classical
  obtain ⟨b, hb⟩ : ∃ b, b = Nat.find hex := ⟨_, rfl⟩
  have h1 : k < (b + 1) ^ 100 := hb ▸ Nat.find_spec hex
  have hBb : B ≤ b := by
    by_contra hcon
    have : (b + 1) ^ 100 ≤ B ^ 100 := Nat.pow_le_pow_left (by omega) 100
    omega
  have hb0 : 0 < b := lt_of_lt_of_le (lt_of_lt_of_le (by positivity) hB) hBb
  have h2 : b ^ 100 ≤ k := by
    by_contra hcon
    have hmin := Nat.find_min hex (m := b - 1) (by omega)
    apply hmin
    rw [Nat.sub_add_cancel hb0]
    omega
  refine ⟨b, hBb, h2, ?_⟩
  calc k ≤ (b + 1) ^ 100 := h1.le
    _ ≤ (2 * b) ^ 100 := Nat.pow_le_pow_left (by omega) 100
    _ = 2 ^ 100 * b ^ 100 := mul_pow _ _ _
    _ ≤ b * b ^ 100 := Nat.mul_le_mul_right _ (le_trans hB hBb)
    _ = b ^ 101 := by ring

private theorem t5_choose_g (M : ℕ) (n k b P ρ ρ' e β : ℕ) (hb : 2 ≤ b) (hn : 0 < n) (hk : 2 ≤ k) (hk2 : k ≤ b ^ (M + 101))
    (hρpos : 1 ≤ ρ) (hbase : ρ * P * ρ ≤ b) (hρG : ρ ≤ b) (he : ρ' + e = ρ + 1) (he2 : ρ' + (M + 101) * e ≤ 2 * ρ)
    (hβ1 : 1 ≤ β) (hβ2 : β ≤ P ^ ρ)
    (h1 : n * b ^ ρ ≤ k ^ ρ) (h2 : k ^ ρ' < n * b ^ ρ') :
    ∃ g : ℕ, ρ ≤ g ∧ g ≤ b ^ 2 ∧
      26 / 100 ≤ ((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ ∧
      ((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ ≤ 3 / 4 := by
  have hfac1 : 1 ≤ ρ.factorial := Nat.factorial_pos ρ
  have hkR : (0 : ℝ) < (k : ℝ) ^ (ρ + 1) := by
    have : (0 : ℝ) < k := by exact_mod_cast (by omega : 0 < k)
    positivity
  have hup : 4 * (n * ρ.factorial * β * ρ ^ ρ) ≤ 3 * k ^ (ρ + 1) := by
    have hf : ρ.factorial ≤ ρ ^ ρ := Nat.factorial_le_pow ρ
    have h3 : n * ρ.factorial * β * ρ ^ ρ ≤ n * b ^ ρ := by
      calc n * ρ.factorial * β * ρ ^ ρ ≤ n * ρ ^ ρ * P ^ ρ * ρ ^ ρ := by gcongr
        _ = n * (ρ * P * ρ) ^ ρ := by rw [mul_pow, mul_pow]; ring
        _ ≤ n * b ^ ρ := by gcongr
    have h4 : 2 * k ^ ρ ≤ k ^ (ρ + 1) := by
      rw [pow_succ, mul_comm]; exact Nat.mul_le_mul_left _ hk
    omega
  have hlow : k ^ (ρ + 1) ≤ n * ρ.factorial * β * (b ^ 2) ^ ρ := by
    calc k ^ (ρ + 1) = k ^ ρ' * k ^ e := by rw [← pow_add, he]
      _ ≤ (n * b ^ ρ') * (b ^ (M + 101)) ^ e := by gcongr
      _ = n * b ^ (ρ' + (M + 101) * e) := by rw [← pow_mul, mul_assoc, ← pow_add]
      _ ≤ n * b ^ (2 * ρ) := Nat.mul_le_mul_left _ (Nat.pow_le_pow_right (by omega) he2)
      _ = n * 1 * 1 * (b ^ 2) ^ ρ := by rw [← pow_mul]; ring
      _ ≤ n * ρ.factorial * β * (b ^ 2) ^ ρ := by gcongr
  have hc : (0 : ℝ) < ((n * ρ.factorial * β : ℕ) : ℝ) / (k : ℝ) ^ (ρ + 1) := by
    apply div_pos _ hkR
    exact_mod_cast Nat.mul_pos (Nat.mul_pos hn hfac1) hβ1
  obtain ⟨g, hg1, hg2, hg3, hg4⟩ := t1_tuning _ hc ρ (b ^ 2) hρpos
    (le_trans hρG (Nat.le_self_pow (by norm_num) b))
    (by
      rw [div_mul_eq_mul_div, div_le_div_iff₀ hkR (by norm_num)]
      have : (4 : ℝ) * (((n * ρ.factorial * β : ℕ) : ℝ) * (ρ : ℝ) ^ ρ) ≤ 3 * (k : ℝ) ^ (ρ + 1) := by
        exact_mod_cast hup
      linarith)
    (by
      rw [div_mul_eq_mul_div, div_le_div_iff₀ (by norm_num) hkR]
      have : (k : ℝ) ^ (ρ + 1) ≤ ((n * ρ.factorial * β : ℕ) : ℝ) * ((b ^ 2 : ℕ) : ℝ) ^ ρ := by
        exact_mod_cast hlow
      linarith)
  refine ⟨g, hg1, hg2, ?_, ?_⟩
  · have : ((26 / 100 : ℚ) : ℝ) ≤ ((((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ : ℚ) : ℝ) := by
      push_cast at hg3 ⊢
      exact hg3
    exact_mod_cast this
  · have : ((((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ : ℚ) : ℝ) ≤ ((3 / 4 : ℚ) : ℝ) := by
      push_cast at hg4 ⊢
      exact hg4
    exact_mod_cast this

/-- No translate of the scaled family in `(k, k + b^95]` is wholly bad, for a step list `S` with
norm at most `(N+1)·8b`. -/
private theorem t5_window (M : ℕ) (n k b P ρ' e : ℕ) (S : List ℕ) (hb : 2 ^ 100 ≤ b) (hn : 0 < n) (hk1 : b ^ (M + 100) ≤ k)
    (hk2 : k ≤ b ^ (M + 101)) (hSmem : ∀ s ∈ S, 1 ≤ s ∧ s ≤ P) (hρpos : 1 ≤ S.length)
    (hbase : S.length * P * S.length ≤ b) (hNb : S.length * P + 1 ≤ b) (hρb : S.length + 1 ≤ b)
    (he : ρ' + e = S.length + 1) (he2 : ρ' + (M + 101) * e ≤ 2 * S.length)
    (hnorm : ∑ j ∈ Finset.range (S.sum + 1), |((coefL S j : ℤ) : ℝ)| ≤ ((S.sum : ℝ) + 1) * (8 * (b : ℝ)))
    (h1 : n * b ^ S.length ≤ k ^ S.length) (h2 : k ^ ρ' < n * b ^ ρ')
    (m : ℕ) (hm1 : k < m) (hm2 : m ≤ k + b ^ (M + 95)) :
    ∃ d, m ≤ d ∧ d < m + b ^ 3 ∧ n % d < k := by
  have hSne : S ≠ [] := by
    intro h
    rw [h] at hρpos
    simp at hρpos
  have hN1 : S.length ≤ S.sum := by
    have h := List.card_nsmul_le_sum S 1 (fun s hs => (hSmem s hs).1)
    simpa using h
  have hN2 : S.sum ≤ S.length * P := by
    have h := List.sum_le_card_nsmul S P (fun s hs => (hSmem s hs).2)
    simpa using h
  have hβ1 : 1 ≤ S.prod := List.one_le_prod_of_one_le (fun s hs => (hSmem s hs).1)
  have hβ2 : S.prod ≤ P ^ S.length := List.prod_le_pow_card S P (fun s hs => (hSmem s hs).2)
  have hnb := fun g hg hk hm => no_bad_translate_scaled n k m g S hSne hg hk hm
  generalize hA : ∑ j ∈ Finset.range (S.sum + 1), |(coefL S j : ℚ)| = A at hnb
  have hAR : (A : ℝ) ≤ ((S.sum : ℝ) + 1) * (8 * (b : ℝ)) := by
    rw [← hA]
    simp only [Rat.cast_sum, Rat.cast_abs, Rat.cast_intCast]
    exact hnorm
  have hA0 : 0 ≤ A := by
    rw [← hA]
    exact Finset.sum_nonneg (fun _ _ => abs_nonneg _)
  clear hnorm hSmem hSne
  generalize S.length = ρ at *
  generalize S.sum = N at *
  generalize S.prod = β at *
  have hb1 : 1 ≤ b := le_trans (Nat.one_le_two_pow) hb
  have hb64 : 64 ≤ b := le_trans (by norm_num) hb
  have hkb : b ≤ k := le_trans (Nat.le_self_pow (by norm_num) b) hk1
  obtain ⟨g, hg1, hg2, hg3, hg4⟩ := t5_choose_g M n k b P ρ ρ' e β (by omega) hn (by omega) hk2 hρpos hbase
    (by omega) he he2 hβ1 hβ2 h1 h2
  have hNp : N + 1 ≤ b := by omega
  have hgN : g * N + 1 ≤ b ^ 3 := by
    have : g * (N + 1) ≤ b ^ 2 * b := Nat.mul_le_mul hg2 hNp
    have e : b ^ 2 * b = b ^ 3 := by ring
    rw [e] at this
    nlinarith
  have hg0 : 0 < g := by omega
  have h95 : b ^ 3 ≤ b ^ (M + 95) := Nat.pow_le_pow_right hb1 (by norm_num)
  obtain ⟨j, hj1, _, hj3⟩ := hnb g hg0 (by omega) hm1
    (by
      have hlen2 : m + g * N - k ≤ 2 * b ^ (M + 95) := by omega
      have hlen2R : ((m + g * N - k : ℕ) : ℝ) ≤ 2 * (b : ℝ) ^ (M + 95) := by exact_mod_cast hlen2
      have hNR : (N : ℝ) + 1 ≤ b := by exact_mod_cast hNp
      have hbR : (64 : ℝ) ≤ b := by exact_mod_cast hb64
      have hmR : (b : ℝ) ^ (M + 100) < ((m + g * N : ℕ) : ℝ) := by
        have : b ^ (M + 100) < m + g * N := by omega
        exact_mod_cast this
      have hA0R : (0 : ℝ) ≤ A := by exact_mod_cast hA0
      have hAb : (A : ℝ) ≤ (b : ℝ) * (8 * (b : ℝ)) :=
        le_trans hAR (mul_le_mul_of_nonneg_right hNR (by positivity))
      have hR : ((2 * A * ((m + g * N - k : ℕ) : ℚ) : ℚ) : ℝ) < (((m + g * N : ℕ) : ℚ) : ℝ) := by
        simp only [Rat.cast_mul, Rat.cast_ofNat, Rat.cast_natCast]
        calc 2 * (A : ℝ) * ((m + g * N - k : ℕ) : ℝ)
            ≤ 2 * ((b : ℝ) * (8 * (b : ℝ))) * (2 * (b : ℝ) ^ (M + 95)) := by gcongr
          _ = 32 * (b : ℝ) ^ (M + 97) := by ring
          _ ≤ (b : ℝ) ^ 3 * (b : ℝ) ^ (M + 97) := by
              gcongr
              nlinarith [pow_le_pow_left₀ (by norm_num : (0 : ℝ) ≤ 64) hbR 3]
          _ = (b : ℝ) ^ (M + 100) := by ring
          _ < _ := hmR
      exact_mod_cast hR)
    (by
      have hkQ : (0 : ℚ) < k := by exact_mod_cast (by omega : 0 < k)
      have hρN : ρ ≤ g * N := le_trans hg1 (Nat.le_mul_of_pos_right g (by omega))
      have hle := t1_le_gfun ρ ((m : ℚ) + ((g * N - ρ : ℕ) : ℚ)) ((k : ℚ) + 2 * (b : ℚ) ^ (M + 95))
        (by
          have : (0 : ℚ) < m := by exact_mod_cast (by omega : 0 < m)
          positivity)
        (by
          have : m + (g * N - ρ) + ρ ≤ k + 2 * b ^ (M + 95) := by omega
          exact_mod_cast this)
      have hbQ : (64 : ℚ) ≤ b := by exact_mod_cast hb64
      have hkbQ : (b : ℚ) ^ (M + 100) ≤ k := by exact_mod_cast hk1
      have hρQ : (ρ : ℚ) + 1 ≤ b := by
        have : ρ + 1 ≤ b := by omega
        exact_mod_cast this
      obtain ⟨h, hh⟩ : ∃ h : ℚ, h = 2 * (b : ℚ) ^ (M + 95) := ⟨_, rfl⟩
      rw [← hh] at hle
      have hh0 : 0 < h := by rw [hh]; positivity
      have hkh : (0 : ℚ) < k + h := by linarith
      -- Bernoulli
      have hbern := one_add_mul_le_pow (a := -(h / (k + h)))
        (by
          have : h / (k + h) ≤ 1 := by rw [div_le_one hkh]; linarith
          linarith) (ρ + 1)
      have hratio : 1 + -(h / (k + h)) = k / (k + h) := by field_simp; ring
      rw [hratio, div_pow] at hbern
      have hsmall : ((ρ + 1 : ℕ) : ℚ) * (h / (k + h)) ≤ 1 / 26 := by
        rw [← mul_div_assoc, div_le_iff₀ hkh]
        have e1 : ((ρ + 1 : ℕ) : ℚ) * h ≤ b * h := by
          apply mul_le_mul_of_nonneg_right _ hh0.le
          push_cast
          exact hρQ
        have e2 : (b : ℚ) * h * 26 ≤ (b : ℚ) ^ (M + 100) := by
          rw [hh]
          calc (b : ℚ) * (2 * (b : ℚ) ^ (M + 95)) * 26 = 52 * (b : ℚ) ^ (M + 96) := by ring
            _ ≤ (b : ℚ) ^ 4 * (b : ℚ) ^ (M + 96) := by
                gcongr
                have := pow_le_pow_left₀ (by norm_num : (0 : ℚ) ≤ 64) hbQ 4
                linarith
            _ = (b : ℚ) ^ (M + 100) := by ring
        linarith
      have hB : 25 / 26 ≤ (k : ℚ) ^ (ρ + 1) / (k + h) ^ (ρ + 1) := by linarith
      have hkp : (0 : ℚ) < (k : ℚ) ^ (ρ + 1) := by positivity
      have hkhp : (0 : ℚ) < (k + h) ^ (ρ + 1) := by positivity
      calc (1 / 4 : ℚ) = 26 / 100 * (25 / 26) := by norm_num
        _ ≤ (((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ) *
              ((k : ℚ) ^ (ρ + 1) / (k + h) ^ (ρ + 1)) := by
            apply mul_le_mul hg3 hB (by norm_num)
            exact le_trans (by norm_num) hg3
        _ = (n : ℚ) * (((g ^ ρ * β : ℕ) : ℚ) * ((ρ.factorial : ℚ) / (k + h) ^ (ρ + 1))) := by
            push_cast
            field_simp
        _ ≤ _ := by gcongr)
    (by
      have hkQ : (0 : ℚ) < k := by exact_mod_cast (by omega : 0 < k)
      have hle := t1_gfun_le ρ (m : ℚ) (k : ℚ) hkQ (by exact_mod_cast hm1.le)
      calc (n : ℚ) * (((g ^ ρ * β : ℕ) : ℚ) * gfun ρ (m : ℚ))
          ≤ (n : ℚ) * (((g ^ ρ * β : ℕ) : ℚ) * ((ρ.factorial : ℚ) / (k : ℚ) ^ (ρ + 1))) := by gcongr
        _ = ((n * ρ.factorial * β : ℕ) : ℚ) / (k : ℚ) ^ (ρ + 1) * (g : ℚ) ^ ρ := by
            push_cast
            field_simp
        _ ≤ 3 / 4 := hg4)
  refine ⟨m + g * j, by omega, ?_, hj3⟩
  have : g * j ≤ g * N := Nat.mul_le_mul_left g hj1
  omega


private theorem t5_root (M : ℕ) (B k : ℕ) (hB : 2 ^ (M + 100) ≤ B) (hk : B ^ (M + 100) ≤ k) :
    ∃ b, B ≤ b ∧ b ^ (M + 100) ≤ k ∧ k ≤ b ^ (M + 101) := by
  have hex : ∃ x, k < (x + 1) ^ (M + 100) := ⟨k, by
    calc k < k + 1 := Nat.lt_succ_self k
      _ ≤ (k + 1) ^ (M + 100) := Nat.le_self_pow (by norm_num) _⟩
  classical
  obtain ⟨b, hb⟩ : ∃ b, b = Nat.find hex := ⟨_, rfl⟩
  have h1 : k < (b + 1) ^ (M + 100) := hb ▸ Nat.find_spec hex
  have hBb : B ≤ b := by
    by_contra hcon
    have : (b + 1) ^ (M + 100) ≤ B ^ (M + 100) := Nat.pow_le_pow_left (by omega) (M + 100)
    omega
  have hb0 : 0 < b := lt_of_lt_of_le (lt_of_lt_of_le (by positivity) hB) hBb
  have h2 : b ^ (M + 100) ≤ k := by
    by_contra hcon
    have hmin := Nat.find_min hex (m := b - 1) (by omega)
    apply hmin
    rw [Nat.sub_add_cancel hb0]
    omega
  refine ⟨b, hBb, h2, ?_⟩
  calc k ≤ (b + 1) ^ (M + 100) := h1.le
    _ ≤ (2 * b) ^ (M + 100) := Nat.pow_le_pow_left (by omega) (M + 100)
    _ = 2 ^ (M + 100) * b ^ (M + 100) := mul_pow _ _ _
    _ ≤ b * b ^ (M + 100) := Nat.mul_le_mul_right _ (le_trans hB hBb)
    _ = b ^ (M + 101) := by ring

private theorem t5_pow_le_exp (D : ℕ) : ∃ X : ℝ, 1 ≤ X ∧ ∀ x : ℝ, X ≤ x → x ^ D ≤ Real.exp x := by
  have h := (Real.tendsto_exp_div_pow_atTop D).eventually_ge_atTop 1
  obtain ⟨X, hX⟩ := Filter.eventually_atTop.mp h
  refine ⟨max X 1, le_max_right _ _, fun x hx => ?_⟩
  have h1 := hX x (le_trans (le_max_left _ _) hx)
  have hx0 : 0 < x := lt_of_lt_of_le one_pos (le_trans (le_max_right _ _) hx)
  rwa [le_div_iff₀ (pow_pos hx0 D), one_mul] at h1

/-- The least `ρ ≥ R` with `n·b^ρ ≤ k^ρ`. -/
private theorem t5_choose (R n k b : ℕ) (hb : 2 ≤ b) (hk : 2 * b ≤ k) (hn : k ^ (R + 1) ≤ n) :
    ∃ ρ, R + 1 ≤ ρ ∧ n * b ^ ρ ≤ k ^ ρ ∧ k ^ (ρ - 1) < n * b ^ (ρ - 1) := by
  classical
  have hex : ∃ ρ, R ≤ ρ ∧ n * b ^ ρ ≤ k ^ ρ := by
    refine ⟨n + R, by omega, ?_⟩
    calc n * b ^ (n + R) ≤ 2 ^ (n + R) * b ^ (n + R) := by
          apply Nat.mul_le_mul_right
          exact le_trans Nat.lt_two_pow_self.le (Nat.pow_le_pow_right (by norm_num) (by omega))
      _ = (2 * b) ^ (n + R) := (mul_pow _ _ _).symm
      _ ≤ k ^ (n + R) := Nat.pow_le_pow_left hk _
  obtain ⟨q, hq⟩ : ∃ q, q = Nat.find hex := ⟨_, rfl⟩
  have h1 : R ≤ q ∧ n * b ^ q ≤ k ^ q := by rw [hq]; exact Nat.find_spec hex
  have h2 : R + 1 ≤ q := by
    by_contra hcon
    have e : q = R := by omega
    have h2 := h1.2
    rw [e] at h2
    have h3 : k ^ R < k ^ (R + 1) := Nat.pow_lt_pow_right (by omega) (by omega)
    have h4 : n * 1 ≤ n * b ^ R := Nat.mul_le_mul_left n (Nat.one_le_pow _ _ (by omega))
    omega
  refine ⟨q, h2, h1.2, ?_⟩
  have h5 := Nat.find_min hex (m := q - 1) (by omega)
  by_contra h6
  exact h5 ⟨by omega, not_lt.mp h6⟩

/-- The budget for `log n ≤ log^A k`. -/
private theorem t5_budget (A M E : ℕ) : ∃ B : ℕ, ∀ (n k b ρ : ℕ) (T : ℝ), B ≤ b → b ^ (M + 100) ≤ k →
    k ≤ b ^ (M + 101) → 0 < n → 2 ≤ ρ → k ^ (ρ - 1) < n * b ^ (ρ - 1) →
    Real.log (n : ℝ) ≤ Real.log (k : ℝ) ^ A → 0 ≤ T → T ^ (2 * (A + 1)) ≤ (ρ : ℝ) →
    Real.exp T ≤ (b : ℝ) ∧ ρ ^ (E + 2) ≤ b := by
  obtain ⟨X, hX1, hX⟩ := t5_pow_le_exp (2 * (A + 1) * (E + 2))
  refine ⟨⌈Real.exp (max X (M + 101))⌉₊, ?_⟩
  intro n k b ρ T hB hk1 hk2 hn hρ h2 hlog hT0 hT
  have hbe : Real.exp (max X (M + 101)) ≤ (b : ℝ) := Nat.le_of_ceil_le hB
  have hbR : (0 : ℝ) < b := lt_of_lt_of_le (Real.exp_pos _) hbe
  obtain ⟨lb, hlb⟩ : ∃ x, x = Real.log (b : ℝ) := ⟨_, rfl⟩
  have hlbmax : max X (M + 101) ≤ lb := by rw [hlb]; exact (Real.le_log_iff_exp_le hbR).mpr hbe
  have hlbX : X ≤ lb := le_trans (le_max_left _ _) hlbmax
  have hlbM : (M : ℝ) + 101 ≤ lb := le_trans (le_max_right _ _) hlbmax
  have hM0 : (0 : ℝ) ≤ M := Nat.cast_nonneg M
  have hb0 : 0 < b := by exact_mod_cast hbR
  have hkR : (0 : ℝ) < k := by
    have : 0 < k := lt_of_lt_of_le (Nat.pow_pos hb0) hk1
    exact_mod_cast this
  have hnR : (0 : ℝ) < n := by exact_mod_cast hn
  obtain ⟨L, hL⟩ : ∃ x, x = Real.log (k : ℝ) := ⟨_, rfl⟩
  rw [← hL] at hlog
  have hL1 : ((M : ℝ) + 100) * lb ≤ L := by
    have := Real.log_le_log (by positivity) (show ((b : ℝ)) ^ (M + 100) ≤ k by exact_mod_cast hk1)
    rw [Real.log_pow] at this
    rw [hlb, hL]
    push_cast at this
    exact this
  have hL2 : L ≤ ((M : ℝ) + 101) * lb := by
    have := Real.log_le_log hkR (show (k : ℝ) ≤ (b : ℝ) ^ (M + 101) by exact_mod_cast hk2)
    rw [Real.log_pow] at this
    rw [hlb, hL]
    push_cast at this
    exact this
  have hlb0 : 0 < lb := by linarith
  have hL100 : 100 * lb ≤ L := le_trans (mul_le_mul_of_nonneg_right (by linarith) hlb0.le) hL1
  have hLsq : L ≤ lb ^ 2 := by
    rw [sq]
    exact le_trans hL2 (mul_le_mul_of_nonneg_right hlbM hlb0.le)
  have hL10 : 100 ≤ L := by linarith
  have hρ1 : ((ρ - 1 : ℕ) : ℝ) * (99 / 100 * L) ≤ L ^ A := by
    have h3 : Real.log ((k : ℝ) ^ (ρ - 1)) ≤ Real.log ((n : ℝ) * (b : ℝ) ^ (ρ - 1)) :=
      Real.log_le_log (by positivity) (by exact_mod_cast h2.le)
    rw [Real.log_mul hnR.ne' (by positivity), Real.log_pow, Real.log_pow, ← hlb, ← hL] at h3
    have h4 : ((ρ - 1 : ℕ) : ℝ) * lb ≤ ((ρ - 1 : ℕ) : ℝ) * (1 / 100 * L) :=
      mul_le_mul_of_nonneg_left (by linarith) (Nat.cast_nonneg _)
    linarith
  have hρL : (ρ : ℝ) ≤ L ^ (A + 1) := by
    have h3 : (ρ : ℝ) ≤ 2 * ((ρ - 1 : ℕ) : ℝ) := by
      have : ρ ≤ 2 * (ρ - 1) := by omega
      exact_mod_cast this
    have h4 : ((ρ - 1 : ℕ) : ℝ) * 2 ≤ ((ρ - 1 : ℕ) : ℝ) * (99 / 100 * L) :=
      mul_le_mul_of_nonneg_left (by linarith) (Nat.cast_nonneg _)
    have h5 : L ^ A * 1 ≤ L ^ A * L := mul_le_mul_of_nonneg_left (by linarith) (by positivity)
    rw [pow_succ]
    linarith
  constructor
  · have h3 : (T ^ 2) ^ (A + 1) ≤ L ^ (A + 1) := by
      rw [← pow_mul]
      exact le_trans hT hρL
    have h4 : T ^ 2 ≤ L := le_of_pow_le_pow_left₀ (by omega) (by linarith) h3
    have h5 : T ≤ lb := le_of_pow_le_pow_left₀ (show (2 : ℕ) ≠ 0 by norm_num) hlb0.le (le_trans h4 hLsq)
    rw [← Real.le_log_iff_exp_le hbR, ← hlb]
    exact h5
  · have h3 : (ρ : ℝ) ^ (E + 2) ≤ (b : ℝ) := by
      calc (ρ : ℝ) ^ (E + 2) ≤ ((lb ^ 2) ^ (A + 1)) ^ (E + 2) := by
            apply pow_le_pow_left₀ (Nat.cast_nonneg _)
            exact le_trans hρL (pow_le_pow_left₀ (by linarith) hLsq _)
        _ = lb ^ (2 * (A + 1) * (E + 2)) := by rw [← pow_mul, ← pow_mul, mul_assoc]
        _ ≤ Real.exp lb := hX lb hlbX
        _ = b := by rw [hlb, Real.exp_log hbR]
    exact_mod_cast h3

private theorem t5_dense (hprod : ∀ (j : ℕ), ∃ (E ρ₀ : ℕ), ∀ (ρ : ℕ), ρ₀ ≤ ρ → ∃ (s : ℕ → ℕ),
      (∀ (i : ℕ), i < ρ → (1 : ℕ) ≤ s i ∧ s i ≤ ρ ^ E) ∧
        ∃ (T : ℝ), (0 : ℝ) ≤ T ∧ T ^ j ≤ (↑ρ : ℝ) ∧
          ∀ (N : ℕ), ∑ i ∈ Finset.range (N + (1 : ℕ)),
            |(↑((∏ t ∈ Finset.range ρ, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ s t)).coeff i) : ℝ)| ≤
              ((N : ℝ) + 1) * (8 * Real.exp T)) :
    ∀ (A : ℕ), ∃ (u₀ K₀ : ℕ), ∀ (n k : ℕ), K₀ ≤ k → k ^ u₀ ≤ n →
      Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ A →
      k ^ (A + (1 : ℕ)) ≤ (Finset.card (Finset.filter (fun (d : ℕ) => n % d < k) (Finset.Ioc k ((2 : ℕ) * k)))) ^ (A + (2 : ℕ)) := by
  intro A
  obtain ⟨M, hM⟩ : ∃ M, M = 9 * A := ⟨_, rfl⟩
  obtain ⟨E, ρ₀, hfam⟩ := hprod (2 * (A + 1))
  obtain ⟨B1, hB1⟩ := t5_budget A M E
  obtain ⟨R, hR1, hR2⟩ : ∃ R, ρ₀ ≤ R ∧ 2 * M + 202 ≤ R :=
    ⟨max ρ₀ (2 * M + 202), le_max_left _ _, le_max_right _ _⟩
  obtain ⟨B, hBa, hBb'⟩ : ∃ B : ℕ, B1 ≤ B ∧ 2 ^ (M + 100) ≤ B := ⟨max B1 (2 ^ (M + 100)), le_max_left _ _, le_max_right _ _⟩
  refine ⟨R + 1, B ^ (M + 100), ?_⟩
  intro n k hK hn hrange
  obtain ⟨b, hBb, hk1, hk2⟩ := t5_root M B k hBb' hK
  have hb : 2 ^ 100 ≤ b := le_trans (Nat.pow_le_pow_right (by norm_num) (by omega)) (le_trans hBb' hBb)
  have hb2 : 2 ≤ b := le_trans (by norm_num) hb
  have hk2b : 2 * b ≤ k := by
    calc 2 * b ≤ b * b := Nat.mul_le_mul_right b hb2
      _ = b ^ 2 := (sq b).symm
      _ ≤ b ^ (M + 100) := Nat.pow_le_pow_right (by omega) (by omega)
      _ ≤ k := hk1
  have hn0 : 0 < n := lt_of_lt_of_le (Nat.pow_pos (by omega)) hn
  obtain ⟨ρ, hρ, h1, h2⟩ := t5_choose R n k b hb2 hk2b hn
  obtain ⟨s, hs, T, hT0, hT, hsum⟩ := hfam ρ (by omega)
  obtain ⟨hexp, hpow⟩ := hB1 n k b ρ T (le_trans hBa hBb) hk1 hk2 hn0 (by omega) h2 hrange hT0 hT
  obtain ⟨S, hS⟩ : ∃ S, S = (Finset.range ρ).toList.map s := ⟨_, rfl⟩
  have hlen : S.length = ρ := by rw [hS, List.length_map, Finset.length_toList, Finset.card_range]
  have hSmem : ∀ x ∈ S, 1 ≤ x ∧ x ≤ ρ ^ E := by
    intro x hx
    rw [hS, List.mem_map] at hx
    obtain ⟨t, ht, rfl⟩ := hx
    rw [Finset.mem_toList, Finset.mem_range] at ht
    exact hs t ht
  have hpoly : (S.map (fun s => (1 - Polynomial.X ^ s : Polynomial ℤ))).prod =
      ∏ t ∈ Finset.range ρ, (1 - (Polynomial.X : Polynomial ℤ) ^ s t) := by
    rw [hS, List.map_map]
    exact Finset.prod_map_toList _ _
  have hP1 : 1 ≤ ρ ^ E := Nat.one_le_pow _ _ (by omega)
  have hbase : ρ * ρ ^ E * ρ ≤ b := by
    have e : ρ * ρ ^ E * ρ = ρ ^ (E + 2) := by ring
    rw [e]
    exact hpow
  have hNb : ρ * ρ ^ E + 1 ≤ b := by
    have h3 : ρ * ρ ^ E * 2 ≤ ρ * ρ ^ E * ρ := Nat.mul_le_mul_left _ (by omega)
    have h4 : 1 ≤ ρ * ρ ^ E := Nat.mul_pos (by omega) hP1
    omega
  have hρb : ρ + 1 ≤ b := by
    have : ρ * 1 ≤ ρ * ρ ^ E := Nat.mul_le_mul_left _ hP1
    omega
  have hl2 : ∑ j ∈ Finset.range (S.sum + 1), |((coefL S j : ℤ) : ℝ)| ≤ ((S.sum : ℝ) + 1) * (8 * (b : ℝ)) := by
    simp only [coefL_eq_coeff, hpoly]
    refine le_trans (hsum S.sum) (mul_le_mul_of_nonneg_left ?_ (by positivity))
    linarith
  have e95 : b ^ 3 * b ^ (M + 92) = b ^ (M + 95) := by rw [← pow_add]; congr 1; omega
  have hcount := t1_blocks (fun d => n % d < k) k (b ^ 3) (b ^ (M + 92))
    (by rw [e95]; exact le_trans (Nat.pow_le_pow_right (by omega) (by omega)) hk1)
    (by
      intro m hm1 hm2
      rw [e95] at hm2
      exact t5_window M n k b (ρ ^ E) (ρ - 1) 2 S hb hn0 hk1 hk2
        hSmem (by omega) (by rw [hlen]; exact hbase) (by rw [hlen]; exact hNb) (by rw [hlen]; exact hρb)
        (by omega) (by omega) hl2 (by rw [hlen]; exact h1) h2 m hm1 hm2)
    (Nat.pow_pos (by omega))
  have h9 : (b ^ (M + 101)) ^ (A + 1) ≤ (b ^ (M + 92)) ^ (A + 2) := by
    rw [← pow_mul, ← pow_mul]
    apply Nat.pow_le_pow_right (by omega)
    rw [hM]
    nlinarith
  exact le_trans (Nat.pow_le_pow_left hk2 _) (le_trans h9 (Nat.pow_le_pow_left hcount _))

/-- A cosine sum with non-negative integer coefficients of total `ρ`, bounded below by `-Q`, as a family of
`ρ` exponents with a bound for the coefficients of `∏ (1 - X^s)`. -/
private theorem t5_fam (ρ : ℕ) (hρ2 : 2 ≤ ρ) (Q : ℝ) (a : ℕ → ℕ) (ha1 : ∑ k ∈ Finset.Icc (1 : ℕ) ρ, a k = ρ)
    (ha2 : ∀ (x : ℝ), -Q ≤ ∑ k ∈ Finset.Icc (1 : ℕ) ρ, (↑(a k) : ℝ) * Real.cos ((↑k : ℝ) * x)) :
    ∃ (s : ℕ → ℕ), (∀ (i : ℕ), i < ρ → (1 : ℕ) ≤ s i ∧ s i ≤ ρ) ∧
      ∀ (N : ℕ), ∑ i ∈ Finset.range (N + (1 : ℕ)),
        |(↑((∏ t ∈ Finset.range ρ, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ s t)).coeff i) : ℝ)| ≤
          ((N : ℝ) + 1) * (8 * Real.exp (Q * Real.log ρ)) := by
  have hρR : (0 : ℝ) < ρ := by exact_mod_cast (by omega : 0 < ρ)
  obtain ⟨F, hF⟩ : ∃ F, F = (Finset.Icc 1 ρ).sigma (fun d => Finset.range (a d)) := ⟨_, rfl⟩
  have hcard : F.card = ρ := by
    rw [hF, Finset.card_sigma]
    simp only [Finset.card_range]
    exact ha1
  have hmem : ∀ t ∈ F, 1 ≤ t.1 ∧ t.1 ≤ ρ := by
    intro t ht
    rw [hF, Finset.mem_sigma, Finset.mem_Icc] at ht
    exact ht.1
  obtain ⟨e⟩ : Nonempty (F ≃ Fin ρ) := ⟨Fintype.equivFinOfCardEq (by rw [Fintype.card_coe]; exact hcard)⟩
  obtain ⟨s, hs⟩ : ∃ s : ℕ → ℕ, ∀ i : Fin ρ, s i = (e.symm i).1.1 :=
    ⟨fun i => if h : i < ρ then (e.symm ⟨i, h⟩).1.1 else 1, fun i => by simp⟩
  have hprod : ∏ t ∈ Finset.range ρ, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ s t) =
      ∏ t ∈ F, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ t.1) := by
    rw [Finset.prod_range, ← Finset.prod_coe_sort F (fun t => (1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ t.1)]
    refine Fintype.prod_equiv e.symm _ _ (fun i => ?_)
    rw [hs i]
  refine ⟨s, ?_, ?_⟩
  · intro i hi
    have h2 : s i = (e.symm ⟨i, hi⟩).1.1 := hs ⟨i, hi⟩
    rw [h2]
    exact hmem _ (e.symm ⟨i, hi⟩).2
  · intro N
    rw [hprod]
    have h := t1_l1_product_int F (fun t => t.1) Q
      (by
        intro w hw
        have hsig : ∑ t ∈ F, (w ^ t.1).re = ∑ d ∈ Finset.Icc 1 ρ, (a d : ℝ) * (w ^ d).re := by
          rw [hF, Finset.sum_sigma]
          simp only [Finset.sum_const, Finset.card_range, nsmul_eq_mul]
        rw [hsig]
        have h1 := ha2 w.arg
        have hw2 : w = Complex.exp (w.arg * Complex.I) := by
          have := Complex.norm_mul_exp_arg_mul_I w
          rw [hw] at this
          simpa using this.symm
        have h2 : ∀ d : ℕ, (w ^ d).re = Real.cos ((d : ℝ) * w.arg) := by
          intro d
          conv_lhs => rw [hw2]
          rw [← Complex.exp_nat_mul, ← mul_assoc]
          have : ((d : ℂ) * (w.arg : ℂ)) = (((d : ℝ) * w.arg : ℝ) : ℂ) := by push_cast; rfl
          rw [this, Complex.exp_ofReal_mul_I_re]
        simp only [h2]
        exact h1)
      (by rw [hcard]; exact hρ2) N
    rw [hcard] at h
    have hr : (ρ : ℝ) ^ Q = Real.exp (Q * Real.log ρ) := by
      rw [Real.rpow_def_of_pos hρR, mul_comm]
    rw [hr] at h
    exact h

/-- The least `ν` with `ρ ≤ ν^q` has `ν^q ≤ 2^q·ρ`. -/
private theorem t5_root_up (q ρ : ℕ) (hq : 1 ≤ q) (hρ : 2 ≤ ρ) : ∃ ν : ℕ, ρ ≤ ν ^ q ∧ ν ^ q ≤ 2 ^ q * ρ := by
  classical
  have hex : ∃ ν, ρ ≤ ν ^ q := ⟨ρ, Nat.le_self_pow (by omega) ρ⟩
  obtain ⟨ν, hν⟩ : ∃ ν, ν = Nat.find hex := ⟨_, rfl⟩
  have h1 : ρ ≤ ν ^ q := hν ▸ Nat.find_spec hex
  have h2 : 2 ≤ ν := by
    by_contra hcon
    have : ν ^ q ≤ 1 ^ q := Nat.pow_le_pow_left (by omega) q
    rw [one_pow] at this
    omega
  have h3 : (ν - 1) ^ q < ρ := by
    have := Nat.find_min hex (m := ν - 1) (by omega)
    omega
  refine ⟨ν, h1, ?_⟩
  calc ν ^ q ≤ (2 * (ν - 1)) ^ q := Nat.pow_le_pow_left (by omega) q
    _ = 2 ^ q * (ν - 1) ^ q := mul_pow _ _ _
    _ ≤ 2 ^ q * ρ := Nat.mul_le_mul_left _ h3.le

private theorem t5_cosq (hcos : ∀ q : ℕ, 2 ≤ q → ∃ C : ℕ, ∀ ν ρ : ℕ, 1 ≤ ρ → ρ ≤ ν ^ q → ∃ a : ℕ → ℕ,
      (∑ k ∈ Finset.Icc 1 ρ, a k = ρ) ∧
        ∀ x : ℝ, -((C : ℝ) * ν) ≤ ∑ k ∈ Finset.Icc 1 ρ, (a k : ℝ) * Real.cos (k * x)) :
    ∀ (j : ℕ), ∃ (E ρ₀ : ℕ), ∀ (ρ : ℕ), ρ₀ ≤ ρ → ∃ (s : ℕ → ℕ),
      (∀ (i : ℕ), i < ρ → (1 : ℕ) ≤ s i ∧ s i ≤ ρ ^ E) ∧
        ∃ (T : ℝ), (0 : ℝ) ≤ T ∧ T ^ j ≤ (↑ρ : ℝ) ∧
          ∀ (N : ℕ), ∑ i ∈ Finset.range (N + (1 : ℕ)),
            |(↑((∏ t ∈ Finset.range ρ, ((1 : Polynomial ℤ) - (Polynomial.X : Polynomial ℤ) ^ s t)).coeff i) : ℝ)| ≤
              ((N : ℝ) + 1) * (8 * Real.exp T) := by
  intro j
  obtain ⟨C, hC⟩ := hcos (2 * (j + 1)) (by omega)
  obtain ⟨X, hX1, hX⟩ := t5_pow_le_exp (4 * (j + 1) + 1)
  refine ⟨1, ⌈Real.exp (max X (max (2 * C) 4))⌉₊, ?_⟩
  intro ρ hρ
  have hρe : Real.exp (max X (max (2 * C) 4)) ≤ (ρ : ℝ) := Nat.le_of_ceil_le hρ
  have hρR : (0 : ℝ) < ρ := lt_of_lt_of_le (Real.exp_pos _) hρe
  have hlog : max X (max (2 * C) 4) ≤ Real.log ρ := (Real.le_log_iff_exp_le hρR).mpr hρe
  have hlX : X ≤ Real.log ρ := le_trans (le_max_left _ _) hlog
  have hlC : 2 * (C : ℝ) ≤ Real.log ρ := le_trans (le_trans (le_max_left _ _) (le_max_right _ _)) hlog
  have hl4 : 4 ≤ Real.log ρ := le_trans (le_trans (le_max_right _ _) (le_max_right _ _)) hlog
  have hexpl : Real.exp (Real.log ρ) = ρ := Real.exp_log hρR
  have hρ2 : 2 ≤ ρ := by
    have h1 := Real.add_one_le_exp (Real.log ρ)
    rw [hexpl] at h1
    have : (2 : ℝ) ≤ ρ := by linarith
    exact_mod_cast this
  obtain ⟨ν, hν1, hν2⟩ := t5_root_up (2 * (j + 1)) ρ (by omega) hρ2
  obtain ⟨a, ha1, ha2⟩ := hC ν ρ (by omega) hν1
  obtain ⟨s, hs1, hs2⟩ := t5_fam ρ hρ2 (C * ν) a ha1 ha2
  generalize Real.log (ρ : ℝ) = l at *
  have hT0 : 0 ≤ (C : ℝ) * ν * l := by positivity
  refine ⟨s, fun i hi => by rw [pow_one]; exact hs1 i hi, C * ν * l, hT0, ?_, hs2⟩
  have hCl : (2 * (C : ℝ) * l) ^ (2 * (j + 1)) ≤ ρ := by
    have h1 : 2 * (C : ℝ) * l ≤ l ^ 2 := by
      rw [sq]
      exact mul_le_mul_of_nonneg_right hlC (by linarith)
    have h2 : (2 * (C : ℝ) * l) ^ (2 * (j + 1)) ≤ l ^ (4 * (j + 1)) := by
      calc (2 * (C : ℝ) * l) ^ (2 * (j + 1)) ≤ (l ^ 2) ^ (2 * (j + 1)) := pow_le_pow_left₀ (by positivity) h1 _
        _ = l ^ (4 * (j + 1)) := by rw [← pow_mul]; congr 1; ring
    have h3 := hX l hlX
    rw [hexpl, pow_succ] at h3
    have h4 : l ^ (4 * (j + 1)) * 1 ≤ l ^ (4 * (j + 1)) * l := mul_le_mul_of_nonneg_left (by linarith) (by positivity)
    linarith
  have hνR : (ν : ℝ) ^ (2 * (j + 1)) ≤ 2 ^ (2 * (j + 1)) * ρ := by exact_mod_cast hν2
  have hmain : (((C : ℝ) * ν * l) ^ (j + 1)) ^ 2 ≤ (ρ : ℝ) ^ 2 := by
    calc (((C : ℝ) * ν * l) ^ (j + 1)) ^ 2 = ((C : ℝ) * l) ^ (2 * (j + 1)) * (ν : ℝ) ^ (2 * (j + 1)) := by
          rw [← pow_mul, ← mul_pow]
          congr 1 <;> ring
      _ ≤ ((C : ℝ) * l) ^ (2 * (j + 1)) * (2 ^ (2 * (j + 1)) * ρ) :=
          mul_le_mul_of_nonneg_left hνR (by positivity)
      _ = (2 * (C : ℝ) * l) ^ (2 * (j + 1)) * ρ := by
          rw [← mul_assoc, ← mul_pow]
          congr 2
          ring
      _ ≤ ρ * ρ := mul_le_mul_of_nonneg_right hCl hρR.le
      _ = (ρ : ℝ) ^ 2 := (sq _).symm
  have hT : ((C : ℝ) * ν * l) ^ (j + 1) ≤ ρ := le_of_pow_le_pow_left₀ (by norm_num) hρR.le hmain
  rcases le_total ((C : ℝ) * ν * l) 1 with h | h
  · refine le_trans (pow_le_one₀ hT0 h) ?_
    exact_mod_cast (by omega : 1 ≤ ρ)
  · exact le_trans (pow_le_pow_right₀ h (Nat.le_succ j)) hT

private lemma cq_dirichlet (N : ℕ) (x : ℝ) :
    2 * Real.sin (x / 2) * ∑ k ∈ Finset.Icc 1 N, Real.cos ((k : ℝ) * x) =
      Real.sin (((N : ℝ) + 1 / 2) * x) - Real.sin (x / 2) := by
  induction N with
  | zero =>
    have e : (((0 : ℕ) : ℝ) + 1 / 2) * x = x / 2 := by push_cast; ring
    rw [e]; simp
  | succ N ih =>
    rw [Finset.sum_Icc_succ_top (by omega), mul_add, ih]
    have e1 : (((N + 1 : ℕ) : ℝ) + 1 / 2) * x = ((N + 1 : ℕ) : ℝ) * x + x / 2 := by ring
    have e2 : ((N : ℝ) + 1 / 2) * x = ((N + 1 : ℕ) : ℝ) * x - x / 2 := by push_cast; ring
    rw [e1, e2, Real.sin_add, Real.sin_sub]
    ring

private noncomputable def cq_g (w : ℝ) (q : ℕ) (u : ℝ) : ℝ := w ^ q * (u ^ q)⁻¹

private noncomputable def cq_P (w : ℝ) (q : ℕ) (u : ℝ) : ℝ :=
  -(u ^ (q + 1) / (q * w ^ q)) * (1 - Real.cos (cq_g w q u))

private lemma cq_g_pos (w : ℝ) (hw : 0 < w) (q : ℕ) (u : ℝ) (hu : 0 < u) : 0 < cq_g w q u := by
  unfold cq_g; positivity

private lemma cq_g_anti (w : ℝ) (hw : 0 < w) (q : ℕ) (u v : ℝ) (hu : 0 < u) (huv : u ≤ v) :
    cq_g w q v ≤ cq_g w q u := by
  unfold cq_g
  have h1 : u ^ q ≤ v ^ q := pow_le_pow_left₀ hu.le huv q
  exact mul_le_mul_of_nonneg_left (inv_anti₀ (by positivity) h1) (by positivity)

private lemma cq_deriv (w : ℝ) (hw : 0 < w) (q : ℕ) (hq : 2 ≤ q) (u : ℝ) (hu : 0 < u) :
    HasDerivAt (cq_P w q) (Real.sin (cq_g w q u) -
      ((q : ℝ) + 1) / (q * cq_g w q u) * (1 - Real.cos (cq_g w q u))) u := by
  have hq0 : (q : ℝ) ≠ 0 := by positivity
  have h1 : HasDerivAt (fun u : ℝ => u ^ q) ((q : ℝ) * u ^ (q - 1)) u := hasDerivAt_pow q u
  have h2 : HasDerivAt (cq_g w q) (w ^ q * (-((q : ℝ) * u ^ (q - 1)) / (u ^ q) ^ 2)) u :=
    (h1.inv (by positivity)).const_mul (w ^ q)
  have h3 : HasDerivAt (fun u : ℝ => -(u ^ (q + 1) / (q * w ^ q)))
      (-((((q + 1 : ℕ) : ℝ) * u ^ (q + 1 - 1)) / (q * w ^ q))) u :=
    ((hasDerivAt_pow (q + 1) u).div_const _).neg
  have h4 := h3.mul ((h2.cos).const_sub 1)
  refine h4.congr_deriv ?_
  have e : u ^ q = u ^ (q - 1) * u := by rw [← pow_succ]; congr 1; omega
  unfold cq_g
  rw [Nat.add_sub_cancel, pow_succ u q, e]
  push_cast
  field_simp
  ring

/-- One step: `sin g(m)` dominates the increment of `P + g`, with a gain where `1 ≤ g ≤ 2`. -/
private lemma cq_step (w : ℝ) (hw : 0 < w) (q : ℕ) (hq : 2 ≤ q) (m : ℝ) (hm : 0 < m) (e : ℝ)
    (he : e = 0 ∨ (e = 1 / 5 ∧ cq_g w q m ≤ 2 ∧ 1 ≤ cq_g w q (m + 1))) :
    (cq_P w q (m + 1) + cq_g w q (m + 1)) - (cq_P w q m + cq_g w q m) + e ≤ Real.sin (cq_g w q m) := by
  obtain ⟨c, hc, hc2⟩ := exists_hasDerivAt_eq_slope (cq_P w q) _ (show m < m + 1 by linarith)
    (fun u hu => (cq_deriv w hw q hq u (lt_of_lt_of_le hm hu.1)).continuousAt.continuousWithinAt)
    (fun u hu => cq_deriv w hw q hq u (hm.trans hu.1))
  have hc0 : 0 < c := hm.trans hc.1
  have hgc := cq_g_pos w hw q c hc0
  have h1 := cq_g_anti w hw q m c hm hc.1.le
  have h2 := cq_g_anti w hw q c (m + 1) hc0 hc.2.le
  have e1 : m + 1 - m = 1 := by ring
  rw [e1, div_one] at hc2
  have hlip : Real.sin (cq_g w q c) - Real.sin (cq_g w q m) ≤ cq_g w q m - cq_g w q c := by
    have h := Real.abs_sin_sub_sin_le (cq_g w q c) (cq_g w q m)
    have h3 := le_abs_self (Real.sin (cq_g w q c) - Real.sin (cq_g w q m))
    rw [abs_sub_comm (cq_g w q c), abs_of_nonneg (sub_nonneg.mpr h1)] at h
    linarith
  have hgain : e ≤ ((q : ℝ) + 1) / (q * cq_g w q c) * (1 - Real.cos (cq_g w q c)) := by
    have hcos : 0 ≤ 1 - Real.cos (cq_g w q c) := by linarith [Real.cos_le_one (cq_g w q c)]
    have hq0 : (0 : ℝ) < q := by exact_mod_cast (by omega : 0 < q)
    rcases he with he | ⟨he, h3, h4⟩
    · rw [he]; positivity
    · have h5 : Real.cos (cq_g w q c) ≤ Real.cos 1 :=
        Real.cos_le_cos_of_nonneg_of_le_pi (by norm_num) (by linarith [Real.pi_gt_three]) (by linarith)
      have h6 := Real.cos_one_le
      have h7 : 1 / 2 ≤ ((q : ℝ) + 1) / (q * cq_g w q c) := by
        rw [div_le_div_iff₀ (by norm_num) (by positivity)]
        nlinarith
      rw [he]
      nlinarith
  linarith

private lemma cq_tele (F : ℕ → ℝ) (a b : ℕ) (hab : a ≤ b) :
    ∑ m ∈ Finset.Ico a b, (F (m + 1) - F m) = F b - F a := by
  induction b, hab using Nat.le_induction with
  | base => simp
  | succ b hb ih => rw [Finset.sum_Ico_succ_top hb, ih]; ring

private lemma cq_half (q : ℕ) (hq : 2 ≤ q) (y : ℝ) (hy : 0 ≤ y) (h : y ≤ 1 + 1 / (2 * (q : ℝ))) : y ^ q ≤ 2 := by
  have hq0 : (0 : ℝ) < q := by exact_mod_cast (by omega : 0 < q)
  have h1 : y ≤ Real.exp (1 / (2 * (q : ℝ))) := by
    linarith [Real.add_one_le_exp (1 / (2 * (q : ℝ)))]
  have h2 : y ^ q ≤ Real.exp (1 / (2 * (q : ℝ))) ^ q := pow_le_pow_left₀ hy h1 q
  rw [← Real.exp_nat_mul] at h2
  have e : (q : ℝ) * (1 / (2 * (q : ℝ))) = 1 / 2 := by field_simp
  rw [e] at h2
  have h3 : Real.exp (1 / 2) ≤ 2 := by
    have h4 : Real.exp (1 / 2) ^ (2 : ℕ) ≤ 2 ^ (2 : ℕ) := by
      rw [← Real.exp_nat_mul]
      have e2 : ((2 : ℕ) : ℝ) * (1 / 2) = 1 := by norm_num
      rw [e2]
      linarith [Real.exp_one_lt_d9]
    exact le_of_pow_le_pow_left₀ (by norm_num) (by norm_num) h4
  linarith

/-- The sum of `sin ((w/m)^q)` over `μ ≤ m < b` is almost nonnegative once `μ` is large. -/
private lemma cq_main (q : ℕ) (hq : 2 ≤ q) : ∃ μ : ℕ, 0 < μ ∧ ∀ (w : ℝ), 0 < w → ∀ b : ℕ, μ ≤ b → w ≤ b →
    -((b : ℝ) * cq_g w q b) ≤ ∑ m ∈ Finset.Ico μ b, Real.sin (cq_g w q m) := by
  have hqR : (2 : ℝ) ≤ q := by exact_mod_cast hq
  obtain ⟨c, hc⟩ : ∃ c : ℝ, c = 1 / (2 * (q : ℝ) + 1) := ⟨_, rfl⟩
  have hc0 : 0 < c := by rw [hc]; positivity
  have hc1 : c ≤ 1 / 5 := by
    rw [hc]; exact one_div_le_one_div_of_le (by norm_num) (by linarith)
  have hcq : c * (2 * (q : ℝ) + 1) = 1 := by rw [hc]; field_simp
  obtain ⟨B, hB⟩ : ∃ B : ℝ, B = (10 / c) ^ q := ⟨_, rfl⟩
  have hB0 : 0 < B := by rw [hB]; positivity
  refine ⟨⌈10 / c * (B + 2)⌉₊ + 1, by omega, ?_⟩
  set μ := ⌈10 / c * (B + 2)⌉₊ + 1 with hμ
  intro w hw b hb hwb
  have hμ1 : 10 / c * (B + 2) ≤ (μ : ℝ) := by
    rw [hμ]; push_cast
    linarith [Nat.le_ceil (10 / c * (B + 2))]
  have hμ0 : (0 : ℝ) < μ := by positivity
  have hbR : (0 : ℝ) < b := lt_of_lt_of_le hμ0 (by exact_mod_cast hb)
  have hgb := cq_g_pos w hw q b hbR
  have hfrac : ∀ u : ℝ, 0 < u → cq_g w q u = (w / u) ^ q := by
    intro u hu; unfold cq_g; rw [div_pow]; rfl
  by_cases hsmall : cq_g w q μ ≤ 3
  · have h0 : 0 ≤ ∑ m ∈ Finset.Ico μ b, Real.sin (cq_g w q m) := by
      apply Finset.sum_nonneg
      intro m hm
      have hm1 : (μ : ℝ) ≤ m := by exact_mod_cast (Finset.mem_Ico.mp hm).1
      have h1 := cq_g_anti w hw q μ m hμ0 hm1
      have h2 := cq_g_pos w hw q m (hμ0.trans_le hm1)
      exact Real.sin_nonneg_of_nonneg_of_le_pi h2.le (by linarith [Real.pi_gt_three])
    have : 0 ≤ (b : ℝ) * cq_g w q b := by positivity
    linarith
  rw [not_le] at hsmall
  have hμw : (μ : ℝ) ≤ w * (1 - c) := by
    by_contra hcon
    rw [not_le] at hcon
    have h1 : w / μ ≤ 1 + 1 / (2 * (q : ℝ)) := by
      rw [div_le_iff₀ hμ0]
      have e : (1 + 1 / (2 * (q : ℝ))) * (1 - c) = 1 := by rw [hc]; field_simp; ring
      have h2 : 0 < 1 + 1 / (2 * (q : ℝ)) := by positivity
      have h3 := mul_le_mul_of_nonneg_left hcon.le h2.le
      have h4 : (1 + 1 / (2 * (q : ℝ))) * (w * (1 - c)) = w := by
        calc _ = w * ((1 + 1 / (2 * (q : ℝ))) * (1 - c)) := by ring
          _ = w := by rw [e, mul_one]
      linarith
    have h2 := cq_half q hq (w / μ) (by positivity) h1
    rw [hfrac μ hμ0] at hsmall
    linarith
  have hwμ : (μ : ℝ) ≤ w := by nlinarith
  set P := ⌈w * (1 - c)⌉₊ with hP
  set Q := ⌊w⌋₊ with hQ
  set a := max μ ⌈c / 10 * w⌉₊ with ha
  have hwc : 0 ≤ w * (1 - c) := by nlinarith
  have haP : a ≤ P := by
    apply max_le
    · have := hμw.trans (Nat.le_ceil (w * (1 - c)))
      exact_mod_cast this
    · exact Nat.ceil_mono (by nlinarith)
  have hQw : (Q : ℝ) ≤ w := Nat.floor_le hw.le
  have hQb : Q ≤ b := by exact_mod_cast hQw.trans hwb
  have hPw : (P : ℝ) < w * (1 - c) + 1 := Nat.ceil_lt_add_one hwc
  have hQw2 : w < (Q : ℝ) + 1 := Nat.lt_floor_add_one w
  have hab : a ≤ b := by
    apply max_le hb
    have : (⌈c / 10 * w⌉₊ : ℕ) ≤ b := by
      rw [Nat.ceil_le]; nlinarith
    exact this
  have haμ : μ ≤ a := le_max_left _ _
  have haR : (0 : ℝ) < a := hμ0.trans_le (by exact_mod_cast haμ)
  have hga : cq_g w q a ≤ B := by
    rw [hfrac a haR, hB]
    apply pow_le_pow_left₀ (by positivity)
    have h1 : c / 10 * w ≤ a := (Nat.le_ceil _).trans (by exact_mod_cast le_max_right _ _)
    rw [div_le_div_iff₀ haR hc0]
    linarith
  have hcost : (a : ℝ) - μ ≤ c / 10 * w + 1 := by
    have h1 : (⌈c / 10 * w⌉₊ : ℝ) < c / 10 * w + 1 := Nat.ceil_lt_add_one (by positivity)
    rw [ha, Nat.cast_max]
    rcases le_total (μ : ℝ) (⌈c / 10 * w⌉₊ : ℝ) with h | h
    · rw [max_eq_right h]; linarith
    · rw [max_eq_left h]
      have : 0 < c / 10 * w := by positivity
      linarith
  have hfirst : -((a : ℝ) - μ) ≤ ∑ m ∈ Finset.Ico μ a, Real.sin (cq_g w q m) := by
    have h := Finset.card_nsmul_le_sum (Finset.Ico μ a) (fun m : ℕ => Real.sin (cq_g w q m)) (-1)
      (fun m _ => Real.neg_one_le_sin _)
    rw [Nat.card_Ico, nsmul_eq_mul, Nat.cast_sub haμ] at h
    linarith
  set F : ℕ → ℝ := fun m => cq_P w q m + cq_g w q m with hF
  have hsecond : F b - F a + ((Q : ℝ) - P) / 5 ≤ ∑ m ∈ Finset.Ico a b, Real.sin (cq_g w q m) := by
    have h1 : ∑ m ∈ Finset.Ico a b, ((F (m + 1) - F m) + (if P ≤ m ∧ m < Q then (1 / 5 : ℝ) else 0)) ≤
        ∑ m ∈ Finset.Ico a b, Real.sin (cq_g w q m) := by
      apply Finset.sum_le_sum
      intro m hm
      have hma : (a : ℝ) ≤ m := by exact_mod_cast (Finset.mem_Ico.mp hm).1
      have hm0 : (0 : ℝ) < m := haR.trans_le hma
      have h := cq_step w hw q hq m hm0 (if P ≤ m ∧ m < Q then (1 / 5 : ℝ) else 0) (by
        by_cases hc3 : P ≤ m ∧ m < Q
        · right
          rw [if_pos hc3]
          refine ⟨rfl, ?_, ?_⟩
          · rw [hfrac m hm0]
            apply cq_half q hq _ (by positivity)
            have h2 : w * (1 - c) ≤ m := (Nat.le_ceil _).trans (by exact_mod_cast hc3.1)
            rw [div_le_iff₀ hm0]
            have e : (1 + 1 / (2 * (q : ℝ))) * (1 - c) = 1 := by rw [hc]; field_simp; ring
            have h3 : 0 < 1 + 1 / (2 * (q : ℝ)) := by positivity
            have h4 := mul_le_mul_of_nonneg_left h2 h3.le
            have h5 : (1 + 1 / (2 * (q : ℝ))) * (w * (1 - c)) = w := by
              calc _ = w * ((1 + 1 / (2 * (q : ℝ))) * (1 - c)) := by ring
                _ = w := by rw [e, mul_one]
            linarith
          · rw [hfrac _ (by positivity)]
            apply one_le_pow₀
            rw [one_le_div (by positivity)]
            have : ((m + 1 : ℕ) : ℝ) ≤ Q := by exact_mod_cast hc3.2
            push_cast at this
            linarith
        · left; rw [if_neg hc3])
      simp only [hF]
      push_cast
      linarith
    rw [Finset.sum_add_distrib, cq_tele F a b hab, ← Finset.sum_filter, Finset.sum_const, nsmul_eq_mul] at h1
    have h2 : Finset.Ico P Q ⊆ (Finset.Ico a b).filter (fun m => P ≤ m ∧ m < Q) := by
      intro m hm
      rw [Finset.mem_Ico] at hm
      rw [Finset.mem_filter, Finset.mem_Ico]
      exact ⟨⟨haP.trans hm.1, hm.2.trans_le hQb⟩, hm⟩
    have h3 := Finset.card_le_card h2
    rw [Nat.card_Ico] at h3
    have h4 : (Q : ℝ) - P ≤ (((Finset.Ico a b).filter (fun m => P ≤ m ∧ m < Q)).card : ℝ) := by
      have h5 : Q ≤ ((Finset.Ico a b).filter (fun m => P ≤ m ∧ m < Q)).card + P := by omega
      have h6 : (Q : ℝ) ≤ (((Finset.Ico a b).filter (fun m => P ≤ m ∧ m < Q)).card : ℝ) + P := by exact_mod_cast h5
      linarith
    linarith
  have hFa : F a ≤ B := by
    have h1 : cq_P w q a ≤ 0 := by
      unfold cq_P
      have h2 : 0 ≤ 1 - Real.cos (cq_g w q a) := by linarith [Real.cos_le_one (cq_g w q a)]
      have h3 : 0 ≤ (a : ℝ) ^ (q + 1) / (q * w ^ q) := by positivity
      nlinarith
    simp only [hF]; linarith
  have hFb : -((b : ℝ) * cq_g w q b) ≤ F b := by
    have h1 : 1 - Real.cos (cq_g w q b) ≤ cq_g w q b ^ 2 / 2 := by
      linarith [Real.one_sub_sq_div_two_le_cos (x := cq_g w q b)]
    have h2 : 0 ≤ (b : ℝ) ^ (q + 1) / (q * w ^ q) := by positivity
    have h3 := mul_le_mul_of_nonneg_left h1 h2
    have h4 : (b : ℝ) ^ (q + 1) / (q * w ^ q) * (cq_g w q b ^ 2 / 2) = (b : ℝ) * cq_g w q b / (2 * q) := by
      unfold cq_g
      rw [pow_succ]
      field_simp
    have h5 : (b : ℝ) * cq_g w q b / (2 * q) ≤ (b : ℝ) * cq_g w q b :=
      div_le_self (by positivity) (by linarith)
    simp only [hF]
    unfold cq_P
    linarith
  rw [← Finset.sum_Ico_consecutive _ haμ hab]
  have hfin : B + 2 ≤ c / 10 * w := by
    have h1 : 10 / c * (B + 2) ≤ w := hμ1.trans hwμ
    have h2 := mul_le_mul_of_nonneg_left h1 (show 0 ≤ c / 10 by positivity)
    have e : c / 10 * (10 / c * (B + 2)) = B + 2 := by field_simp
    linarith
  linarith

private def cq_N (s μ q m : ℕ) : ℕ := (s * μ ^ q) / m ^ q

/-- For `0 < x ≤ π`: the sum of Dirichlet kernels is at least `-4b`. -/
private lemma cq_poly (q : ℕ) (hq : 2 ≤ q) (μ : ℕ) (hμ : 0 < μ)
    (hmain : ∀ (w : ℝ), 0 < w → ∀ b : ℕ, μ ≤ b → w ≤ b →
      -((b : ℝ) * cq_g w q b) ≤ ∑ m ∈ Finset.Ico μ b, Real.sin (cq_g w q m))
    (s b : ℕ) (hs : 0 < s) (hb : μ ≤ b) (hsb : 4 ^ q * (s * μ ^ q) ≤ b ^ q) (x : ℝ) (hx : 0 < x) (hxπ : x ≤ Real.pi) :
    -(4 * (b : ℝ)) ≤ ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * x) := by
  have hμR : (0 : ℝ) < μ := by exact_mod_cast hμ
  have hsR : (0 : ℝ) < s := by exact_mod_cast hs
  have hbR : (0 : ℝ) < b := hμR.trans_le (by exact_mod_cast hb)
  have hq0 : (q : ℝ) ≠ 0 := by positivity
  have hsbR : (4 : ℝ) ^ q * (s * (μ : ℝ) ^ q) ≤ (b : ℝ) ^ q := by exact_mod_cast hsb
  have hpi4 : x ≤ 4 := hxπ.trans Real.pi_le_four
  obtain ⟨z, hz0, hzq⟩ : ∃ z : ℝ, 0 < z ∧ z ^ q = s * x := by
    refine ⟨(s * x) ^ (1 / (q : ℝ)), by positivity, ?_⟩
    rw [← Real.rpow_natCast, ← Real.rpow_mul (by positivity), one_div_mul_cancel hq0, Real.rpow_one]
  have hwq : (z * μ) ^ q = s * x * (μ : ℝ) ^ q := by rw [mul_pow, hzq]
  have h4q : (4 : ℝ) ≤ 4 ^ q := by
    calc (4 : ℝ) = 4 ^ 1 := by norm_num
      _ ≤ 4 ^ q := pow_le_pow_right₀ (by norm_num) (by omega)
  have hwb : z * μ ≤ b := by
    apply le_of_pow_le_pow_left₀ (show q ≠ 0 by omega) hbR.le
    rw [hwq]
    calc (s : ℝ) * x * (μ : ℝ) ^ q = x * (s * (μ : ℝ) ^ q) := by ring
      _ ≤ 4 ^ q * (s * (μ : ℝ) ^ q) := mul_le_mul_of_nonneg_right (hpi4.trans h4q) (by positivity)
      _ ≤ (b : ℝ) ^ q := hsbR
  have h1 := hmain (z * μ) (by positivity) b hb hwb
  have hg : ∀ u : ℝ, cq_g (z * μ) q u = x * (s * (μ : ℝ) ^ q * (u ^ q)⁻¹) := by
    intro u; unfold cq_g; rw [hwq]; ring
  have htail : cq_g (z * μ) q b ≤ x := by
    rw [hg]
    have h2 : (s : ℝ) * (μ : ℝ) ^ q * ((b : ℝ) ^ q)⁻¹ ≤ 1 := by
      rw [← div_eq_mul_inv, div_le_one (by positivity)]
      have : 0 ≤ (s : ℝ) * (μ : ℝ) ^ q := by positivity
      nlinarith
    calc x * (s * (μ : ℝ) ^ q * ((b : ℝ) ^ q)⁻¹) ≤ x * 1 := mul_le_mul_of_nonneg_left h2 hx.le
      _ = x := mul_one x
  have hterm : ∀ m ∈ Finset.Ico μ b, Real.sin (cq_g (z * μ) q m) - x / 2 ≤
      Real.sin (((cq_N s μ q m : ℝ) + 1 / 2) * x) := by
    intro m hm
    have hm0 : 0 < m := hμ.trans_le (Finset.mem_Ico.mp hm).1
    have hmq : 0 < m ^ q := Nat.pow_pos hm0
    have hmqR : (0 : ℝ) < (m : ℝ) ^ q := by exact_mod_cast hmq
    set r : ℝ := s * (μ : ℝ) ^ q * ((m : ℝ) ^ q)⁻¹ with hr
    have hr' : r * (m : ℝ) ^ q = s * (μ : ℝ) ^ q := by rw [hr]; field_simp
    have hlo : (cq_N s μ q m : ℝ) ≤ r := by
      have h2 : cq_N s μ q m * m ^ q ≤ s * μ ^ q := Nat.div_mul_le_self _ _
      have h3 : (cq_N s μ q m : ℝ) * (m : ℝ) ^ q ≤ s * (μ : ℝ) ^ q := by exact_mod_cast h2
      rw [← hr'] at h3
      exact le_of_mul_le_mul_right h3 hmqR
    have hhi : r ≤ (cq_N s μ q m : ℝ) + 1 := by
      have h2 : s * μ ^ q < (cq_N s μ q m + 1) * m ^ q := by
        have h := Nat.lt_mul_div_succ (s * μ ^ q) hmq
        calc s * μ ^ q < m ^ q * (s * μ ^ q / m ^ q + 1) := h
          _ = (cq_N s μ q m + 1) * m ^ q := mul_comm _ _
      have h3 : (s : ℝ) * (μ : ℝ) ^ q < ((cq_N s μ q m : ℝ) + 1) * (m : ℝ) ^ q := by exact_mod_cast h2
      rw [← hr'] at h3
      exact (lt_of_mul_lt_mul_right h3 hmqR.le).le
    have h4 := Real.abs_sin_sub_sin_le (cq_g (z * μ) q m) (((cq_N s μ q m : ℝ) + 1 / 2) * x)
    have h5 := le_abs_self (Real.sin (cq_g (z * μ) q m) - Real.sin (((cq_N s μ q m : ℝ) + 1 / 2) * x))
    have h6 : |cq_g (z * μ) q m - ((cq_N s μ q m : ℝ) + 1 / 2) * x| ≤ x / 2 := by
      rw [hg, ← hr, abs_le]
      constructor <;> nlinarith
    linarith
  have hsum := Finset.sum_le_sum hterm
  rw [Finset.sum_sub_distrib, Finset.sum_const, Nat.card_Ico, nsmul_eq_mul, Nat.cast_sub hb] at hsum
  have hD : 2 * Real.sin (x / 2) * ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * x) =
      ∑ m ∈ Finset.Ico μ b, Real.sin (((cq_N s μ q m : ℝ) + 1 / 2) * x) - ((b : ℝ) - μ) * Real.sin (x / 2) := by
    rw [Finset.mul_sum, Finset.sum_congr rfl (fun m _ => cq_dirichlet (cq_N s μ q m) x),
      Finset.sum_sub_distrib, Finset.sum_const, Nat.card_Ico, nsmul_eq_mul, Nat.cast_sub hb]
  have hsin : x / 4 ≤ Real.sin (x / 2) := by
    have h2 := Real.mul_le_sin (show 0 ≤ x / 2 by linarith) (by linarith)
    have h3 : 1 / 4 ≤ 1 / Real.pi := one_div_le_one_div_of_le Real.pi_pos Real.pi_le_four
    have h4 : 2 / Real.pi * (x / 2) = 1 / Real.pi * x := by ring
    rw [h4] at h2
    nlinarith
  have hsin0 : 0 < Real.sin (x / 2) := by linarith
  set T := ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * x) with hT
  have h7 : (b : ℝ) * cq_g (z * μ) q b ≤ b * x := mul_le_mul_of_nonneg_left htail hbR.le
  have h8 : ((b : ℝ) - μ) * (x / 2) ≤ b * (x / 2) := mul_le_mul_of_nonneg_right (by linarith) (by linarith)
  have h9 : ((b : ℝ) - μ) * Real.sin (x / 2) ≤ b * Real.sin (x / 2) := mul_le_mul_of_nonneg_right (by linarith) hsin0.le
  have h10 : (b : ℝ) * (x / 4) ≤ b * Real.sin (x / 2) := mul_le_mul_of_nonneg_left hsin hbR.le
  have h11 : 0 ≤ 2 * Real.sin (x / 2) * (T + 4 * b) := by
    have e : 2 * Real.sin (x / 2) * (T + 4 * b) = 2 * Real.sin (x / 2) * T + 8 * (b * Real.sin (x / 2)) := by ring
    have h12 : 0 ≤ (b : ℝ) * x := by positivity
    rw [e, hD]
    linarith
  have h12 : 0 ≤ T + 4 * b := nonneg_of_mul_nonneg_right h11 (by linarith)
  linarith

private lemma cq_reduce (x : ℝ) : ∃ y : ℝ, 0 ≤ y ∧ y ≤ Real.pi ∧ ∀ k : ℕ, Real.cos ((k : ℝ) * x) = Real.cos ((k : ℝ) * y) := by
  refine ⟨Real.arccos (Real.cos x), Real.arccos_nonneg _, Real.arccos_le_pi _, fun k => ?_⟩
  have h : Real.cos (Real.arccos (Real.cos x)) = Real.cos x :=
    Real.cos_arccos (Real.neg_one_le_cos x) (Real.cos_le_one x)
  have h1 := Polynomial.Chebyshev.T_real_cos x (k : ℤ)
  have h2 := Polynomial.Chebyshev.T_real_cos (Real.arccos (Real.cos x)) (k : ℤ)
  rw [h] at h2
  push_cast at h1 h2
  rw [← h1, ← h2]

private theorem cos_q : ∀ (q : ℕ), (2 : ℕ) ≤ q → ∃ (C : ℕ), ∀ (ν ρ : ℕ), (1 : ℕ) ≤ ρ → ρ ≤ ν ^ q →
    ∃ (a : ℕ → ℕ), (∑ k ∈ Finset.Icc (1 : ℕ) ρ, a k = ρ) ∧
      ∀ (x : ℝ), -((↑C : ℝ) * (↑ν : ℝ)) ≤ ∑ k ∈ Finset.Icc (1 : ℕ) ρ, (↑(a k) : ℝ) * Real.cos ((↑k : ℝ) * x) := by
  intro q hq
  obtain ⟨μ, hμ, hmain⟩ := cq_main q hq
  refine ⟨20 * μ, ?_⟩
  intro ν ρ hρ hρν
  have hν : 0 < ν := by
    rcases Nat.eq_zero_or_pos ν with h | h
    · rw [h, zero_pow (by omega)] at hρν; omega
    · exact h
  obtain ⟨b, hbdef⟩ : ∃ b : ℕ, b = 4 * (μ * ν) := ⟨_, rfl⟩
  have hμν : μ ≤ μ * ν := Nat.le_mul_of_pos_right μ hν
  have hb : μ < b := by omega
  set R : ℕ → ℕ := fun s => ∑ m ∈ Finset.Ico μ b, cq_N s μ q m with hR
  have hRs : ∀ s, s ≤ R s := by
    intro s
    have h1 : cq_N s μ q μ = s := Nat.mul_div_cancel _ (Nat.pow_pos hμ)
    have h2 := Finset.single_le_sum (f := fun m => cq_N s μ q m) (fun i _ => Nat.zero_le _)
      (Finset.mem_Ico.mpr ⟨le_refl μ, hb⟩)
    simp only [hR]
    omega
  have hR0 : R 0 = 0 := by simp [hR, cq_N]
  have hstep : ∀ s, R (s + 1) ≤ R s + b := by
    intro s
    have h1 : ∀ m ∈ Finset.Ico μ b, cq_N (s + 1) μ q m ≤ cq_N s μ q m + 1 := by
      intro m hm
      have hmμ := (Finset.mem_Ico.mp hm).1
      have hmq : 0 < m ^ q := Nat.pow_pos (hμ.trans_le hmμ)
      have h2 : μ ^ q ≤ m ^ q := Nat.pow_le_pow_left hmμ q
      unfold cq_N
      rw [← Nat.add_div_right _ hmq]
      apply Nat.div_le_div_right
      rw [add_mul, one_mul]
      omega
    have h2 := Finset.sum_le_sum h1
    rw [Finset.sum_add_distrib, Finset.sum_const, Nat.card_Ico, smul_eq_mul, mul_one] at h2
    simp only [hR]
    omega
  have hex : ∃ s, ρ < R s := ⟨ρ + 1, (Nat.lt_succ_self ρ).trans_le (hRs (ρ + 1))⟩
  obtain ⟨s, hs1, hs2⟩ : ∃ s, R s ≤ ρ ∧ ρ < R (s + 1) := by
    have h1 := Nat.find_spec hex
    have h0 : Nat.find hex ≠ 0 := by
      intro h; rw [h, hR0] at h1; omega
    obtain ⟨s, hs⟩ := Nat.exists_eq_succ_of_ne_zero h0
    refine ⟨s, ?_, ?_⟩
    · exact not_lt.mp (Nat.find_min hex (by omega))
    · rw [hs] at h1; exact h1
  have hsρ : s ≤ ρ := (hRs s).trans hs1
  have hrem : ρ - R s ≤ b := by have := hstep s; omega
  have hNle : ∀ m ∈ Finset.Ico μ b, cq_N s μ q m ≤ ρ := fun m hm =>
    (Finset.single_le_sum (f := fun m => cq_N s μ q m) (fun i _ => Nat.zero_le _) hm).trans hs1
  have hfil : ∀ m ∈ Finset.Ico μ b, (Finset.Icc 1 ρ).filter (fun k => k ≤ cq_N s μ q m) = Finset.Icc 1 (cq_N s μ q m) := by
    intro m hm
    have := hNle m hm
    ext k
    simp only [Finset.mem_filter, Finset.mem_Icc]
    omega
  refine ⟨fun k => ((Finset.Ico μ b).filter (fun m => k ≤ cq_N s μ q m)).card + (if k = 1 then ρ - R s else 0), ?_, ?_⟩
  · rw [Finset.sum_add_distrib, Finset.sum_ite_eq' (Finset.Icc 1 ρ) 1 (fun _ => ρ - R s),
      if_pos (Finset.mem_Icc.mpr ⟨le_refl 1, hρ⟩)]
    have h1 : ∑ k ∈ Finset.Icc 1 ρ, ((Finset.Ico μ b).filter (fun m => k ≤ cq_N s μ q m)).card = R s := by
      simp only [Finset.card_filter]
      rw [Finset.sum_comm]
      simp only [hR]
      apply Finset.sum_congr rfl
      intro m hm
      rw [← Finset.card_filter, hfil m hm, Nat.card_Icc]
      omega
    rw [h1]; omega
  · intro x
    obtain ⟨y, hy0, hyπ, hxy⟩ := cq_reduce x
    have h1 : ∑ k ∈ Finset.Icc 1 ρ, ((((Finset.Ico μ b).filter (fun m => k ≤ cq_N s μ q m)).card +
        (if k = 1 then ρ - R s else 0) : ℕ) : ℝ) * Real.cos ((k : ℝ) * x) =
        ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * y) +
          ((ρ - R s : ℕ) : ℝ) * Real.cos y := by
      have h2 : ∀ k ∈ Finset.Icc 1 ρ, ((((Finset.Ico μ b).filter (fun m => k ≤ cq_N s μ q m)).card +
          (if k = 1 then ρ - R s else 0) : ℕ) : ℝ) * Real.cos ((k : ℝ) * x) =
          ∑ m ∈ Finset.Ico μ b, (if k ≤ cq_N s μ q m then Real.cos ((k : ℝ) * y) else 0) +
            (if k = 1 then ((ρ - R s : ℕ) : ℝ) * Real.cos y else 0) := by
        intro k _
        rw [hxy k, Nat.cast_add, add_mul, ← Finset.sum_filter, Finset.sum_const, nsmul_eq_mul]
        congr 1
        split_ifs with hk
        · rw [hk, Nat.cast_one, one_mul]
        · simp
      rw [Finset.sum_congr rfl h2, Finset.sum_add_distrib, Finset.sum_comm,
        Finset.sum_ite_eq' (Finset.Icc 1 ρ) 1 (fun _ => ((ρ - R s : ℕ) : ℝ) * Real.cos y),
        if_pos (Finset.mem_Icc.mpr ⟨le_refl 1, hρ⟩)]
      congr 1
      apply Finset.sum_congr rfl
      intro m hm
      rw [← Finset.sum_filter, hfil m hm]
    have h3 : -(b : ℝ) ≤ ((ρ - R s : ℕ) : ℝ) * Real.cos y := by
      have h4 : ((ρ - R s : ℕ) : ℝ) ≤ b := by exact_mod_cast hrem
      have h5 : (0 : ℝ) ≤ ((ρ - R s : ℕ) : ℝ) := Nat.cast_nonneg _
      nlinarith [Real.neg_one_le_cos y]
    have hbR : (0 : ℝ) ≤ b := Nat.cast_nonneg b
    have h6 : -(4 * (b : ℝ)) ≤ ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * y) := by
      have hnonneg : (∀ k : ℕ, Real.cos ((k : ℝ) * y) = 1) ∨ s = 0 →
          0 ≤ ∑ m ∈ Finset.Ico μ b, ∑ k ∈ Finset.Icc 1 (cq_N s μ q m), Real.cos ((k : ℝ) * y) := by
        rintro (h | h)
        · exact Finset.sum_nonneg fun m _ => Finset.sum_nonneg fun k _ => by rw [h k]; norm_num
        · have : ∀ m, cq_N s μ q m = 0 := by intro m; rw [h]; simp [cq_N]
          simp [this]
      rcases hy0.eq_or_lt with h | h
      · have := hnonneg (Or.inl fun k => by rw [← h]; simp)
        linarith
      · rcases Nat.eq_zero_or_pos s with hs0 | hs0
        · have := hnonneg (Or.inr hs0)
          linarith
        · refine cq_poly q hq μ hμ hmain s b hs0 hb.le ?_ y h hyπ
          calc 4 ^ q * (s * μ ^ q) ≤ 4 ^ q * (ν ^ q * μ ^ q) :=
                Nat.mul_le_mul_left _ (Nat.mul_le_mul_right _ (hsρ.trans hρν))
            _ = b ^ q := by rw [hbdef, mul_pow, mul_pow]; ring
    have h7 : (b : ℝ) * 5 = ((20 * μ : ℕ) : ℝ) * ν := by rw [hbdef]; push_cast; ring
    simp only
    rw [h1]
    linarith

private lemma ra_exp (q : ℕ) (hq : 2 ≤ q) : 1 / 2 ≤ 1 - 1 / (q : ℝ) := by
  have h : (2 : ℝ) ≤ q := by exact_mod_cast hq
  have : 1 / (q : ℝ) ≤ 1 / 2 := one_div_le_one_div_of_le (by norm_num) h
  linarith

/-- `n ↦ n^(-(1-1/q))` as a completely multiplicative function. -/
private noncomputable def ra_f (q : ℕ) (hq : 2 ≤ q) : ℕ →*₀ ℝ where
  toFun n := (n : ℝ) ^ (-(1 - 1 / (q : ℝ)))
  map_zero' := by
    rw [Nat.cast_zero]
    have := ra_exp q hq
    exact Real.zero_rpow (by linarith)
  map_one' := by simp
  map_mul' a b := by
    simp only [Nat.cast_mul]
    exact Real.mul_rpow (Nat.cast_nonneg a) (Nat.cast_nonneg b)

private lemma ra_f_apply (q : ℕ) (hq : 2 ≤ q) (n : ℕ) : ra_f q hq n = (n : ℝ) ^ (-(1 - 1 / (q : ℝ))) := rfl

private lemma ra_f_nonneg (q : ℕ) (hq : 2 ≤ q) (n : ℕ) : 0 ≤ ra_f q hq n := by
  rw [ra_f_apply]; positivity

private lemma ra_f_le (q : ℕ) (hq : 2 ≤ q) (p : ℕ) (hp : 2 ≤ p) : ra_f q hq p ≤ 3 / 4 := by
  rw [ra_f_apply]
  have h0 := ra_exp q hq
  have h1 : (p : ℝ) ^ (-(1 - 1 / (q : ℝ))) ≤ (2 : ℝ) ^ (-(1 - 1 / (q : ℝ))) :=
    Real.rpow_le_rpow_of_nonpos (by norm_num) (by exact_mod_cast hp) (by linarith)
  have h2 : (2 : ℝ) ^ (-(1 - 1 / (q : ℝ))) ≤ (2 : ℝ) ^ (-(1 / 2 : ℝ)) :=
    Real.rpow_le_rpow_of_exponent_le (by norm_num) (by linarith)
  have h3 : (2 : ℝ) ^ (-(1 / 2 : ℝ)) ≤ 3 / 4 := by
    have h4 : ((2 : ℝ) ^ (-(1 / 2 : ℝ))) ^ (2 : ℕ) ≤ (3 / 4 : ℝ) ^ (2 : ℕ) := by
      rw [← Real.rpow_natCast ((2 : ℝ) ^ (-(1 / 2 : ℝ))) 2, ← Real.rpow_mul (by norm_num)]
      have e : (-(1 / 2 : ℝ)) * ((2 : ℕ) : ℝ) = ((-1 : ℤ) : ℝ) := by norm_num
      rw [e, Real.rpow_intCast]
      norm_num
    exact le_of_pow_le_pow_left₀ (by norm_num) (by norm_num) h4
  exact h1.trans (h2.trans h3)

private lemma ra_euler (q : ℕ) (hq : 2 ≤ q) (m : ℕ) (hm : 0 < m) (D : Finset ℕ) (hD : ∀ d ∈ D, d ∣ m) :
    ∑ d ∈ D, ra_f q hq d ≤ ∏ p ∈ m.primeFactors, (1 - ra_f q hq p)⁻¹ := by
  have hs : ∀ {p : ℕ}, p.Prime → ‖ra_f q hq p‖ < 1 := by
    intro p hp
    rw [Real.norm_of_nonneg (ra_f_nonneg q hq p)]
    linarith [ra_f_le q hq p hp.two_le]
  have h := (EulerProduct.summable_and_hasSum_factoredNumbers_prod_filter_prime_geometric
    (f := (ra_f q hq).toMonoidHom) hs m.primeFactors).2
  have hfil : m.primeFactors.filter Nat.Prime = m.primeFactors :=
    Finset.filter_true_of_mem (fun p hp => Nat.prime_of_mem_primeFactors hp)
  rw [hfil] at h
  have hmem : ∀ d ∈ D, d ∈ Nat.factoredNumbers m.primeFactors := by
    intro d hd
    rw [Nat.mem_factoredNumbers_iff_primeFactors_subset]
    exact ⟨ne_zero_of_dvd_ne_zero hm.ne' (hD d hd), Nat.primeFactors_mono (hD d hd) hm.ne'⟩
  have h2 := sum_le_hasSum (D.subtype (fun d => d ∈ Nat.factoredNumbers m.primeFactors))
    (fun i _ => ra_f_nonneg q hq i) h
  rw [Finset.sum_subtype_of_mem (fun d => ra_f q hq d) hmem] at h2
  exact h2

private lemma ra_inv_le (y : ℝ) (h0 : 0 ≤ y) (h1 : y ≤ 3 / 4) : (1 - y)⁻¹ ≤ Real.exp (4 * y) := by
  have hpos : 0 < 1 - y := by linarith
  have h2 : (1 - y)⁻¹ ≤ 1 + 4 * y := by
    rw [inv_le_iff_one_le_mul₀ hpos]
    nlinarith
  have := Real.add_one_le_exp (4 * y)
  linarith

private lemma ra_prod (q : ℕ) (hq : 2 ≤ q) (m : ℕ) :
    ∏ p ∈ m.primeFactors, (1 - ra_f q hq p)⁻¹ ≤ Real.exp (4 * ∑ p ∈ m.primeFactors, ra_f q hq p) := by
  rw [Finset.mul_sum, Real.exp_sum]
  apply Finset.prod_le_prod
  · intro p hp
    have := ra_f_le q hq p (Nat.prime_of_mem_primeFactors hp).two_le
    exact inv_nonneg.mpr (by linarith)
  · intro p hp
    exact ra_inv_le _ (ra_f_nonneg q hq p) (ra_f_le q hq p (Nat.prime_of_mem_primeFactors hp).two_le)

private lemma ra_powdiff (a b : ℝ) (hb : 0 ≤ b) (hab : b ≤ a) (n : ℕ) :
    a ^ (n + 1) - b ^ (n + 1) ≤ ((n : ℝ) + 1) * a ^ n * (a - b) := by
  have ha : 0 ≤ a := hb.trans hab
  induction n with
  | zero => simp
  | succ n ih =>
    have h1 : b ^ (n + 1) ≤ a ^ (n + 1) := pow_le_pow_left₀ hb hab _
    have h2 := mul_le_mul_of_nonneg_left ih ha
    have h3 := mul_le_mul_of_nonneg_right h1 (sub_nonneg.mpr hab)
    have e1 : a ^ (n + 1 + 1) - b ^ (n + 1 + 1) = a * (a ^ (n + 1) - b ^ (n + 1)) + b ^ (n + 1) * (a - b) := by ring
    have e2 : (((n + 1 : ℕ) : ℝ) + 1) * a ^ (n + 1) * (a - b) =
        a * (((n : ℝ) + 1) * a ^ n * (a - b)) + a ^ (n + 1) * (a - b) := by push_cast; ring
    rw [e1, e2]
    linarith

/-- For `t > 0`: `(t^q)^(-(1-1/q)) = (t^(q-1))⁻¹`, written with `q = r + 1`. -/
private lemma ra_root (r : ℕ) (t : ℝ) (ht : 0 < t) :
    (t ^ (r + 1)) ^ (-(1 - 1 / ((r + 1 : ℕ) : ℝ))) = (t ^ r)⁻¹ := by
  have h : ((r : ℝ) + 1) ≠ 0 := by positivity
  rw [← Real.rpow_natCast t (r + 1), ← Real.rpow_mul ht.le, ← Real.rpow_natCast t r, ← Real.rpow_neg ht.le]
  congr 1
  push_cast
  field_simp
  ring

/-- `∑_{j=1}^N j^(-(1-1/q)) ≤ q N^(1/q)`. -/
private lemma ra_zeta (r : ℕ) (hq : 2 ≤ r + 1) (N : ℕ) :
    ∑ j ∈ Finset.Icc 1 N, ra_f (r + 1) hq j ≤ ((r : ℝ) + 1) * (N : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) := by
  induction N with
  | zero =>
    have : 0 ≤ ((r : ℝ) + 1) * ((0 : ℕ) : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) := by positivity
    simpa using this
  | succ N ih =>
    rw [Finset.sum_Icc_succ_top (by omega)]
    set a : ℝ := ((N + 1 : ℕ) : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) with ha
    set b : ℝ := (N : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) with hb
    have hb0 : 0 ≤ b := by positivity
    have ha0 : 0 < a := by positivity
    have haq : a ^ (r + 1) = ((N + 1 : ℕ) : ℝ) := by
      rw [ha, ← Real.rpow_natCast, ← Real.rpow_mul (by positivity), one_div_mul_cancel (by positivity), Real.rpow_one]
    have hbq : b ^ (r + 1) = (N : ℝ) := by
      rw [hb, ← Real.rpow_natCast, ← Real.rpow_mul (by positivity), one_div_mul_cancel (by positivity), Real.rpow_one]
    have hab : b ≤ a := Real.rpow_le_rpow (by positivity) (by push_cast; linarith) (by positivity)
    have hf : ra_f (r + 1) hq (N + 1) = (a ^ r)⁻¹ := by
      rw [ra_f_apply, ← haq]
      exact ra_root r a ha0
    have key : (a ^ r)⁻¹ ≤ ((r : ℝ) + 1) * (a - b) := by
      rw [inv_le_iff_one_le_mul₀ (by positivity)]
      have h := ra_powdiff a b hb0 hab r
      rw [haq, hbq] at h
      push_cast at h
      linarith
    rw [hf]
    linarith

/-- A set of at most `t^q` positive integers has `∑ s^(-(1-1/q)) ≤ (q+1) t`. -/
private lemma ra_sum (r : ℕ) (hq : 2 ≤ r + 1) (S : Finset ℕ) (t : ℝ) (ht : 0 < t) (hS : ∀ s ∈ S, 1 ≤ s)
    (hc : (S.card : ℝ) ≤ t ^ (r + 1)) : ∑ s ∈ S, ra_f (r + 1) hq s ≤ ((r : ℝ) + 2) * t := by
  set N : ℕ := ⌊t ^ (r + 1)⌋₊ with hN
  have hNle : (N : ℝ) ≤ t ^ (r + 1) := Nat.floor_le (by positivity)
  have hroot : (t ^ (r + 1)) ^ (1 / ((r + 1 : ℕ) : ℝ)) = t := by
    rw [← Real.rpow_natCast, ← Real.rpow_mul ht.le, mul_one_div_cancel (by positivity), Real.rpow_one]
  have hσ := ra_exp (r + 1) hq
  rw [← Finset.sum_filter_add_sum_filter_not S (fun s => s ≤ N)]
  have h1 : ∑ s ∈ S.filter (fun s => s ≤ N), ra_f (r + 1) hq s ≤ ((r : ℝ) + 1) * t := by
    calc ∑ s ∈ S.filter (fun s => s ≤ N), ra_f (r + 1) hq s ≤ ∑ j ∈ Finset.Icc 1 N, ra_f (r + 1) hq j := by
          apply Finset.sum_le_sum_of_subset_of_nonneg
          · intro s hs
            rw [Finset.mem_filter] at hs
            exact Finset.mem_Icc.mpr ⟨hS s hs.1, hs.2⟩
          · intro i _ _; exact ra_f_nonneg _ _ i
      _ ≤ ((r : ℝ) + 1) * (N : ℝ) ^ (1 / ((r + 1 : ℕ) : ℝ)) := ra_zeta r hq N
      _ ≤ ((r : ℝ) + 1) * (t ^ (r + 1)) ^ (1 / ((r + 1 : ℕ) : ℝ)) :=
          mul_le_mul_of_nonneg_left (Real.rpow_le_rpow (by positivity) hNle (by positivity)) (by positivity)
      _ = ((r : ℝ) + 1) * t := by rw [hroot]
  have h2 : ∑ s ∈ S.filter (fun s => ¬ s ≤ N), ra_f (r + 1) hq s ≤ t := by
    calc ∑ s ∈ S.filter (fun s => ¬ s ≤ N), ra_f (r + 1) hq s
        ≤ ∑ s ∈ S.filter (fun s => ¬ s ≤ N), (t ^ r)⁻¹ := by
          apply Finset.sum_le_sum
          intro s hs
          rw [Finset.mem_filter, not_le] at hs
          have hlt : t ^ (r + 1) < s := (Nat.floor_lt (by positivity)).mp hs.2
          rw [ra_f_apply, ← ra_root r t ht]
          exact Real.rpow_le_rpow_of_nonpos (by positivity) hlt.le (by linarith)
      _ = ((S.filter (fun s => ¬ s ≤ N)).card : ℝ) * (t ^ r)⁻¹ := by
          rw [Finset.sum_const, nsmul_eq_mul]
      _ ≤ t ^ (r + 1) * (t ^ r)⁻¹ := by
          apply mul_le_mul_of_nonneg_right _ (by positivity)
          refine le_trans ?_ hc
          exact_mod_cast Finset.card_filter_le _ _
      _ = t := by rw [pow_succ]; field_simp
  linarith

/-- `ω(m) ≤ 2 log m`. -/
private lemma ra_omega (m : ℕ) (hm : 0 < m) : (m.primeFactors.card : ℝ) ≤ 2 * Real.log m := by
  have h1 : 2 ^ m.primeFactors.card ≤ m := by
    calc 2 ^ m.primeFactors.card = ∏ _p ∈ m.primeFactors, 2 := by rw [Finset.prod_const]
      _ ≤ ∏ p ∈ m.primeFactors, p :=
          Finset.prod_le_prod' (fun p hp => (Nat.prime_of_mem_primeFactors hp).two_le)
      _ ≤ m := Nat.le_of_dvd hm (Nat.prod_primeFactors_dvd m)
  have h2 : Real.log ((2 : ℝ) ^ m.primeFactors.card) ≤ Real.log m :=
    Real.log_le_log (by positivity) (by exact_mod_cast h1)
  rw [Real.log_pow] at h2
  have h3 := Real.log_two_gt_d9
  have h4 : (0 : ℝ) ≤ m.primeFactors.card := Nat.cast_nonneg _
  nlinarith

private lemma ra_count (r : ℕ) (hq : 2 ≤ r + 1) (m k : ℕ) (hm : 0 < m) (hk : 0 < k) (t : ℝ) (ht : 0 < t)
    (hw : (m.primeFactors.card : ℝ) ≤ t ^ (r + 1)) :
    (Finset.card (Finset.filter (fun (d : ℕ) => d ∣ m) (Finset.Ioc k ((2 : ℕ) * k))) : ℝ)
      ≤ (2 * (k : ℝ)) ^ (1 - 1 / ((r + 1 : ℕ) : ℝ)) * Real.exp (4 * (((r : ℝ) + 2) * t)) := by
  set D := Finset.filter (fun (d : ℕ) => d ∣ m) (Finset.Ioc k ((2 : ℕ) * k)) with hDdef
  have hkR : (0 : ℝ) < k := by exact_mod_cast hk
  have hσ := ra_exp (r + 1) hq
  have h1 : (D.card : ℝ) * ((2 * (k : ℝ)) ^ (-(1 - 1 / ((r + 1 : ℕ) : ℝ)))) ≤ ∑ d ∈ D, ra_f (r + 1) hq d := by
    rw [← nsmul_eq_mul, ← Finset.sum_const]
    apply Finset.sum_le_sum
    intro d hd
    rw [hDdef, Finset.mem_filter, Finset.mem_Ioc] at hd
    rw [ra_f_apply]
    apply Real.rpow_le_rpow_of_nonpos _ _ (by linarith)
    · have : 0 < d := by omega
      exact_mod_cast this
    · exact_mod_cast hd.1.2
  have h2 := ra_euler (r + 1) hq m hm D (fun d hd => (Finset.mem_filter.mp hd).2)
  have h3 := ra_prod (r + 1) hq m
  have h4 := ra_sum r hq m.primeFactors t ht (fun p hp => (Nat.prime_of_mem_primeFactors hp).one_le) hw
  have h5 : Real.exp (4 * ∑ p ∈ m.primeFactors, ra_f (r + 1) hq p) ≤ Real.exp (4 * (((r : ℝ) + 2) * t)) :=
    Real.exp_le_exp.mpr (by linarith)
  have h6 := h1.trans (h2.trans (h3.trans h5))
  have hX : 0 < (2 * (k : ℝ)) ^ (1 - 1 / ((r + 1 : ℕ) : ℝ)) := by positivity
  rw [Real.rpow_neg (by positivity), ← div_eq_mul_inv, div_le_iff₀ hX] at h6
  linarith

private theorem h_divA : ∀ (A : ℕ), (1 : ℕ) ≤ A → ∃ (K₀ : ℕ), ∀ (m k : ℕ), K₀ ≤ k → (0 : ℕ) < m →
    Real.log (↑m : ℝ) ≤ Real.log (↑k : ℝ) ^ A →
    (Finset.card (Finset.filter (fun (d : ℕ) => d ∣ m) (Finset.Ioc k ((2 : ℕ) * k)))) ^ (A + (2 : ℕ)) < k ^ (A + (1 : ℕ)) := by
  intro A hA
  have hq : 2 ≤ A + 1 := by omega
  have hA1 : (1 : ℝ) ≤ A := by exact_mod_cast hA
  obtain ⟨C, hC⟩ : ∃ C : ℝ, C = 8 * ((A : ℝ) + 2) ^ (3 : ℕ) := ⟨_, rfl⟩
  have hC0 : 0 < C := by rw [hC]; positivity
  refine ⟨⌈Real.exp (2 * C ^ (A + 1) + 2 * ((A : ℝ) + 1) ^ (2 : ℕ) + 1)⌉₊, ?_⟩
  intro m k hk hm hlog
  have hk1 := Nat.le_of_ceil_le hk
  have hkR : (0 : ℝ) < k := lt_of_lt_of_le (Real.exp_pos _) hk1
  have hk0 : 0 < k := by exact_mod_cast hkR
  have hL := (Real.le_log_iff_exp_le hkR).mpr hk1
  set L := Real.log (k : ℝ) with hLdef
  have hCp : 0 < C ^ (A + 1) := by positivity
  have hAp : 0 < ((A : ℝ) + 1) ^ (2 : ℕ) := by positivity
  have hLpos : 0 < L := by linarith
  have hw : (m.primeFactors.card : ℝ) ≤ (L / C) ^ (A + 1) := by
    have h1 := ra_omega m hm
    have h4 : 0 < L ^ A := by positivity
    rw [div_pow, le_div_iff₀ hCp, pow_succ L A]
    have h5 : (m.primeFactors.card : ℝ) * C ^ (A + 1) ≤ (2 * L ^ A) * C ^ (A + 1) :=
      mul_le_mul_of_nonneg_right (by linarith) hCp.le
    have h6 : L ^ A * (2 * C ^ (A + 1)) ≤ L ^ A * L := mul_le_mul_of_nonneg_left (by linarith) h4.le
    linarith
  have hcount := ra_count A hq m k hm hk0 (L / C) (by positivity) hw
  set D := Finset.card (Finset.filter (fun (d : ℕ) => d ∣ m) (Finset.Ioc k ((2 : ℕ) * k))) with hDdef
  set σ : ℝ := 1 - 1 / ((A + 1 : ℕ) : ℝ) with hσdef
  have hσ : 1 / 2 ≤ σ := ra_exp (A + 1) hq
  have hkexp : (k : ℝ) = Real.exp L := (Real.exp_log hkR).symm
  have h2k : (2 * (k : ℝ)) ^ σ ≤ Real.exp (σ * (1 + L)) := by
    have h2 : 2 * (k : ℝ) ≤ Real.exp (1 + L) := by
      rw [Real.exp_add, ← hkexp]
      have := Real.add_one_le_exp (1 : ℝ)
      nlinarith
    calc (2 * (k : ℝ)) ^ σ ≤ (Real.exp (1 + L)) ^ σ :=
          Real.rpow_le_rpow (by positivity) h2 (by linarith)
      _ = Real.exp (σ * (1 + L)) := by rw [← Real.exp_mul, mul_comm]
  have hD : (D : ℝ) ≤ Real.exp (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C))) := by
    rw [Real.exp_add]
    exact hcount.trans (mul_le_mul_of_nonneg_right h2k (Real.exp_pos _).le)
  have hlt : ((A : ℝ) + 2) * (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C))) < ((A : ℝ) + 1) * L := by
    have e1 : 4 * (((A : ℝ) + 2) * (L / C)) = L / (2 * ((A : ℝ) + 2) ^ (2 : ℕ)) := by
      rw [hC]; field_simp; ring
    have e2 : ((A : ℝ) + 2) * σ = ((A : ℝ) + 1) - 1 / ((A : ℝ) + 1) := by
      rw [hσdef]; push_cast; field_simp; ring
    have e3 : ((A : ℝ) + 2) * (L / (2 * ((A : ℝ) + 2) ^ (2 : ℕ))) = L / (2 * ((A : ℝ) + 2)) := by
      field_simp
    have e4 : L / (2 * ((A : ℝ) + 2)) ≤ L / (2 * ((A : ℝ) + 1)) :=
      div_le_div_of_nonneg_left hLpos.le (by positivity) (by linarith)
    have e5 : L / (2 * ((A : ℝ) + 1)) = 1 / ((A : ℝ) + 1) * L / 2 := by field_simp
    have e6 : (A : ℝ) + 1 < 1 / ((A : ℝ) + 1) * L / 2 := by
      rw [← e5, lt_div_iff₀ (by positivity)]
      nlinarith
    have e7 : 0 < 1 / ((A : ℝ) + 1) := by positivity
    calc ((A : ℝ) + 2) * (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C)))
        = ((A : ℝ) + 2) * σ * (1 + L) + ((A : ℝ) + 2) * (L / (2 * ((A : ℝ) + 2) ^ (2 : ℕ))) := by rw [e1]; ring
      _ = ((A : ℝ) + 1 - 1 / ((A : ℝ) + 1)) * (1 + L) + L / (2 * ((A : ℝ) + 2)) := by rw [e2, e3]
      _ < ((A : ℝ) + 1) * L := by nlinarith
  have hfin : ((D ^ (A + 2) : ℕ) : ℝ) < ((k ^ (A + 1) : ℕ) : ℝ) := by
    push_cast
    calc (D : ℝ) ^ (A + 2) ≤ Real.exp (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C))) ^ (A + 2) :=
          pow_le_pow_left₀ (Nat.cast_nonneg _) hD _
      _ = Real.exp (((A : ℝ) + 2) * (σ * (1 + L) + 4 * (((A : ℝ) + 2) * (L / C)))) := by
          rw [← Real.exp_nat_mul]; push_cast; rfl
      _ < Real.exp (((A : ℝ) + 1) * L) := Real.exp_lt_exp.mpr hlt
      _ = (k : ℝ) ^ (A + 1) := by
          rw [hkexp, ← Real.exp_nat_mul]; push_cast; rfl
  exact_mod_cast hfin

private theorem t5_dense_lit : ∀ (A : ℕ), ∃ (u₀ K₀ : ℕ), ∀ (n k : ℕ), K₀ ≤ k → k ^ u₀ ≤ n →
      Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ A →
      k ^ (A + (1 : ℕ)) ≤ (Finset.card (Finset.filter (fun (d : ℕ) => n % d < k) (Finset.Ioc k ((2 : ℕ) * k)))) ^ (A + (2 : ℕ)) :=
  t5_dense (t5_cosq cos_q)

theorem erdos_1094__h2__h2__h5__h2__h2__h1__h1__h2 : ∀ (c : ℝ),
  (0 : ℝ) < c →
    ∀ (c₁ : ℝ),
      (0 : ℝ) < c₁ →
        ∃ (K : ℕ),
          ∀ (n k : ℕ),
            (0 : ℕ) < k →
              k * k * k ≤ n →
                Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) →
                  (n.choose k).minFac > n / k →
                    (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) →
                      ∏ m ∈ Finset.Ioc (n - k) n,
                            ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p =
                          k.factorial →
                        (∀ (m : ℕ),
                            n - k < m →
                              m ≤ n →
                                ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨
                                  ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) →
                          n / k ∣ (k - (1 : ℕ)).choose (n % k) →
                            n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k →
                              (∃ (x : ℕ),
                                  n - k < x ∧
                                    x ≤ n ∧
                                      x ≠ k * (n / k) ∧
                                        ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                          x) →
                                (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k →
                                  (∑ x ∈ Finset.Ioc (n - k) n with
                                          ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                            x,
                                          (1 : ℕ)) ^
                                        (10 : ℕ) <
                                      k →
                                    c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) <
                                        Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) →
                                      k ≤ K := by
  -- approach: Run certificates built from nonnegative cosine polynomials with a small constant term show that k^0.99 moduli of (k,2k] have a multiple in the window whenever log n ≤ log⁷k. Each divides a smooth element, which has under k^(8/9) divisors there, so there are at least k^0.1 smooth elements.
  -- licence: CC-BY-4.0
  -- label: under k^0.1 smooth elements, log n above log⁷k
  -- claim: $\forall c>0\ \forall c_1>0\ \exists K\ \forall n,k:\ 0<k,\ k^3\le n,\ \exp(c\log^2 k)<n,\ \operatorname{lpf}\binom nk>\lfloor n/k\rfloor,\ \bigl(\forall p \text{ prime},\ k<p\le\lfloor n/k\rfloor,\ \forall m\in(n-k,n],\ p\nmid m\bigr),\ \prod_{m=n-k+1}^{n} s_k(m) = k!,\ \bigl(\forall m\in(n-k,n]:\ s_k(m)=m \text{ or } s_k(m)<k\bigr),\ \lfloor n/k\rfloor\mid\binom{k-1}{n \bmod k},\ n<k\,2^{k-1}+k,\ \bigl(\exists x\in(n-k,n]:\ x\ne k\lfloor n/k\rfloor,\ s_k(x)=x\bigr),\ (n-k)^2<k\prod_{p\le k \text{ prime}}p^{\lfloor\log_p k\rfloor},\ N^{10}<k,\ c_1\log^4 k<\log n\,(\log\log k)^6,\ \log^7 k<\log n \Rightarrow k\le K,\quad N=\#\{x\in(n-k,n]:\ s_k(x)=x\}$
  -- Here $s_k(m)=\prod_{p\le k \text{ prime}}p^{v_p(m)}$ is the $k$-smooth part and $N$ the number of $k$-smooth elements of the window. The parent statement with one more hypothesis, the last: $\log n$ exceeds $\log^7 k$. In the Lean, `n / k` is the floor, `n % k` the remainder, `n - k` truncated subtraction (here $k\le n$), `Nat.log p k` is $\lfloor\log_p k\rfloor$, `minFac` the least prime factor, `Finset.Ioc (n - k) n` the window, and $N$ is written as a sum of ones.
  have h_few7 :
      ∀ (c : ℝ),
        (0 : ℝ) < c →
          ∀ (c₁ : ℝ),
            (0 : ℝ) < c₁ →
              ∃ (K : ℕ),
                ∀ (n k : ℕ),
                  (0 : ℕ) < k →
                    k * k * k ≤ n →
                      Real.exp (c * Real.log (↑k : ℝ) ^ (2 : ℕ)) < (↑n : ℝ) →
                        (n.choose k).minFac > n / k →
                          (∀ (p : ℕ), Nat.Prime p → k < p → p ≤ n / k → ∀ (m : ℕ), n - k < m → m ≤ n → ¬p ∣ m) →
                            ∏ m ∈ Finset.Ioc (n - k) n,
                                  ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p =
                                k.factorial →
                              (∀ (m : ℕ),
                                  n - k < m →
                                    m ≤ n →
                                      ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p = m ∨
                                        ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (m.factorization : ℕ → ℕ) p < k) →
                                n / k ∣ (k - (1 : ℕ)).choose (n % k) →
                                  n < k * (2 : ℕ) ^ (k - (1 : ℕ)) + k →
                                    (∃ (x : ℕ),
                                        n - k < x ∧
                                          x ≤ n ∧
                                            x ≠ k * (n / k) ∧
                                              ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                                x) →
                                      (n - k) * (n - k) < k * ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ Nat.log p k →
                                        (∑ x ∈ Finset.Ioc (n - k) n with
                                                ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p, p ^ (x.factorization : ℕ → ℕ) p =
                                                  x,
                                                (1 : ℕ)) ^
                                              (10 : ℕ) <
                                            k →
                                          c₁ * Real.log (↑k : ℝ) ^ (4 : ℕ) <
                                              Real.log (↑n : ℝ) * Real.log (Real.log (↑k : ℝ)) ^ (6 : ℕ) →
        Real.log (↑k : ℝ) ^ (7 : ℕ) < Real.log (↑n : ℝ) →
        k ≤ K := by
    sorry
  intro c hc c₁ hc₁
  obtain ⟨u₀, K₀, hdense⟩ := t5_dense_lit 98
  obtain ⟨K₀', hdiv⟩ := h_divA 7 (by norm_num)
  obtain ⟨KF, hKF⟩ := h_few7 c hc c₁ hc₁
  refine ⟨KF + K₀ + K₀' + ⌈Real.exp ((u₀ : ℝ) / c)⌉₊ + 2 + ⌈Real.exp 1⌉₊, ?_⟩
  intro n k hk h3 hexp hmin hfree hsmooth hsplit hD1 hbd hex hA hN10 hrange
  by_contra hcon
  have hgt := not_le.mp hcon
  have hk2 : 2 ≤ k := by omega
  have hsq : k * k ≤ n :=
    calc k * k = k * k * 1 := (Nat.mul_one _).symm
      _ ≤ k * k * k := Nat.mul_le_mul_left _ hk
      _ ≤ n := h3
  have hkn : k ≤ n := le_trans (Nat.le_mul_self k) hsq
  have hl1 : 1 ≤ Real.log (k : ℝ) := by
    have h1 : Real.exp 1 ≤ (k : ℝ) :=
      le_trans (Nat.le_ceil _) (by exact_mod_cast (show ⌈Real.exp 1⌉₊ ≤ k by omega))
    exact (Real.le_log_iff_exp_le (by exact_mod_cast hk)).mpr h1
  by_cases hnear : Real.log (↑n : ℝ) ≤ Real.log (↑k : ℝ) ^ (7 : ℕ)
  · have hbig : k ≤ (Finset.filter (fun (x : ℕ) => ∏ p ∈ Finset.Icc (1 : ℕ) k with Nat.Prime p,
        p ^ (x.factorization : ℕ → ℕ) p = x) (Finset.Ioc (n - k) n)).card ^ (10 : ℕ) := by
      have hu : k ^ u₀ ≤ n := t1_kpow c hc u₀ n k hk (by omega) hexp
      have hN := hdense n k (by omega) hu (le_trans hnear (pow_le_pow_right₀ hl1 (by norm_num)))
      refine many_count7 _ _ k ?_ ?_ hN hk
      · intro d hd
        rw [Finset.mem_filter, Finset.mem_Ioc] at hd
        have hdk : d * k ≤ n :=
          calc d * k ≤ (2 * k) * k := Nat.mul_le_mul_right k hd.1.2
            _ ≤ (k * k) * k := Nat.mul_le_mul_right k (Nat.mul_le_mul_right k hk2)
            _ ≤ n := h3
        have hlt := many_landing n k hk hkn hfree d hd.1.1 ((Nat.le_div_iff_mul_le hk).mpr hdk) hd.2
        have hmle : n % d ≤ n := Nat.mod_le n d
        have hx1 : n - k < n - n % d := by omega
        have hx2 : n - n % d ≤ n := Nat.sub_le _ _
        refine ⟨n - n % d, Finset.mem_filter.mpr ⟨Finset.mem_Ioc.mpr ⟨hx1, hx2⟩, ?_⟩, Nat.dvd_sub_mod n⟩
        rcases hsplit (n - n % d) hx1 hx2 with h | h
        · exact h
        · omega
      · intro x hx
        have hxw := Finset.mem_Ioc.mp (Finset.mem_filter.mp hx).1
        have hx0 : 0 < x := by omega
        have hlog : Real.log ((x : ℕ) : ℝ) ≤ Real.log (n : ℝ) :=
          Real.log_le_log (by exact_mod_cast hx0) (by exact_mod_cast hxw.2)
        refine lt_of_le_of_lt (Nat.pow_le_pow_left (Finset.card_le_card ?_) 9)
          (hdiv x k (by omega) hx0 (le_trans hlog hnear))
        exact Finset.filter_subset_filter _ (Finset.filter_subset _ _)
    rw [Finset.card_eq_sum_ones] at hbig
    exact absurd hN10 (not_lt.mpr hbig)
  · have := hKF n k hk h3 hexp hmin hfree hsmooth hsplit hD1 hbd hex hA hN10 hrange (not_le.mp hnear)
    omega
How to read this page