Open Proof Network

Problems / erdos-257 / variant-6b364d2b

the case A = primes (Tao–Teräväinen)

variant-6b364d2b

open

Restated as hole h_primes of erdos-257 (split by 20261023T133841Z-t1023-f1-partial.lean).

$\displaystyle \sum_{p \text{ prime}}\frac{1}{2^p-1} \ \text{ is irrational}.$

The case $A = \{\text{primes}\}$ of the root. The sum is Lean's tsum over the subtype $\{p : p \text{ prime}\}$, the ordinary sum of a convergent positive series; $0$ and $1$ are not prime, so no junk value $\frac10$ occurs.

unverified · history

Lean
theorem Opn.erdos_257_primes :
    Irrational (∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1)) := 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 →

How it is being proved

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

Split into

Proved from this hole by 20261022T160309Z-t1022-l-partial.lean (merged in f0822873b58e, attestation 000948).

  • h1 → variant-6b364d2b--h1 (open)

    $\displaystyle \forall q \ge 1\ \exists c_0 > 0\ \forall \delta > 0:\ \text{a prime cube } p_\varepsilon = p_0 + \textstyle\sum_k \varepsilon_k v_k, \text{ a finite set of } n \text{ obeying (5.5)--(5.6), a split } S_0 \sqcup S_1 \sqcup S_2 \text{ of the primes} \le B \text{ with } X_p \text{ constant on } S_0,\ |X_p| \le \delta \text{ on } S_1,\ M \text{ even},\ \kappa_1, \dots, \kappa_5 \le \delta,\ \sum_{p \in S_1} \operatorname{Var} X_p \ge c_0$

Read the partial proof (Lean)
private noncomputable def lamG (A : Set ℕ) [DecidablePred (· ∈ A)] (p : ℕ × ℕ) : ℝ :=
  if p.2 ∈ A ∧ p.2 ∣ p.1 ∧ p.1 ≠ 0 then (1 / 2 : ℝ) ^ p.1 else 0

private lemma lamG_nonneg (A : Set ℕ) [DecidablePred (· ∈ A)] (p : ℕ × ℕ) : 0 ≤ lamG A p := by
  unfold lamG; split_ifs <;> positivity

private lemma lamG_row (A : Set ℕ) [DecidablePred (· ∈ A)] (m : ℕ) :
    ∑' a, lamG A (m, a) = (((Nat.divisors m).filter (· ∈ A)).card : ℝ) * (1 / 2 : ℝ) ^ m := by
  rw [tsum_eq_sum (s := (Nat.divisors m).filter (· ∈ A))]
  · rw [Finset.card_eq_sum_ones, Nat.cast_sum, Finset.sum_mul]
    refine Finset.sum_congr rfl fun a ha => ?_
    simp only [Finset.mem_filter, Nat.mem_divisors] at ha
    simp [lamG, ha.1.1, ha.1.2, ha.2]
  · intro a ha
    simp only [Finset.mem_filter, Nat.mem_divisors, not_and] at ha
    unfold lamG
    rw [if_neg]
    rintro ⟨h1, h2, h3⟩
    exact ha ⟨h2, h3⟩ h1

private lemma lamG_row_summable (A : Set ℕ) [DecidablePred (· ∈ A)] (m : ℕ) : Summable fun a => lamG A (m, a) := by
  apply summable_of_ne_finset_zero (s := (Nat.divisors m).filter (· ∈ A))
  intro a ha
  simp only [Finset.mem_filter, Nat.mem_divisors, not_and] at ha
  unfold lamG
  rw [if_neg]
  rintro ⟨h1, h2, h3⟩
  exact ha ⟨h2, h3⟩ h1

private lemma lamG_summable (A : Set ℕ) [DecidablePred (· ∈ A)] : Summable (lamG A) := by
  rw [summable_prod_of_nonneg (lamG_nonneg A)]
  refine ⟨lamG_row_summable A, ?_⟩
  simp_rw [lamG_row]
  have hg : Summable fun m : ℕ => ((m : ℝ) ^ 1) * (1 / 2 : ℝ) ^ m :=
    summable_pow_mul_geometric_of_norm_lt_one 1 (by norm_num)
  refine Summable.of_nonneg_of_le (fun m => by positivity) (fun m => ?_) hg
  gcongr
  rw [pow_one]
  exact_mod_cast (Finset.card_filter_le _ _).trans (Nat.card_divisors_le_self m)

private lemma lamG_col (A : Set ℕ) [DecidablePred (· ∈ A)] (a : ℕ) :
    ∑' m, lamG A (m, a) = if a ∈ A then (1 : ℝ) / (2 ^ a - 1) else 0 := by
  by_cases ha : a ∈ A
  swap
  · simp [lamG, ha]
  rw [if_pos ha]
  rcases Nat.eq_zero_or_pos a with h0 | hpos
  · subst h0; simp [lamG]
  have hinj : Function.Injective fun k : ℕ => a * (k + 1) := by
    intro x y h; simp only at h
    have := Nat.eq_of_mul_eq_mul_left hpos h; omega
  have hsupp : Function.support (fun m => lamG A (m, a)) ⊆ Set.range fun k : ℕ => a * (k + 1) := by
    intro m hm
    simp only [Function.mem_support, lamG, ne_eq, ite_eq_right_iff, Classical.not_imp] at hm
    obtain ⟨⟨_, ⟨c, hc⟩, hm0⟩, _⟩ := hm
    refine ⟨c - 1, ?_⟩
    have : c ≠ 0 := by rintro rfl; simp at hc; exact hm0 hc
    simp only; rw [hc]; congr 1; omega
  rw [← hinj.tsum_eq hsupp]
  have hterm : ∀ k : ℕ, lamG A (a * (k + 1), a) = (1 / 2 : ℝ) ^ a * ((1 / 2 : ℝ) ^ a) ^ k := by
    intro k
    have : a * (k + 1) ≠ 0 := by positivity
    simp only [lamG, ha, dvd_mul_right, ne_eq, this, not_false_eq_true, and_self, if_true]
    rw [← pow_mul, ← pow_add]; congr 1; ring
  simp_rw [hterm]
  rw [tsum_mul_left, tsum_geometric_of_lt_one (by positivity)
    (pow_lt_one₀ (by norm_num) (by norm_num) hpos.ne')]
  have h2 : (1 : ℝ) < 2 ^ a := one_lt_pow₀ (by norm_num) hpos.ne'
  have h2' : (2 : ℝ) ^ a - 1 ≠ 0 := by linarith
  rw [one_div_pow]
  field_simp

/-- **Lambert identity.** `∑_{a ∈ A} 1/(2^a - 1) = ∑_m #{d ∣ m : d ∈ A} / 2^m`. (In Lean
`1/(2^0 - 1) = 0` and `Nat.divisors 0 = ∅`, so `0 ∈ A` changes neither side.) -/
private theorem lambert_identity (A : Set ℕ) [DecidablePred (· ∈ A)] :
    ∑' a : A, (1 : ℝ) / (2 ^ a.1 - 1) =
      ∑' m : ℕ, ((((Nat.divisors m).filter (· ∈ A)).card : ℕ) : ℝ) / 2 ^ m := by
  have hs := lamG_summable A
  have hrows : ∑' m : ℕ, ((((Nat.divisors m).filter (· ∈ A)).card : ℕ) : ℝ) / 2 ^ m
      = ∑' m, ∑' a, lamG A (m, a) := by
    refine tsum_congr fun m => ?_
    rw [lamG_row, one_div, inv_pow, div_eq_mul_inv]
  have hcols : ∑' a : A, (1 : ℝ) / (2 ^ a.1 - 1) = ∑' a, ∑' m, lamG A (m, a) := by
    rw [tsum_subtype A (fun a => (1 : ℝ) / (2 ^ a - 1))]
    refine tsum_congr fun a => ?_
    rw [lamG_col]; simp [Set.indicator]
  rw [hrows, hcols]
  have hu : Summable (Function.uncurry fun m a => lamG A (m, a)) := hs
  exact hu.tsum_comm

private lemma omega_eq_card_filter (n : ℕ) :
    ArithmeticFunction.cardDistinctFactors n = ((Nat.divisors n).filter (· ∈ {p : ℕ | p.Prime})).card := by
  rw [ArithmeticFunction.cardDistinctFactors_apply, ← List.card_toFinset]
  congr 1
  ext p
  simp only [Finset.mem_filter, Nat.mem_divisors, Set.mem_setOf_eq, List.mem_toFinset,
    Nat.mem_primeFactorsList']
  tauto

/-- Equation (1.7) of Tao–Teräväinen: `∑_p 1/(2^p - 1) = ∑_n ω(n)/2^n`. -/
private theorem lambert_primes :
    ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) =
      ∑' n : ℕ, (ArithmeticFunction.cardDistinctFactors n : ℝ) / 2 ^ n := by
  rw [lambert_identity]
  refine tsum_congr fun n => ?_
  rw [omega_eq_card_filter]

private lemma omega_div_summable :
    Summable fun n : ℕ => (ArithmeticFunction.cardDistinctFactors n : ℝ) / 2 ^ n := by
  have hg : Summable fun m : ℕ => ((m : ℝ) ^ 1) * (1 / 2 : ℝ) ^ m :=
    summable_pow_mul_geometric_of_norm_lt_one 1 (by norm_num)
  refine Summable.of_nonneg_of_le (fun m => by positivity) (fun m => ?_) hg
  rw [pow_one, one_div_pow, div_eq_mul_one_div]
  gcongr
  rw [omega_eq_card_filter]
  exact_mod_cast (Finset.card_filter_le _ _).trans (Nat.card_divisors_le_self m)


/-- Elementary step (2) of the primes route (see postmortem #894): if `∑ f(n)/2^n = a/b`, then
for every `N` the binary tail `∑_{k ≥ 0} f(N+1+k)/2^(k+1)` times `b` is an integer. With
`f = ω` this is the reduction Tao–Teräväinen's argument starts from. -/
private theorem tail_mul_den_int (f : ℕ → ℕ) (hs : Summable (fun n => (f n : ℝ) / 2 ^ n))
    (a : ℤ) (b : ℕ) (hb : 0 < b) (h : ∑' n, (f n : ℝ) / 2 ^ n = a / b) (N : ℕ) :
    ∃ z : ℤ, (b : ℝ) * ∑' k, (f (k + (N + 1)) : ℝ) / 2 ^ (k + 1) = z := by
  have hsplit := hs.sum_add_tsum_nat_add (N + 1)
  have hfin : ∀ n ∈ Finset.range (N + 1), (2 : ℝ) ^ N * ((f n : ℝ) / 2 ^ n) = ((f n * 2 ^ (N - n) : ℕ) : ℝ) := by
    intro n hn
    have hn' : n ≤ N := Nat.lt_succ_iff.mp (Finset.mem_range.mp hn)
    push_cast
    have : (2 : ℝ) ^ N = 2 ^ (N - n) * 2 ^ n := by rw [← pow_add, Nat.sub_add_cancel hn']
    rw [this]; field_simp
  have htail : ∀ k, (2 : ℝ) ^ N * ((f (k + (N + 1)) : ℝ) / 2 ^ (k + (N + 1))) = (f (k + (N + 1)) : ℝ) / 2 ^ (k + 1) := by
    intro k
    have : (2 : ℝ) ^ (k + (N + 1)) = 2 ^ (k + 1) * 2 ^ N := by rw [← pow_add]; ring_nf
    rw [this]; field_simp
  have hI : (2 : ℝ) ^ N * ∑ n ∈ Finset.range (N + 1), (f n : ℝ) / 2 ^ n
      = ((∑ n ∈ Finset.range (N + 1), f n * 2 ^ (N - n) : ℕ) : ℝ) := by
    rw [Finset.mul_sum, Nat.cast_sum]; exact Finset.sum_congr rfl hfin
  have hT : (2 : ℝ) ^ N * ∑' k, (f (k + (N + 1)) : ℝ) / 2 ^ (k + (N + 1))
      = ∑' k, (f (k + (N + 1)) : ℝ) / 2 ^ (k + 1) := by
    rw [← tsum_mul_left]; exact tsum_congr htail
  refine ⟨2 ^ N * a - b * ((∑ n ∈ Finset.range (N + 1), f n * 2 ^ (N - n) : ℕ) : ℤ), ?_⟩
  have hb' : (b : ℝ) ≠ 0 := by exact_mod_cast hb.ne'
  rw [← hT]
  have key : (2 : ℝ) ^ N * (a / b) = ((∑ n ∈ Finset.range (N + 1), f n * 2 ^ (N - n) : ℕ) : ℝ)
      + 2 ^ N * ∑' k, (f (k + (N + 1)) : ℝ) / 2 ^ (k + (N + 1)) := by
    rw [← h, ← hsplit, mul_add, hI]
  push_cast at key ⊢
  have : (b : ℝ) * (2 ^ N * (a / b)) = 2 ^ N * a := by field_simp
  linear_combination this - (b : ℝ) * key

/-- Equation (5.1) of Tao–Teräväinen from rationality: if `∑_p 1/(2^p-1) = a/q`, then for every
`N`, `q · ∑_{h ≥ 1} ω(N+h)/2^h` is an integer. -/
private theorem eq_5_1 (a : ℤ) (q : ℕ) (hq : 0 < q)
    (h : ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) = a / q) (N : ℕ) :
    ∃ z : ℤ, (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (k + (N + 1)) : ℝ) / 2 ^ (k + 1) = z :=
  tail_mul_den_int (fun n => ArithmeticFunction.cardDistinctFactors n) omega_div_summable a q hq
    (by rw [← lambert_primes]; exact h) N


private lemma omega_eq_card_pf (n : ℕ) : ArithmeticFunction.cardDistinctFactors n = n.primeFactors.card := by
  rw [ArithmeticFunction.cardDistinctFactors_apply, ← List.card_toFinset]; rfl

/-- additivity used in (5.2): `ω(p k) + 1_{p ∣ k} = ω(k) + 1` for a prime `p` and `k ≠ 0`. -/
private lemma omega_mul_prime {p k : ℕ} (hp : p.Prime) (hk : k ≠ 0) :
    ArithmeticFunction.cardDistinctFactors (p * k) + (if p ∣ k then 1 else 0)
      = ArithmeticFunction.cardDistinctFactors k + 1 := by
  rw [omega_eq_card_pf, omega_eq_card_pf, Nat.primeFactors_mul hp.ne_zero hk, hp.primeFactors]
  by_cases h : p ∣ k
  · have hm : p ∈ k.primeFactors := Nat.mem_primeFactors.mpr ⟨hp, h, hk⟩
    rw [if_pos h, Finset.singleton_union, Finset.insert_eq_of_mem hm]
  · have hm : p ∉ k.primeFactors := fun hm => h (Nat.mem_primeFactors.mp hm).2.1
    rw [if_neg h, Finset.singleton_union, Finset.card_insert_of_notMem hm]

private lemma omega_shift_summable (c : ℕ) :
    Summable fun k : ℕ => (ArithmeticFunction.cardDistinctFactors (k + c) : ℝ) / 2 ^ (k + 1) := by
  have h1 : Summable fun k : ℕ => ((k : ℝ) ^ 1) * (1 / 2 : ℝ) ^ k :=
    summable_pow_mul_geometric_of_norm_lt_one 1 (by norm_num)
  have h2 : Summable fun k : ℕ => (c : ℝ) * (1 / 2 : ℝ) ^ k := summable_geometric_two.mul_left _
  refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) (h1.add h2)
  have hw : (ArithmeticFunction.cardDistinctFactors (k + c) : ℝ) ≤ k + c := by
    rw [omega_eq_card_filter]
    exact_mod_cast (Finset.card_filter_le _ _).trans (Nat.card_divisors_le_self _)
  have hpos : (0 : ℝ) < 2 ^ k := by positivity
  rw [div_le_iff₀ (by positivity), pow_one, one_div_pow]
  have : ((k : ℝ) * (1 / 2 ^ k) + c * (1 / 2 ^ k)) * 2 ^ (k + 1) = 2 * (k + c) := by
    rw [pow_succ]; field_simp
  rw [this]
  have : (0 : ℝ) ≤ k + c := by positivity
  linarith

private lemma ind_summable (f : ℕ → Prop) [DecidablePred f] :
    Summable fun k : ℕ => (if f k then (1 : ℝ) else 0) / 2 ^ (k + 1) := by
  refine Summable.of_nonneg_of_le (fun k => by split_ifs <;> positivity) (fun k => ?_) summable_geometric_two
  rw [one_div_pow]
  have : (0 : ℝ) < 2 ^ k := by positivity
  split_ifs
  · rw [div_le_div_iff_of_pos_left one_pos (by positivity) this]; rw [pow_succ]; linarith
  · simp

/-- Equation (5.2)–(5.3) of Tao–Teräväinen: from the tail congruences (5.1) at `m`, for a prime `p`
and `n = p m`: `q ∑_{h ≥ 1} ω(n + p h)/2^h + q δ_p(n)` is an integer, where
`δ_p(n) = ∑_{h ≥ 1} 1_{p² ∣ n + p h}/2^h`. -/
private theorem eq_5_2 (q : ℕ)
    (hint : ∀ N : ℕ, ∃ z : ℤ,
      (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (k + (N + 1)) : ℝ) / 2 ^ (k + 1) = z)
    {p : ℕ} (hp : p.Prime) (m : ℕ) :
    ∃ z : ℤ, (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (p * m + p * (k + 1)) : ℝ) / 2 ^ (k + 1)
      + (q : ℝ) * ∑' k, (if p ^ 2 ∣ p * m + p * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) = z := by
  obtain ⟨z, hz⟩ := hint m
  refine ⟨z + q, ?_⟩
  have hterm : ∀ k : ℕ,
      (ArithmeticFunction.cardDistinctFactors (p * m + p * (k + 1)) : ℝ) / 2 ^ (k + 1)
      + (if p ^ 2 ∣ p * m + p * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)
      = (ArithmeticFunction.cardDistinctFactors (k + (m + 1)) : ℝ) / 2 ^ (k + 1) + (1 / 2 : ℝ) ^ (k + 1) := by
    intro k
    have he : p * m + p * (k + 1) = p * (k + (m + 1)) := by ring
    have hd : p ^ 2 ∣ p * (k + (m + 1)) ↔ p ∣ k + (m + 1) := by
      rw [pow_two]; exact Nat.mul_dvd_mul_iff_left hp.pos
    have key := omega_mul_prime hp (show k + (m + 1) ≠ 0 by omega)
    rw [he]
    have key' : (ArithmeticFunction.cardDistinctFactors (p * (k + (m + 1))) : ℝ)
        + (if p ^ 2 ∣ p * (k + (m + 1)) then (1 : ℝ) else 0)
        = (ArithmeticFunction.cardDistinctFactors (k + (m + 1)) : ℝ) + 1 := by
      rw [if_congr hd rfl rfl]
      have := congrArg (fun t : ℕ => (t : ℝ)) key
      simp only [Nat.cast_add, Nat.cast_ite, Nat.cast_one, Nat.cast_zero] at this
      exact this
    rw [one_div_pow, ← add_div, key', add_div]
  have hG : Summable fun k : ℕ => (1 / 2 : ℝ) ^ (k + 1) :=
    (summable_nat_add_iff 1).mpr summable_geometric_two
  have hsA : Summable fun k : ℕ =>
      (ArithmeticFunction.cardDistinctFactors (p * m + p * (k + 1)) : ℝ) / 2 ^ (k + 1) := by
    refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_)
      ((omega_shift_summable (m + 1)).add hG)
    have := hterm k
    have h0 : (0 : ℝ) ≤ (if p ^ 2 ∣ p * m + p * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) := by
      split_ifs <;> positivity
    linarith
  have hsB := ind_summable (fun k => p ^ 2 ∣ p * m + p * (k + 1))
  rw [← mul_add, ← hsA.tsum_add hsB, tsum_congr hterm,
    (omega_shift_summable (m + 1)).tsum_add hG]
  have hg : ∑' k : ℕ, (1 / 2 : ℝ) ^ (k + 1) = 1 := by
    simp_rw [pow_succ]; rw [tsum_mul_right, tsum_geometric_two]; norm_num
  rw [hg, mul_add, hz]; push_cast; ring

/-- `(-1)^{|ε|}` for `ε ∈ {0,1}^K`, as a product of signs. -/
private noncomputable def cubeSgn {K : ℕ} (ε : Fin K → Bool) : ℝ := ∏ i, if ε i then (-1 : ℝ) else 1

private lemma cubeSgn_eq {K : ℕ} (ε : Fin K → Bool) :
    cubeSgn ε = (-1 : ℝ) ^ (Finset.univ.filter fun i => ε i = true).card := by
  unfold cubeSgn
  rw [Finset.prod_ite]; simp

/-- The shifts (5.7): `r_{ε,h} = p₀ h + ∑_k (h - k) ε_k v_k` (coordinates `k = 1, …, K` are
`Fin K` indices `k - 1`). -/
private def cubeShift {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (ε : Fin K → Bool) (h : ℕ) : ℤ :=
  p0 * h + ∑ k : Fin K, ((h : ℤ) - (k.1 + 1)) * (if ε k then v k else 0)

/-- (5.7): `r_{ε,h} = p_ε h - ∑_k k ε_k v_k` with `p_ε = p₀ + ∑_k ε_k v_k`. -/
private lemma cubeShift_eq {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (ε : Fin K → Bool) (h : ℕ) :
    cubeShift p0 v ε h = (p0 + ∑ k : Fin K, (if ε k then v k else 0)) * h
      - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) := by
  unfold cubeShift
  rw [add_mul, Finset.sum_mul, add_sub_assoc, ← Finset.sum_sub_distrib]
  congr 1
  refine Finset.sum_congr rfl fun k _ => ?_
  ring

/-- The cancellation behind (5.8): for `1 ≤ h ≤ K` the alternating sum over the cube of any
function of `r_{ε,h}` vanishes, because `r_{ε,h}` does not depend on `ε_h`. -/
private theorem cube_cancel {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (g : ℤ → ℝ) {h : ℕ} (h1 : 1 ≤ h) (hK : h ≤ K) :
    ∑ ε : Fin K → Bool, cubeSgn ε * g (cubeShift p0 v ε h) = 0 := by
  let j : Fin K := ⟨h - 1, by omega⟩
  let fl : (Fin K → Bool) → (Fin K → Bool) := fun ε => Function.update ε j (!ε j)
  have hinv : Function.Involutive fl := by
    intro ε; funext i
    by_cases hi : i = j
    · subst hi; simp [fl]
    · simp [fl, Function.update_of_ne hi]
  have hshift : ∀ ε, cubeShift p0 v (fl ε) h = cubeShift p0 v ε h := by
    intro ε; unfold cubeShift; congr 1
    refine Finset.sum_congr rfl fun k _ => ?_
    by_cases hk : k = j
    · have : ((h : ℤ) - (k.1 + 1)) = 0 := by rw [hk]; simp only [j]; omega
      rw [this, zero_mul, zero_mul]
    · simp [fl, Function.update_of_ne hk]
  have hsgn : ∀ ε, cubeSgn (fl ε) = - cubeSgn ε := by
    intro ε; unfold cubeSgn
    rw [Fintype.prod_eq_mul_prod_compl j, Fintype.prod_eq_mul_prod_compl j (fun i => if ε i then (-1 : ℝ) else 1)]
    have hrest : ∏ i ∈ ({j}ᶜ : Finset (Fin K)), (if fl ε i then (-1 : ℝ) else 1)
        = ∏ i ∈ ({j}ᶜ : Finset (Fin K)), (if ε i then (-1 : ℝ) else 1) := by
      refine Finset.prod_congr rfl fun i hi => ?_
      have : i ≠ j := by simpa using hi
      simp [fl, Function.update_of_ne this]
    rw [hrest]
    cases hε : ε j <;> simp [fl, hε]
  set S := ∑ ε : Fin K → Bool, cubeSgn ε * g (cubeShift p0 v ε h) with hS
  have : S = -S := by
    let σ : (Fin K → Bool) ≃ (Fin K → Bool) := hinv.toPerm _
    calc S = ∑ ε, cubeSgn (σ ε) * g (cubeShift p0 v (σ ε) h) := (Equiv.sum_comp σ _).symm
      _ = ∑ ε, -(cubeSgn ε * g (cubeShift p0 v ε h)) := by
          refine Finset.sum_congr rfl fun ε _ => ?_
          show cubeSgn (fl ε) * g (cubeShift p0 v (fl ε) h) = _
          rw [hsgn, hshift]; ring
      _ = -S := by rw [Finset.sum_neg_distrib]
  linarith

private lemma omega_bound_summable (f : ℕ → ℕ) (a b : ℕ) (hf : ∀ k, f k ≤ a + b * k) :
    Summable fun k : ℕ => (ArithmeticFunction.cardDistinctFactors (f k) : ℝ) / 2 ^ (k + 1) := by
  have h0 : Summable fun k : ℕ => ((k : ℝ) ^ 1) * (1 / 2 : ℝ) ^ k :=
    summable_pow_mul_geometric_of_norm_lt_one 1 (by norm_num)
  have h1 : Summable fun k : ℕ => (b : ℝ) * (((k : ℝ) ^ 1) * (1 / 2 : ℝ) ^ k) := h0.mul_left _
  have h2 : Summable fun k : ℕ => (a : ℝ) * (1 / 2 : ℝ) ^ k := summable_geometric_two.mul_left _
  refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) (h2.add h1)
  have hw : (ArithmeticFunction.cardDistinctFactors (f k) : ℝ) ≤ a + b * k := by
    rw [omega_eq_card_filter]
    exact_mod_cast (Finset.card_filter_le _ _).trans ((Nat.card_divisors_le_self (f k)).trans (hf k))
  rw [div_le_iff₀ (by positivity), pow_one, one_div_pow]
  have : ((a : ℝ) * (1 / 2 ^ k) + b * (k * (1 / 2 ^ k))) * 2 ^ (k + 1) = 2 * (a + b * k) := by
    rw [pow_succ]; field_simp
  rw [this]
  have : (0 : ℝ) ≤ a + b * k := by positivity
  linarith

/-- Equation (5.8) of Tao–Teräväinen, from (5.1) for all `N`. Hypotheses: `P ε = p_ε` are primes
(5.4), and `n - ∑_k k ε_k v_k = p_ε m_ε` with `m_ε ∈ ℕ` (this is (5.5) together with (5.6)).
Conclusion: `q ∑_{h ≥ 1} ∑_ε (-1)^{|ε|} ω(n + r_{ε,h+K}) / 2^{h+K}
  + q ∑_ε (-1)^{|ε|} δ_{p_ε}(n - ∑_k k ε_k v_k)` is an integer (indices `h = k + 1`). -/
private theorem eq_5_8 (q : ℕ)
    (hint : ∀ N : ℕ, ∃ z : ℤ,
      (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (k + (N + 1)) : ℝ) / 2 ^ (k + 1) = z)
    {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ)
    (hP : ∀ ε, (P ε : ℤ) = p0 + ∑ k : Fin K, (if ε k then v k else 0))
    (hprime : ∀ ε, (P ε).Prime) (n : ℤ) (m : (Fin K → Bool) → ℕ)
    (hm : ∀ ε, n - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) = P ε * m ε) :
    ∃ z : ℤ,
      (q : ℝ) * ∑' k, (∑ ε : Fin K → Bool, cubeSgn ε *
          (ArithmeticFunction.cardDistinctFactors (n + cubeShift p0 v ε (k + K + 1)).toNat : ℝ))
          / 2 ^ (k + K + 1)
      + (q : ℝ) * ∑ ε : Fin K → Bool, cubeSgn ε *
          ∑' k, (if P ε ^ 2 ∣ P ε * m ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) = z := by
  -- the per-vertex congruences (5.2)
  have hz : ∀ ε, ∃ z : ℤ,
      (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (P ε * m ε + P ε * (k + 1)) : ℝ) / 2 ^ (k + 1)
      + (q : ℝ) * ∑' k, (if P ε ^ 2 ∣ P ε * m ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) = z :=
    fun ε => eq_5_2 q hint (hprime ε) (m ε)
  choose z hz using hz
  -- the shift identity n + r_{ε,h} = p_ε m_ε + p_ε h
  have hsh : ∀ ε (h : ℕ), (n + cubeShift p0 v ε h).toNat = P ε * m ε + P ε * h := by
    intro ε h
    have : n + cubeShift p0 v ε h = ((P ε * m ε + P ε * h : ℕ) : ℤ) := by
      rw [cubeShift_eq, ← hP ε]; push_cast; linear_combination hm ε
    rw [this, Int.toNat_natCast]
  let a : (Fin K → Bool) → ℕ → ℝ := fun ε k =>
    (ArithmeticFunction.cardDistinctFactors (P ε * m ε + P ε * (k + 1)) : ℝ) / 2 ^ (k + 1)
  have ha : ∀ ε, Summable (a ε) := fun ε =>
    omega_bound_summable (fun k => P ε * m ε + P ε * (k + 1)) (P ε * m ε + P ε) (P ε)
      (fun k => by ring_nf; omega)
  let F : ℕ → ℝ := fun k => ∑ ε : Fin K → Bool, cubeSgn ε * a ε k
  have hF : Summable F := summable_sum fun ε _ => (ha ε).mul_left _
  -- the first K terms cancel (5.8)
  have hfirst : ∑ i ∈ Finset.range K, F i = 0 := by
    refine Finset.sum_eq_zero fun i hi => ?_
    have hiK : i + 1 ≤ K := Finset.mem_range.mp hi
    have := cube_cancel p0 v (fun t => (ArithmeticFunction.cardDistinctFactors (n + t).toNat : ℝ) / 2 ^ (i + 1))
      (h := i + 1) (by omega) hiK
    simp only [F, a]
    rw [← this]
    refine Finset.sum_congr rfl fun ε _ => ?_
    rw [hsh]
  have htail : ∀ k, F (k + K) = (∑ ε : Fin K → Bool, cubeSgn ε *
      (ArithmeticFunction.cardDistinctFactors (n + cubeShift p0 v ε (k + K + 1)).toNat : ℝ))
      / 2 ^ (k + K + 1) := by
    intro k
    simp only [F, a]
    rw [Finset.sum_div]
    refine Finset.sum_congr rfl fun ε _ => ?_
    rw [hsh, mul_div_assoc]
  have hsum := hF.sum_add_tsum_nat_add K
  rw [hfirst, zero_add] at hsum
  simp_rw [htail] at hsum
  rw [hsum]
  have hswap : ∑' k, F k = ∑ ε : Fin K → Bool, cubeSgn ε * ∑' k, a ε k := by
    simp only [F]
    rw [Summable.tsum_finsetSum fun ε _ => (ha ε).mul_left _]
    refine Finset.sum_congr rfl fun ε _ => ?_
    rw [tsum_mul_left]
  rw [hswap]
  refine ⟨∑ ε : Fin K → Bool, (-1 : ℤ) ^ (Finset.univ.filter fun i => ε i = true).card * z ε, ?_⟩
  push_cast
  rw [Finset.mul_sum, Finset.mul_sum, ← Finset.sum_add_distrib]
  refine Finset.sum_congr rfl fun ε _ => ?_
  rw [← hz ε, cubeSgn_eq]
  simp only [a]
  ring

/-- Rationality of `∑_p 1/(2^p - 1)` implies (5.8) for every prime cube and every admissible `n`:
S1–S4 of the route map (annex on `variant-6b364d2b`) chained together. -/
private theorem rational_imp_5_8 (a : ℤ) (q : ℕ) (hq : 0 < q)
    (h : ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) = a / q)
    {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ)
    (hP : ∀ ε, (P ε : ℤ) = p0 + ∑ k : Fin K, (if ε k then v k else 0))
    (hprime : ∀ ε, (P ε).Prime) (n : ℤ) (m : (Fin K → Bool) → ℕ)
    (hm : ∀ ε, n - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) = P ε * m ε) :
    ∃ z : ℤ,
      (q : ℝ) * ∑' k, (∑ ε : Fin K → Bool, cubeSgn ε *
          (ArithmeticFunction.cardDistinctFactors (n + cubeShift p0 v ε (k + K + 1)).toNat : ℝ))
          / 2 ^ (k + K + 1)
      + (q : ℝ) * ∑ ε : Fin K → Bool, cubeSgn ε *
          ∑' k, (if P ε ^ 2 ∣ P ε * m ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) = z :=
  eq_5_8 q (fun N => eq_5_1 a q hq h N) p0 v P hP hprime n m hm

/-- `2^{ω(n)} ≤ n` for `n ≠ 0`, i.e. `ω(n) ≤ log₂ n`: the crude bound `ω(n) ≪ log n` of §5.8. -/
private theorem two_pow_omega_le {n : ℕ} (hn : n ≠ 0) : 2 ^ ArithmeticFunction.cardDistinctFactors n ≤ n := by
  rw [omega_eq_card_pf]
  calc 2 ^ n.primeFactors.card = ∏ p ∈ n.primeFactors, 2 := by rw [Finset.prod_const]
    _ ≤ ∏ p ∈ n.primeFactors, p :=
        Finset.prod_le_prod' fun p hp => (Nat.prime_of_mem_primeFactors hp).two_le
    _ ≤ n := Nat.le_of_dvd (Nat.pos_of_ne_zero hn) (Nat.prod_primeFactors_dvd n)

/-- `e(t) = exp(2πit)`. -/
private noncomputable def ee (t : ℝ) : ℂ := Complex.exp (((2 * Real.pi * t : ℝ) : ℂ) * Complex.I)

private lemma ee_norm (t : ℝ) : ‖ee t‖ = 1 := Complex.norm_exp_ofReal_mul_I _

private lemma ee_add (a b : ℝ) : ee (a + b) = ee a * ee b := by
  unfold ee; rw [← Complex.exp_add]; congr 1; push_cast; ring

private lemma ee_int (z : ℤ) : ee z = 1 := by
  unfold ee
  rw [← Complex.exp_int_mul_two_pi_mul_I z]; congr 1; push_cast; ring

private lemma ee_re (t : ℝ) : (ee t).re = Real.cos (2 * Real.pi * t) :=
  Complex.exp_ofReal_mul_I_re _

private lemma ee_conj (t : ℝ) : (starRingEnd ℂ) (ee t) = ee (-t) := by
  unfold ee; rw [← Complex.exp_conj]; congr 1
  simp only [map_mul, Complex.conj_ofReal, Complex.conj_I]; push_cast; ring

/-- `‖e(t) - 1‖ ≤ 2π|t|`. -/
private lemma ee_sub_one (t : ℝ) : ‖ee t - 1‖ ≤ 2 * Real.pi * |t| := by
  set x : ℝ := 2 * Real.pi * t with hx
  have hre : (ee t - 1).re = Real.cos x - 1 := by
    rw [Complex.sub_re, ee_re, Complex.one_re]
  have him : (ee t - 1).im = Real.sin x := by
    simp only [Complex.sub_im, Complex.one_im, sub_zero, ee]
    exact Complex.exp_ofReal_mul_I_im _
  have hsq : ‖ee t - 1‖ ^ 2 ≤ x ^ 2 := by
    rw [← Complex.normSq_eq_norm_sq, Complex.normSq_apply, hre, him]
    have h1 := Real.one_sub_sq_div_two_le_cos (x := x)
    have h2 := Real.sin_sq_add_cos_sq x
    nlinarith
  have := (sq_le_sq.mp hsq)
  rw [abs_of_nonneg (norm_nonneg _)] at this
  calc ‖ee t - 1‖ ≤ |x| := this
    _ = 2 * Real.pi * |t| := by
      rw [hx, abs_mul, abs_of_pos (by positivity : (0 : ℝ) < 2 * Real.pi)]

/-- `∑∑ (a_i - a_j)^2 = 2N ∑ a^2 - 2 (∑ a)^2`. -/
private lemma sum_sum_sub_sq {ι : Type*} (s : Finset ι) (a : ι → ℝ) :
    ∑ i ∈ s, ∑ j ∈ s, (a i - a j) ^ 2
      = 2 * (s.card : ℝ) * ∑ i ∈ s, a i ^ 2 - 2 * (∑ i ∈ s, a i) ^ 2 := by
  have e1 : ∀ i j, (a i - a j) ^ 2 = a i ^ 2 + a j ^ 2 - 2 * (a i * a j) := fun i j => by ring
  simp only [e1, Finset.sum_sub_distrib, Finset.sum_add_distrib, Finset.sum_const, nsmul_eq_mul,
    ← Finset.mul_sum]
  rw [← Finset.sum_mul]; ring

/-- The variance step of (5.20): for `|X| ≤ 1/4` on a finite nonempty set, with `n` uniform,
`|E e(X)|^2 ≤ 1 - 16 Var X`. -/
private lemma norm_avg_ee_sq_le {ι : Type*} (s : Finset ι) (hs : s.Nonempty) (X : ι → ℝ)
    (hX : ∀ i ∈ s, |X i| ≤ 1 / 4) :
    ‖((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X i)‖ ^ 2
      ≤ 1 - 16 * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X i ^ 2 - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X i) ^ 2) := by
  have hN : (0 : ℝ) < s.card := by exact_mod_cast hs.card_pos
  set S : ℂ := ∑ i ∈ s, ee (X i) with hS
  have hnorm : ‖S‖ ^ 2 = ∑ i ∈ s, ∑ j ∈ s, Real.cos (2 * Real.pi * (X i - X j)) := by
    have h1 : ((‖S‖ ^ 2 : ℝ) : ℂ) = S * (starRingEnd ℂ) S := by
      rw [Complex.mul_conj, Complex.normSq_eq_norm_sq]
    have h2 : S * (starRingEnd ℂ) S = ∑ i ∈ s, ∑ j ∈ s, ee (X i - X j) := by
      rw [hS, map_sum, Finset.sum_mul_sum]
      refine Finset.sum_congr rfl fun i _ => Finset.sum_congr rfl fun j _ => ?_
      rw [ee_conj, sub_eq_add_neg, ee_add]
    have h3 := congrArg Complex.re (h1.trans h2)
    rw [Complex.ofReal_re] at h3
    rw [h3, Complex.re_sum]
    refine Finset.sum_congr rfl fun i _ => ?_
    rw [Complex.re_sum]
    exact Finset.sum_congr rfl fun j _ => ee_re _
  have hcos : ∀ i ∈ s, ∀ j ∈ s,
      Real.cos (2 * Real.pi * (X i - X j)) ≤ 1 - 8 * (X i - X j) ^ 2 := by
    intro i hi j hj
    have hij : |X i - X j| ≤ 1 / 2 := by
      have := abs_sub (X i) (X j); linarith [hX i hi, hX j hj]
    have hpi : |2 * Real.pi * (X i - X j)| ≤ Real.pi := by
      rw [abs_mul, abs_of_pos (by positivity : (0 : ℝ) < 2 * Real.pi)]
      nlinarith [Real.pi_pos]
    have := Real.cos_le_one_sub_mul_cos_sq hpi
    have hp : Real.pi ≠ 0 := Real.pi_ne_zero
    calc Real.cos (2 * Real.pi * (X i - X j)) ≤ 1 - 2 / Real.pi ^ 2 * (2 * Real.pi * (X i - X j)) ^ 2 :=
          this
      _ = 1 - 8 * (X i - X j) ^ 2 := by field_simp; ring
  have hsum : ‖S‖ ^ 2 ≤ (s.card : ℝ) ^ 2 - 8 * (2 * (s.card : ℝ) * ∑ i ∈ s, X i ^ 2
      - 2 * (∑ i ∈ s, X i) ^ 2) := by
    rw [hnorm, ← sum_sum_sub_sq]
    calc ∑ i ∈ s, ∑ j ∈ s, Real.cos (2 * Real.pi * (X i - X j))
        ≤ ∑ i ∈ s, ∑ j ∈ s, (1 - 8 * (X i - X j) ^ 2) :=
          Finset.sum_le_sum fun i hi => Finset.sum_le_sum fun j hj => hcos i hi j hj
      _ = (s.card : ℝ) ^ 2 - 8 * ∑ i ∈ s, ∑ j ∈ s, (X i - X j) ^ 2 := by
          simp only [Finset.sum_sub_distrib, Finset.sum_const, nsmul_eq_mul, mul_one,
            ← Finset.mul_sum]
          ring
  rw [norm_mul, mul_pow, norm_inv, Complex.norm_real, Real.norm_eq_abs, abs_of_pos hN]
  have hN2 : (0 : ℝ) < (s.card : ℝ) ^ 2 := by positivity
  rw [inv_pow, inv_mul_le_iff₀ hN2]
  have : (s.card : ℝ) ^ 2 * (1 - 16 * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X i ^ 2
      - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X i) ^ 2))
      = (s.card : ℝ) ^ 2 - 8 * (2 * (s.card : ℝ) * ∑ i ∈ s, X i ^ 2
      - 2 * (∑ i ∈ s, X i) ^ 2) := by
    field_simp; ring
  rw [this]; exact hsum

/-- The product step of (5.20): `|∏_p E e(X_p)| ≤ exp(-8 ∑_p Var X_p)`. -/
private lemma norm_prod_avg_le {ι : Type*} (s : Finset ι) (hs : s.Nonempty) (S1 : Finset ℕ)
    (X : ℕ → ι → ℝ) (hX : ∀ p ∈ S1, ∀ i ∈ s, |X p i| ≤ 1 / 4) :
    ‖∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i)‖
      ≤ Real.exp (-8 * ∑ p ∈ S1, ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2)) := by
  have hsq : ‖∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i)‖ ^ 2
      ≤ Real.exp (-8 * ∑ p ∈ S1, ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2)) ^ 2 := by
    rw [norm_prod, ← Finset.prod_pow, sq, ← Real.exp_add, Finset.mul_sum, ← Finset.sum_add_distrib,
      Real.exp_sum]
    refine Finset.prod_le_prod (fun p _ => by positivity) fun p hp => ?_
    refine (norm_avg_ee_sq_le s hs (X p) (hX p hp)).trans ?_
    have := Real.add_one_le_exp (-8 * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2) + -8 * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2))
    linarith
  have := sq_le_sq.mp hsq
  rwa [abs_of_nonneg (norm_nonneg _), abs_of_pos (Real.exp_pos _)] at this

/-- S5–S7 assembled: if every `Y + C₀ + W` is an integer, `E|W| ≤ δ`, the decorrelation defect is
at most `δ`, `|X_p| ≤ 1/4` and `∑ Var X_p ≥ c₀`, then `1 - 8δ ≤ exp(-8c₀)`. -/
private lemma assembly_core {ι : Type*} (s : Finset ι) (hs : s.Nonempty) (S1 : Finset ℕ)
    (X : ℕ → ι → ℝ) (W : ι → ℝ) (C0 δ c0 : ℝ)
    (hint : ∀ i ∈ s, ∃ z : ℤ, (∑ p ∈ S1, X p i) + C0 + W i = z)
    (hX : ∀ p ∈ S1, ∀ i ∈ s, |X p i| ≤ 1 / 4)
    (hW : (s.card : ℝ)⁻¹ * ∑ i ∈ s, |W i| ≤ δ)
    (hdec : ‖((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (∑ p ∈ S1, X p i)
        - ∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i)‖ ≤ δ)
    (hvar : c0 ≤ ∑ p ∈ S1, ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2)) :
    1 - 8 * δ ≤ Real.exp (-8 * c0) := by
  have hN : (0 : ℝ) < s.card := by exact_mod_cast hs.card_pos
  have hδ : 0 ≤ δ := le_trans (by positivity) hW
  -- each phase is `e(-C₀) e(-W)`
  have hpt : ∀ i ∈ s, ‖ee (∑ p ∈ S1, X p i) - ee (-C0)‖ ≤ 2 * Real.pi * |W i| := by
    intro i hi
    obtain ⟨z, hz⟩ := hint i hi
    have : ee (∑ p ∈ S1, X p i) = ee (-C0) * ee (-W i) := by
      have e1 : (∑ p ∈ S1, X p i) = (z : ℝ) + -C0 + -W i := by linarith
      rw [e1, ee_add, ee_add, ee_int, one_mul]
    rw [this, ← mul_sub_one, norm_mul, ee_norm, one_mul]
    have := ee_sub_one (-W i); rwa [abs_neg] at this
  set A : ℂ := ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (∑ p ∈ S1, X p i) with hA
  have hA1 : ‖A - ee (-C0)‖ ≤ 2 * Real.pi * δ := by
    have hc : ee (-C0) = ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (-C0) := by
      have : (s.card : ℂ) ≠ 0 := by exact_mod_cast hN.ne'
      rw [Finset.sum_const, nsmul_eq_mul, ← mul_assoc, Complex.ofReal_natCast,
        inv_mul_cancel₀ this, one_mul]
    rw [hA, hc, ← mul_sub, ← Finset.sum_sub_distrib, norm_mul, norm_inv, Complex.norm_real,
      Real.norm_eq_abs, abs_of_pos hN]
    calc (s.card : ℝ)⁻¹ * ‖∑ i ∈ s, (ee (∑ p ∈ S1, X p i) - ee (-C0))‖
        ≤ (s.card : ℝ)⁻¹ * ∑ i ∈ s, 2 * Real.pi * |W i| := by
          exact mul_le_mul_of_nonneg_left
            ((norm_sum_le _ _).trans (Finset.sum_le_sum hpt)) (inv_nonneg.mpr hN.le)
      _ = 2 * Real.pi * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, |W i|) := by rw [← Finset.mul_sum]; ring
      _ ≤ 2 * Real.pi * δ := mul_le_mul_of_nonneg_left hW (by linarith [Real.pi_pos])
  have hA2 : 1 - 2 * Real.pi * δ ≤ ‖A‖ := by
    have := norm_sub_norm_le (ee (-C0)) (ee (-C0) - A)
    rw [sub_sub_cancel, ee_norm, norm_sub_rev] at this
    linarith
  have hP := norm_prod_avg_le s hs S1 X hX
  have hP2 : ‖A‖ - δ ≤ ‖∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i)‖ := by
    have := norm_sub_norm_le A (A - ∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i))
    rw [sub_sub_cancel] at this
    linarith
  have hexp : Real.exp (-8 * ∑ p ∈ S1, ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2)) ≤ Real.exp (-8 * c0) :=
    Real.exp_le_exp.mpr (by linarith)
  have hpi : Real.pi < 3.15 := Real.pi_lt_d2
  nlinarith

/-- Independence: the average over `Ns^{S₁}` of a product is the product of averages. -/
private lemma sum_pi_prod {R : Type*} [CommSemiring R] (S1 : Finset ℕ) (Ns : Finset ℤ)
    (f : ℕ → ℤ → R) :
    ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), ∏ p : S1, f p (ω p) = ∏ p ∈ S1, ∑ n ∈ Ns, f p n := by
  rw [← Finset.prod_univ_sum (fun _ : S1 => Ns) (fun p n => f p n)]
  exact Finset.prod_coe_sort S1 (fun p => ∑ n ∈ Ns, f p n)

private lemma card_pi (S1 : Finset ℕ) (Ns : Finset ℤ) :
    ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ) = (Ns.card : ℝ) ^ S1.card := by
  rw [Fintype.card_piFinset, Finset.prod_const, Finset.card_univ, Fintype.card_coe]; push_cast; rfl

/-- `E' e(∑ X'_p) = ∏ E e(X_p)` for independent copies. -/
private lemma avg_pi_ee (S1 : Finset ℕ) (Ns : Finset ℤ) (X : ℕ → ℤ → ℝ) :
    (((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ) : ℂ)⁻¹
        * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), ee (∑ p : S1, X p (ω p))
      = ∏ p ∈ S1, ((Ns.card : ℝ) : ℂ)⁻¹ * ∑ n ∈ Ns, ee (X p n) := by
  have hprod : ∀ ω : S1 → ℤ, ee (∑ p : S1, X p (ω p)) = ∏ p : S1, ee (X p (ω p)) := by
    intro ω
    unfold ee
    rw [← Complex.exp_sum]; congr 1; push_cast; rw [Finset.mul_sum, Finset.sum_mul]
  simp_rw [hprod]
  rw [sum_pi_prod S1 Ns (fun p n => ee (X p n)), card_pi, Finset.prod_mul_distrib, Finset.prod_const]
  push_cast
  rw [inv_pow]

/-- `E' exp(c ∑ X'_p) = ∏ E exp(c X_p)`. -/
private lemma avg_pi_exp (S1 : Finset ℕ) (Ns : Finset ℤ) (X : ℕ → ℤ → ℝ) (c : ℝ) :
    ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ)⁻¹
        * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), Real.exp (c * ∑ p : S1, X p (ω p))
      = ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp (c * X p n) := by
  have hprod : ∀ ω : S1 → ℤ, Real.exp (c * ∑ p : S1, X p (ω p)) = ∏ p : S1, Real.exp (c * X p (ω p)) := by
    intro ω; rw [← Real.exp_sum, Finset.mul_sum]
  simp_rw [hprod]
  rw [sum_pi_prod S1 Ns (fun p n => Real.exp (c * X p n)), card_pi, Finset.prod_mul_distrib,
    Finset.prod_const, inv_pow]

/-- `t^M/M! ≤ e^t + e^{-t}` for even `M`. -/
private lemma pow_div_fact_le_exp_add {M : ℕ} (hM : Even M) (t : ℝ) :
    t ^ M / M.factorial ≤ Real.exp t + Real.exp (-t) := by
  have h1 := Real.pow_div_factorial_le_exp |t| (abs_nonneg t) M
  have h2 : t ^ M = |t| ^ M := (hM.pow_abs t).symm
  rw [h2]
  rcases le_total 0 t with ht | ht
  · rw [abs_of_nonneg ht] at h1 ⊢; linarith [Real.exp_pos (-t)]
  · rw [abs_of_nonpos ht] at h1 ⊢; linarith [Real.exp_pos t]

/-- The sharp Taylor bound for `e(y)`, from the one for `exp(ix)`. -/
private lemma ee_taylor (htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial) (M : ℕ) (y : ℝ) :
    ‖ee y - ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (y : ℂ) ^ m‖
      ≤ (2 * Real.pi) ^ M / M.factorial * |y| ^ M := by
  have h := htay (2 * Real.pi * y) M
  have e1 : ∑ m ∈ Finset.range M, (((2 * Real.pi * y : ℝ) : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)
      = ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (y : ℂ) ^ m :=
    Finset.sum_congr rfl fun m _ => by push_cast; ring
  rw [e1, abs_mul, abs_of_pos (by positivity : (0 : ℝ) < 2 * Real.pi), mul_pow] at h
  unfold ee
  calc _ ≤ (2 * Real.pi) ^ M * |y| ^ M / M.factorial := h
    _ = _ := by ring

/-- An average of `e(Z)` is the Taylor part plus the average remainder. -/
private lemma avg_ee_split {γ : Type*} (u : Finset γ) (Z : γ → ℝ) (M : ℕ) :
    ((u.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ u, ee (Z i)
      = ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((u.card : ℝ)⁻¹ * ∑ i ∈ u, Z i ^ m : ℝ) : ℂ)
        + ((u.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ u, (ee (Z i) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Z i : ℂ) ^ m) := by
  have hC : ((u.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ u, ∑ m ∈ Finset.range M,
        (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Z i : ℂ) ^ m
      = ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((u.card : ℝ)⁻¹ * ∑ i ∈ u, Z i ^ m : ℝ) : ℂ) := by
    rw [Finset.sum_comm, Finset.mul_sum]
    refine Finset.sum_congr rfl fun m _ => ?_
    rw [← Finset.mul_sum]; push_cast; ring
  rw [Finset.sum_sub_distrib, mul_sub, hC]; ring

private lemma norm_avg_rem_le {γ : Type*} (u : Finset γ) (Z : γ → ℝ) (M : ℕ)
    (htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial) :
    ‖((u.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ u, (ee (Z i) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Z i : ℂ) ^ m)‖
      ≤ (2 * Real.pi) ^ M / M.factorial * ((u.card : ℝ)⁻¹ * ∑ i ∈ u, |Z i| ^ M) := by
  rw [norm_mul, norm_inv, Complex.norm_real, Real.norm_eq_abs,
    abs_of_nonneg (Nat.cast_nonneg _)]
  calc (u.card : ℝ)⁻¹ * ‖∑ i ∈ u, (ee (Z i) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Z i : ℂ) ^ m)‖
      ≤ (u.card : ℝ)⁻¹ * ∑ i ∈ u, (2 * Real.pi) ^ M / M.factorial * |Z i| ^ M :=
        mul_le_mul_of_nonneg_left ((norm_sum_le _ _).trans
          (Finset.sum_le_sum fun i _ => ee_taylor htay M (Z i))) (inv_nonneg.mpr (Nat.cast_nonneg _))
    _ = _ := by rw [← Finset.mul_sum]; ring

private lemma norm_coef (m : ℕ) :
    ‖(2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)‖ = (2 * Real.pi) ^ m / m.factorial := by
  rw [norm_div, norm_pow, Complex.norm_natCast, norm_mul, Complex.norm_I, mul_one, norm_mul,
    Complex.norm_ofNat, Complex.norm_real, Real.norm_eq_abs, abs_of_pos Real.pi_pos]

/-- The comparison at the start of §5.4: `|E e(Y) - E e(Y')| ≤ ∑_{m<M} (2π)^m/m! |E Y^m - E Y'^m|
+ (2π)^M/M! (E|Y|^M + E|Y'|^M)`. -/
private lemma avg_ee_compare {α β : Type*} (s : Finset α) (t : Finset β) (Y : α → ℝ) (Y' : β → ℝ)
    (M : ℕ) (htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial) :
    ‖((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (Y i) - ((t.card : ℝ) : ℂ)⁻¹ * ∑ j ∈ t, ee (Y' j)‖
      ≤ ∑ m ∈ Finset.range M, (2 * Real.pi) ^ m / m.factorial
          * |(s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m - (t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m|
        + (2 * Real.pi) ^ M / M.factorial * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, |Y i| ^ M
          + (t.card : ℝ)⁻¹ * ∑ j ∈ t, |Y' j| ^ M) := by
  rw [avg_ee_split s Y M, avg_ee_split t Y' M]
  set Rs := ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, (ee (Y i) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Y i : ℂ) ^ m)
  set Rt := ((t.card : ℝ) : ℂ)⁻¹ * ∑ j ∈ t, (ee (Y' j) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Y' j : ℂ) ^ m)
  have hRs := norm_avg_rem_le s Y M htay
  have hRt := norm_avg_rem_le t Y' M htay
  have hmain : ‖∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m : ℝ) : ℂ)
        - ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m : ℝ) : ℂ)‖
      ≤ ∑ m ∈ Finset.range M, (2 * Real.pi) ^ m / m.factorial
          * |(s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m - (t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m| := by
    rw [← Finset.sum_sub_distrib]
    refine (norm_sum_le _ _).trans (Finset.sum_le_sum fun m _ => ?_)
    rw [← mul_sub, norm_mul, norm_coef, ← Complex.ofReal_sub, Complex.norm_real, Real.norm_eq_abs]
  set A := ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m : ℝ) : ℂ)
        - ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m : ℝ) : ℂ) with hA
  have h3 : ‖A + Rs + -Rt‖ ≤ ‖A‖ + ‖Rs‖ + ‖-Rt‖ := norm_add₃_le
  rw [norm_neg] at h3
  have heq : ‖(∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m : ℝ) : ℂ) + Rs)
        - (∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m : ℝ) : ℂ) + Rt)‖ = ‖A + Rs + -Rt‖ := by
    congr 1; rw [hA]; ring
  rw [heq]
  linarith

/-- (5.16)–(5.17) give the decorrelation: `|E e(∑ X_p) - ∏ E e(X_p)| ≤ 2κ₄ + 2κ₅`, with `X'_p`
independent copies (uniform on `Ns^{S₁}`) and `e = exp 1` in `κ₅`. -/
private lemma decor_bound (Ns : Finset ℤ) (S1 : Finset ℕ) (X : ℕ → ℤ → ℝ) (M : ℕ) (hM : Even M)
    (htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial) :
    ‖((Ns.card : ℝ) : ℂ)⁻¹ * ∑ n ∈ Ns, ee (∑ p ∈ S1, X p n)
        - ∏ p ∈ S1, ((Ns.card : ℝ) : ℂ)⁻¹ * ∑ n ∈ Ns, ee (X p n)‖
      ≤ 2 * (∑ m ∈ Finset.range (M + 1), (2 * Real.pi) ^ m / m.factorial
          * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ m
            - ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ)⁻¹
              * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), (∑ p : S1, X p (ω p)) ^ m|)
        + 2 * ((∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
              Real.exp (2 * Real.pi * Real.exp 1 * X p n)
            + ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
              Real.exp (-(2 * Real.pi * Real.exp 1) * X p n)) / Real.exp 1 ^ M) := by
  set Ω := Fintype.piFinset (fun _ : S1 => Ns) with hΩ
  rw [← avg_pi_ee S1 Ns X]
  have hc := avg_ee_compare Ns Ω (fun n => ∑ p ∈ S1, X p n) (fun ω => ∑ p : S1, X p (ω p)) M htay
  beta_reduce at hc
  -- |Y|^M = Y^M
  have habs : ∀ y : ℝ, |y| ^ M = y ^ M := fun y => hM.pow_abs y
  simp only [habs] at hc
  -- the kappa_5 bound on the independent M-th moment
  have hk5 : (2 * Real.pi) ^ M / M.factorial
        * ((Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M)
      ≤ (∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp (2 * Real.pi * Real.exp 1 * X p n)
          + ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
              Real.exp (-(2 * Real.pi * Real.exp 1) * X p n)) / Real.exp 1 ^ M := by
    rw [← avg_pi_exp S1 Ns X, ← avg_pi_exp S1 Ns X, ← mul_add, ← Finset.sum_add_distrib]
    have hE : (0 : ℝ) < Real.exp 1 ^ M := by positivity
    rw [mul_div_assoc, Finset.sum_div, mul_left_comm]
    refine mul_le_mul_of_nonneg_left ?_ (inv_nonneg.mpr (Nat.cast_nonneg _))
    rw [Finset.mul_sum]
    refine Finset.sum_le_sum fun ω _ => ?_
    have := pow_div_fact_le_exp_add hM (2 * Real.pi * Real.exp 1 * ∑ p : S1, X p (ω p))
    rw [le_div_iff₀ hE]
    calc (2 * Real.pi) ^ M / M.factorial * (∑ p : S1, X p (ω p)) ^ M * Real.exp 1 ^ M
        = (2 * Real.pi * Real.exp 1 * ∑ p : S1, X p (ω p)) ^ M / M.factorial := by ring
      _ ≤ _ := by rw [neg_mul]; exact this
  have hsplit : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ M
      ≤ |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ M
          - (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M|
        + (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M := by
    have := le_abs_self ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ M
          - (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M)
    linarith
  rw [Finset.sum_range_succ]
  have hcoef : (0 : ℝ) ≤ (2 * Real.pi) ^ M / M.factorial := by positivity
  have hterms : 0 ≤ ∑ m ∈ Finset.range M, (2 * Real.pi) ^ m / m.factorial
          * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ m
            - (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ m| :=
    Finset.sum_nonneg fun m _ => mul_nonneg (by positivity) (abs_nonneg _)
  have hlast : 0 ≤ (2 * Real.pi) ^ M / M.factorial
          * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ M
            - (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M| :=
    mul_nonneg hcoef (abs_nonneg _)
  have := mul_le_mul_of_nonneg_left hsplit hcoef
  nlinarith

private lemma taylor_term_deriv (m : ℕ) (t : ℝ) :
    HasDerivAt (fun s : ℝ => ((s : ℂ) * Complex.I) ^ (m + 1) / ((m + 1).factorial : ℂ))
      (Complex.I * (((t : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))) t := by
  have h0 : HasDerivAt (fun s : ℝ => (s : ℂ) * Complex.I) (1 * Complex.I) t :=
    ((hasDerivAt_id t).ofReal_comp).mul_const Complex.I
  have h1 := (h0.pow (m + 1)).div_const ((m + 1).factorial : ℂ)
  refine h1.congr_deriv ?_
  rw [Nat.factorial_succ]
  push_cast
  have hf : (m.factorial : ℂ) ≠ 0 := by exact_mod_cast (Nat.factorial_pos m).ne'
  have hm : ((m : ℂ) + 1) ≠ 0 := by exact_mod_cast Nat.succ_ne_zero m
  field_simp

private lemma taylor_deriv (M : ℕ) (t : ℝ) :
    HasDerivAt (fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range (M + 1), ((s : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))
      (Complex.I * (Complex.exp ((t : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range M, ((t : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))) t := by
  have he : HasDerivAt (fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I))
      (Complex.exp ((t : ℂ) * Complex.I) * (1 * Complex.I)) t :=
    (((hasDerivAt_id t).ofReal_comp).mul_const Complex.I).cexp
  have hs : HasDerivAt (fun s : ℝ => ∑ m ∈ Finset.range M,
        ((s : ℂ) * Complex.I) ^ (m + 1) / ((m + 1).factorial : ℂ))
      (∑ m ∈ Finset.range M, Complex.I * (((t : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))) t :=
    HasDerivAt.fun_sum fun m _ => taylor_term_deriv m t
  have hfun : (fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range (M + 1), ((s : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))
      = fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I) - (∑ m ∈ Finset.range M,
        ((s : ℂ) * Complex.I) ^ (m + 1) / ((m + 1).factorial : ℂ) + 1) := by
    funext s; rw [Finset.sum_range_succ']; simp
  rw [hfun]
  refine (he.sub (hs.add_const 1)).congr_deriv ?_
  rw [← Finset.mul_sum]; ring

private lemma taylor_nonneg (M : ℕ) : ∀ x : ℝ, 0 ≤ x →
    ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ x ^ M / M.factorial := by
  induction M with
  | zero =>
    intro x _
    simp [Complex.norm_exp_ofReal_mul_I]
  | succ M ih =>
    intro x hx
    have hB : ∀ t : ℝ, HasDerivAt (fun s : ℝ => s ^ (M + 1) / ((M + 1).factorial : ℝ))
        (t ^ M / (M.factorial : ℝ)) t := by
      intro t
      refine ((hasDerivAt_pow (M + 1) t).div_const ((M + 1).factorial : ℝ)).congr_deriv ?_
      rw [Nat.factorial_succ]; push_cast
      have hf : (M.factorial : ℝ) ≠ 0 := by exact_mod_cast (Nat.factorial_pos M).ne'
      field_simp
    have := image_norm_le_of_norm_deriv_right_le_deriv_boundary
      (f := fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range (M + 1), ((s : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))
      (a := 0) (b := x)
      (f' := fun t => Complex.I * (Complex.exp ((t : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range M, ((t : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)))
      (fun t _ => (taylor_deriv M t).continuousAt.continuousWithinAt)
      (fun t _ => (taylor_deriv M t).hasDerivWithinAt)
      (B := fun s => s ^ (M + 1) / ((M + 1).factorial : ℝ)) (B' := fun t => t ^ M / (M.factorial : ℝ))
      (by simp [Finset.sum_range_succ']) hB
      (fun t ht => by
        rw [norm_mul, Complex.norm_I, one_mul]
        exact ih t ht.1)
      (Set.right_mem_Icc.mpr hx)
    simpa using this

/-- The sharp Taylor bound for `exp(ix)`, every real `x`: `|e^{ix} - ∑_{m<M} (ix)^m/m!| ≤ |x|^M/M!`. -/
private lemma taylor_all (x : ℝ) (M : ℕ) :
    ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial := by
  rcases le_total 0 x with hx | hx
  · rw [abs_of_nonneg hx]; exact taylor_nonneg M x hx
  · have h := taylor_nonneg M (-x) (by linarith)
    rw [abs_of_nonpos hx]
    have hc : Complex.exp ((x : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)
        = (starRingEnd ℂ) (Complex.exp (((-x : ℝ) : ℂ) * Complex.I)
          - ∑ m ∈ Finset.range M, (((-x : ℝ) : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)) := by
      have hz : (starRingEnd ℂ) (((-x : ℝ) : ℂ) * Complex.I) = (x : ℂ) * Complex.I := by
        simp only [map_mul, Complex.conj_ofReal, Complex.conj_I]; push_cast; ring
      rw [map_sub, map_sum, ← Complex.exp_conj, hz]
      congr 1
      refine Finset.sum_congr rfl fun m _ => ?_
      rw [map_div₀, map_pow, hz, map_natCast]
    rw [hc, Complex.norm_conj]
    exact h

/-- `ω(u) = #{p ≤ B prime : p ∣ u}` for `0 < u ≤ B`. -/
private lemma omega_eq_sum_primes_le {u B : ℕ} (hu : 0 < u) (huB : u ≤ B) :
    (ArithmeticFunction.cardDistinctFactors u : ℝ)
      = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, (if p ∣ u then (1 : ℝ) else 0) := by
  rw [Finset.sum_boole, omega_eq_card_pf]
  congr 2
  ext p
  simp only [Nat.mem_primeFactors, Finset.mem_filter, Finset.mem_range]
  constructor
  · rintro ⟨hp, hd, -⟩
    have := Nat.le_of_dvd hu hd
    exact ⟨⟨by omega, hp⟩, hd⟩
  · rintro ⟨⟨-, hp⟩, hd⟩
    exact ⟨hp, hd, hu.ne'⟩

/-- (5.12)–(5.13): the truncated phase is `∑_p X_p`, the sum over the primes up to `B`. -/
private lemma phase_split {β : Type*} [Fintype β] (q : ℝ) (H B : ℕ) (c : β → ℝ) (d : ℕ → ℝ)
    (u : ℕ → β → ℕ) (hu : ∀ k < H, ∀ b, 0 < u k b ∧ u k b ≤ B) :
    q * ∑ k ∈ Finset.range H, (∑ b, c b * (ArithmeticFunction.cardDistinctFactors (u k b) : ℝ)) / d k
      = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime,
          q * ∑ k ∈ Finset.range H, (∑ b, c b * (if p ∣ u k b then (1 : ℝ) else 0)) / d k := by
  rw [← Finset.mul_sum, Finset.sum_comm]
  congr 1
  refine Finset.sum_congr rfl fun k hk => ?_
  rw [← Finset.sum_div, Finset.sum_comm]
  congr 1
  refine Finset.sum_congr rfl fun b _ => ?_
  have h := hu k (Finset.mem_range.mp hk) b
  rw [← Finset.mul_sum, ← omega_eq_sum_primes_le h.1 h.2]

private lemma avg_abs_add3 {ι : Type*} (s : Finset ι) (a b c : ι → ℝ) :
    (s.card : ℝ)⁻¹ * ∑ i ∈ s, |a i + b i + c i|
      ≤ (s.card : ℝ)⁻¹ * ∑ i ∈ s, |a i| + (s.card : ℝ)⁻¹ * ∑ i ∈ s, |b i|
        + (s.card : ℝ)⁻¹ * ∑ i ∈ s, |c i| := by
  rw [← mul_add, ← mul_add, ← Finset.sum_add_distrib, ← Finset.sum_add_distrib]
  exact mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun i _ => abs_add_three _ _ _)
    (inv_nonneg.mpr (Nat.cast_nonneg _))

/-- `E|f| ≤ (E f²)^{1/2}` (Cauchy–Schwarz), used for `κ₃` as defined in (5.14). -/
private lemma avg_abs_le_sqrt {ι : Type*} (s : Finset ι) (f : ι → ℝ) :
    (s.card : ℝ)⁻¹ * ∑ i ∈ s, |f i| ≤ Real.sqrt ((s.card : ℝ)⁻¹ * ∑ i ∈ s, f i ^ 2) := by
  have h0 : 0 ≤ (s.card : ℝ)⁻¹ * ∑ i ∈ s, |f i| :=
    mul_nonneg (inv_nonneg.mpr (Nat.cast_nonneg _)) (Finset.sum_nonneg fun i _ => abs_nonneg _)
  rw [← Real.sqrt_sq h0]
  apply Real.sqrt_le_sqrt
  have hcs := sq_sum_le_card_mul_sum_sq (s := s) (f := fun i => |f i|)
  simp only [sq_abs] at hcs
  rcases Nat.eq_zero_or_pos s.card with h | h
  · simp [h]
  · have hN : (0 : ℝ) < s.card := by exact_mod_cast h
    rw [mul_pow, inv_pow]
    calc ((s.card : ℝ) ^ 2)⁻¹ * (∑ i ∈ s, |f i|) ^ 2
        ≤ ((s.card : ℝ) ^ 2)⁻¹ * ((s.card : ℝ) * ∑ i ∈ s, f i ^ 2) :=
          mul_le_mul_of_nonneg_left (by exact_mod_cast hcs) (by positivity)
      _ = (s.card : ℝ)⁻¹ * ∑ i ∈ s, f i ^ 2 := by field_simp

/-- `|(-1)^{|ε|}| = 1`. -/
private lemma abs_sgn_le_one {K : ℕ} (ε : Fin K → Bool) :
    |∏ i, (if ε i then (-1 : ℝ) else 1)| ≤ 1 := by
  rw [Finset.abs_prod]
  refine le_of_eq (Finset.prod_eq_one fun i _ => ?_)
  split_ifs <;> simp

/-- `|q ∑_ε (-1)^{|ε|} ∑_k g| ≤ q ∑_ε ∑_k g` for `g ≥ 0`: the `δ_p` terms of (5.8) against `κ₁`. -/
private lemma abs_sgn_tsum_le {K : ℕ} (q : ℝ) (hq : 0 ≤ q) (sgn : (Fin K → Bool) → ℝ)
    (hsgn : ∀ ε, |sgn ε| ≤ 1) (g : (Fin K → Bool) → ℕ → ℝ) (hg : ∀ ε k, 0 ≤ g ε k) :
    |q * ∑ ε, sgn ε * ∑' k, g ε k| ≤ q * ∑ ε, ∑' k, g ε k := by
  rw [abs_mul, abs_of_nonneg hq]
  refine mul_le_mul_of_nonneg_left ((Finset.abs_sum_le_sum_abs _ _).trans
    (Finset.sum_le_sum fun ε _ => ?_)) hq
  rw [abs_mul, abs_of_nonneg (tsum_nonneg (hg ε))]
  exact mul_le_of_le_one_left (tsum_nonneg (hg ε)) (hsgn ε)

/-- Truncation (5.11): `|∑ T - ∑_{k<H} T| ≤ ∑_{k} G(k+H)` when `|T| ≤ G` and `G` is summable. -/
private lemma tail_abs_le (T G : ℕ → ℝ) (hT : ∀ k, |T k| ≤ G k) (hG : Summable G) (H : ℕ) :
    |∑' k, T k - ∑ k ∈ Finset.range H, T k| ≤ ∑' k, G (k + H) := by
  have hTs : Summable T :=
    Summable.of_norm_bounded hG (fun k => by rw [Real.norm_eq_abs]; exact hT k)
  rw [← hTs.sum_add_tsum_nat_add H, add_sub_cancel_left]
  have hTH : Summable fun k => T (k + H) := (summable_nat_add_iff H).mpr hTs
  have hGH : Summable fun k => G (k + H) := (summable_nat_add_iff H).mpr hG
  rw [abs_le]
  constructor
  · rw [← tsum_neg]
    exact Summable.tsum_le_tsum (fun k => neg_le_of_abs_le (hT (k + H))) hGH.neg hTH
  · exact Summable.tsum_le_tsum (fun k => le_of_abs_le (hT (k + H))) hTH hGH

private lemma abs_sgn_sum_div_le {β : Type*} [Fintype β] (sgn : β → ℝ) (hs : ∀ b, |sgn b| ≤ 1)
    (w : β → ℝ) (hw : ∀ b, 0 ≤ w b) (d : ℝ) (hd : 0 < d) :
    |(∑ b, sgn b * w b) / d| ≤ (∑ b, w b) / d := by
  rw [abs_div, abs_of_pos hd]
  exact div_le_div_of_nonneg_right ((Finset.abs_sum_le_sum_abs _ _).trans
    (Finset.sum_le_sum fun b _ => by
      rw [abs_mul, abs_of_nonneg (hw b)]; exact mul_le_of_le_one_left (hw b) (hs b))) hd.le

/-- Erdős 257 for `A` the set of primes: `∑_p 1/(2^p - 1) = ∑_{n ≥ 1} ω(n)/2^n` is irrational
(this is also Erdős problem 69). Known: T. Tao and J. Teräväinen, *Quantitative correlations and
some problems on prime factors of consecutive integers*, arXiv:2512.01739 (v2, 2026), Theorem 1.3
(§1.3); earlier conditional on a uniform prime tuples conjecture (Pratt 2024). The paper only
sketches the analogous case of the prime powers ("we leave the details … to the interested
reader"), so that case is not stated here. -/
theorem Opn.erdos_257_primes :
    Irrational (∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1)) := by
  -- approach: Tao–Teräväinen §5: rationality forces (5.8) on every prime cube; averaging e(phase) over n and the variance bound (5.20) contradict Theorem 5.1
  -- licence: CC-BY-4.0
  -- label: Tao–Teräväinen Theorem 5.1 (technical reduction)
  -- claim: $\forall q \ge 1\ \exists c_0 > 0\ \forall \delta > 0:\ \text{a prime cube } p_\varepsilon = p_0 + \textstyle\sum_k \varepsilon_k v_k,
  -- \text{ a finite set of } n \text{ obeying (5.5)--(5.6), a split } S_0 \sqcup S_1 \sqcup S_2 \text{ of the primes} \le B
  -- \text{ with } X_p \text{ constant on } S_0,\ |X_p| \le \delta \text{ on } S_1,\ M \text{ even},\ \kappa_1, \dots, \kappa_5 \le \delta,\ \sum_{p \in S_1} \operatorname{Var} X_p \ge c_0$
  -- Theorem 5.1 of arXiv:2512.01739v2 (p. 49) for n uniform on the finite set Ns, where
  -- X_p(n) = q sum_{1<=h<=H} sum_eps (-1)^|eps| 1[p | n + r_{eps,h+K}] / 2^(h+K) is (5.13), and
  -- kappa_1, kappa_2, kappa_3 are (5.10), (5.11), (5.14) as printed (expectation and sums swapped).
  -- kappa_4, kappa_5 are (5.16), (5.17) as printed, with X'_p independent copies (n uniform on
  -- Ns^{S_1}) and e = exp 1 in (5.17). One part is taken from the paper's proof rather than its
  -- statement: (5.19), |X_p| small on S_1 (shown in section 5.6 from (5.36)). It does not mention the sum or
  -- rationality: it is about prime factors of shifted integers, and in the paper its proof rests on
  -- the paper's Theorem 3.1 and on Lemma 5.2.
  have h1 : ∀ q : ℕ, 0 < q → ∃ c0 : ℝ, 0 < c0 ∧ ∀ δ : ℝ, 0 < δ →
      ∃ (K : ℕ) (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ) (H B : ℕ) (Ns : Finset ℤ)
        (m : ℤ → (Fin K → Bool) → ℕ) (S1 S2 : Finset ℕ) (sgn : (Fin K → Bool) → ℝ)
        (r : (Fin K → Bool) → ℕ → ℤ) (X : ℕ → ℤ → ℝ) (M : ℕ),
        (∀ ε, sgn ε = ∏ i, if ε i then (-1 : ℝ) else 1) ∧
        (∀ ε (h : ℕ), r ε h = p0 * h + ∑ k : Fin K, ((h : ℤ) - (k.1 + 1)) * (if ε k then v k else 0)) ∧
        (∀ p n, X p n = (q : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (if p ∣ (n + r ε (k + K + 1)).toNat then (1 : ℝ) else 0)) / 2 ^ (k + K + 1)) ∧
        Even M ∧
        (∀ ε, (P ε : ℤ) = p0 + ∑ k : Fin K, (if ε k then v k else 0)) ∧ (∀ ε, (P ε).Prime) ∧
        Ns.Nonempty ∧
        (∀ n ∈ Ns, ∀ ε, n - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) = P ε * m n ε) ∧
        (∀ n ∈ Ns, ∀ ε, ∀ k < H, 0 < n + r ε (k + K + 1) ∧ n + r ε (k + K + 1) ≤ B) ∧
        S1 ⊆ (Finset.range (B + 1)).filter Nat.Prime ∧ S2 ⊆ (Finset.range (B + 1)).filter Nat.Prime ∧
        Disjoint S1 S2 ∧
        (∀ p ∈ (Finset.range (B + 1)).filter Nat.Prime, p ∉ S1 → p ∉ S2 →
          ∀ n ∈ Ns, ∀ n' ∈ Ns, X p n = X p n') ∧
        (∀ p ∈ S1, ∀ n ∈ Ns, |X p n| ≤ δ) ∧
        (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) ≤ δ ∧
        (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' k : ℕ, (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
            / 2 ^ (k + H + K + 1) ≤ δ ∧
        Real.sqrt ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S2, X p n) ^ 2) ≤ δ ∧
        ∑ j ∈ Finset.range (M + 1), (2 * Real.pi) ^ j / j.factorial
          * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ j
            - ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ)⁻¹
              * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), (∑ p : S1, X p (ω p)) ^ j| ≤ δ ∧
        (∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp (2 * Real.pi * Real.exp 1 * X p n)
            + ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
              Real.exp (-(2 * Real.pi * Real.exp 1) * X p n)) / Real.exp 1 ^ M ≤ δ ∧
        c0 ≤ ∑ p ∈ S1, ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, X p n ^ 2
          - ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, X p n) ^ 2) := sorry
  have htay := taylor_all
  rintro ⟨r0, hr0⟩
  have hq : 0 < r0.den := r0.den_pos
  have hsum : ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) = (r0.num : ℝ) / (r0.den : ℕ) := by
    rw [← hr0, Rat.cast_def]
  obtain ⟨c0, hc0, hall⟩ := h1 r0.den hq
  have hex : Real.exp (-8 * c0) < 1 := by
    have := Real.exp_lt_exp.mpr (show -8 * c0 < 0 by linarith)
    rwa [Real.exp_zero] at this
  have hδ : 0 < min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := lt_min (by norm_num) (by linarith)
  have hδ1 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) ≤ 1 / 4 := min_le_left _ _
  have hδ2 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) ≤ (1 - Real.exp (-8 * c0)) / 64 :=
    min_le_right _ _
  obtain ⟨K, p0, v, P, H, B, Ns, m, S1, S2, sgn, r, X, M, hsgn, hr, hX, hMeven, hP, hpr, hNs, hm, hrange,
    hS1, hS2, hdisj, hS0, hXs, hk1, hk2, hk3, hk4, hk5, hvar⟩ :=
    hall (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) (div_pos hδ (by positivity))
  have hq1 : (1 : ℝ) ≤ r0.den := by exact_mod_cast hq
  have hd1 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den
      ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := div_le_self hδ.le hq1
  have hd2 : (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den)
      = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) :=
    mul_div_cancel₀ _ (by positivity)
  have hsgn' : cubeSgn = sgn := funext fun ε => by rw [hsgn ε]; rfl
  have hr' : cubeShift p0 v = r := funext fun ε => funext fun h => by rw [hr ε h]; rfl
  obtain ⟨n0, hn0⟩ := hNs
  have key := assembly_core Ns ⟨n0, hn0⟩ S1 X
    (fun n => (r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)
      + ((r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
          - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1))
      + ∑ p ∈ S2, X p n)
    (∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n0)
    (4 * min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64)) c0 ?_
    (fun p hp n hn => ((hXs p hp n hn).trans hd1).trans hδ1) ?_ ((decor_bound Ns S1 X M hMeven htay).trans (by linarith)) hvar
  · linarith
  · intro n hn
    obtain ⟨z, hz⟩ := rational_imp_5_8 r0.num r0.den hq hsum p0 v P hP hpr n (m n) (hm n hn)
    rw [hsgn', hr'] at hz
    refine ⟨z, ?_⟩
    have hu : ∀ k < H, ∀ ε : Fin K → Bool,
        0 < (n + r ε (k + K + 1)).toNat ∧ (n + r ε (k + K + 1)).toNat ≤ B := by
      intro k hk ε
      have h := hrange n hn ε k hk
      constructor <;> omega
    have hsplit := phase_split (r0.den : ℝ) H B sgn (fun k => (2 : ℝ) ^ (k + K + 1))
      (fun k ε => (n + r ε (k + K + 1)).toNat) hu
    beta_reduce at hsplit
    have hPB : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, X p n
        = ∑ p ∈ S1, X p n + ∑ p ∈ S2, X p n
          + ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n := by
      rw [← Finset.sum_union hdisj]
      exact (Finset.sum_sdiff (Finset.union_subset hS1 hS2)).symm.trans (add_comm _ _)
    have hC : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n
        = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n0 := by
      refine Finset.sum_congr rfl fun p hp => ?_
      have hp' := Finset.mem_sdiff.mp hp
      exact hS0 p hp'.1 (fun h => hp'.2 (Finset.mem_union_left _ h))
        (fun h => hp'.2 (Finset.mem_union_right _ h)) n hn n0 hn0
    have hXsum : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime,
        (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (if p ∣ (n + r ε (k + K + 1)).toNat then (1 : ℝ) else 0)) / 2 ^ (k + K + 1)
        = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, X p n :=
      Finset.sum_congr rfl fun p _ => (hX p n).symm
    linarith
  · refine (avg_abs_add3 Ns _ _ _).trans ?_
    have e1 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
        ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := by
      have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε
      calc (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
          ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑ ε : Fin K → Bool, ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) :=
            mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun n _ =>
              abs_sgn_tsum_le _ (Nat.cast_nonneg _) sgn hs1 _ fun ε k =>
                div_nonneg (by split_ifs <;> norm_num) (by positivity))
              (inv_nonneg.mpr (Nat.cast_nonneg _))
        _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)) := by
            rw [← Finset.mul_sum]; ring
        _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
            mul_le_mul_of_nonneg_left hk1 (Nat.cast_nonneg _)
        _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2
    have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε
    have e2 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
          - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
        ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := by
      have hper : ∀ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
          - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
          ≤ (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
            / 2 ^ (k + H + K + 1) := by
        intro n hn
        have hsh : ∀ ε (h : ℕ), (n + r ε h).toNat = P ε * m n ε + P ε * h := by
          intro ε h
          have : n + r ε h = ((P ε * m n ε + P ε * h : ℕ) : ℤ) := by
            rw [← hr', cubeShift_eq, ← hP ε]; push_cast; linear_combination hm n hn ε
          rw [this, Int.toNat_natCast]
        have hG : Summable fun k : ℕ => (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ))
            / 2 ^ (k + K + 1) := by
          simp_rw [Finset.sum_div]
          refine summable_sum fun ε _ => ?_
          have hb := omega_bound_summable (fun k => P ε * m n ε + P ε * (k + K + 1))
            (P ε * m n ε + P ε * (K + 1)) (P ε) (fun k => le_of_eq (by ring))
          refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) hb
          rw [hsh ε]
          exact div_le_div_of_nonneg_left (by positivity) (by positivity)
            (pow_le_pow_right₀ (by norm_num) (by omega))
        rw [← mul_sub, abs_mul, abs_of_nonneg (Nat.cast_nonneg _)]
        exact mul_le_mul_of_nonneg_left (tail_abs_le _ _ (fun k => abs_sgn_sum_div_le sgn hs1 _
          (fun ε => Nat.cast_nonneg _) _ (by positivity)) hG H) (Nat.cast_nonneg _)
      calc _ ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
            / 2 ^ (k + H + K + 1) :=
            mul_le_mul_of_nonneg_left (Finset.sum_le_sum hper) (inv_nonneg.mpr (Nat.cast_nonneg _))
        _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' k : ℕ, (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
            / 2 ^ (k + H + K + 1)) := by rw [← Finset.mul_sum]; ring
        _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
            mul_le_mul_of_nonneg_left hk2 (Nat.cast_nonneg _)
        _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2
    have e3 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |∑ p ∈ S2, X p n|
        ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) :=
      ((avg_abs_le_sqrt Ns fun n => ∑ p ∈ S2, X p n).trans hk3).trans hd1
    linarith
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

Literature: Known: a proof is published and not formalised · proposed by t1022-d, awaiting a steward or curator

References and summary
Literature status

Proposed by t1022-d, awaiting a steward or curator: Known: a proof is published and not formalised (record, 2026-10-22T13:47:27Z).

Untrusted contributor text — a literature summary (D-25 v3.35): the contributor’s reading, not a verdict.

Untrusted: summary by t1022-d, drafted with Claude Opus 5.5 (Claude Code); papers read 2026-10-22 from the linked PDFs. Rendered from targets/erdos-257/nodes/variant-6b364d2b/literature/20261022T134727Z-t1022-d.yaml.

Known (Tao–Teräväinen 2025/26, Theorem 1.3), by quantitative correlation estimates for ω on consecutive integers; a deep analytic result, not formalised anywhere I could find. The analogous statement for the prime powers is only sketched in that paper.

Work on it

Plans

  • Source: T. — not followed by any merged decomposition · full text →

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.«variant-6b364d2b».Context

/-- Erdős 257 for `A` the set of primes: `∑_p 1/(2^p - 1) = ∑_{n ≥ 1} ω(n)/2^n` is irrational
(this is also Erdős problem 69). Known: T. Tao and J. Teräväinen, *Quantitative correlations and
some problems on prime factors of consecutive integers*, arXiv:2512.01739 (v2, 2026), Theorem 1.3
(§1.3); earlier conditional on a uniform prime tuples conjecture (Pratt 2024). The paper only
sketches the analogous case of the prime powers ("we leave the details … to the interested
reader"), so that case is not stated here. -/
theorem Opn.erdos_257_primes :
    Irrational (∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1)) := 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 t1022-e, drafted with Claude Opus 5.5 (claude-opus-5-5), 2026-10-22; no one has read it against the Lean and signed it.

Draft · drafted, by the model named above

$\displaystyle \sum_{p \text{ prime}}\frac{1}{2^p-1} \ \text{ is irrational}.$

The case $A = \{\text{primes}\}$ of the root. The sum is Lean's tsum over the subtype $\{p : p \text{ prime}\}$, the ordinary sum of a convergent positive series; $0$ and $1$ are not prime, so no junk value $\frac10$ occurs.

Rendered from gloss/21af85c140e6….md · Improve these words →

Rendered from targets/erdos-257/nodes/variant-6b364d2b/Statement.lean. Origin: variant (partial).

Depends on (1), each in words

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

  • variant-6b364d2b--h1: $\forall q \ge 1\ \exists c_0 > 0\ \forall \delta > 0:\ \text{a prime cube } p_\varepsilon = p_0 + \textstyle\sum_k \varepsilon_k v_k, \text{ a finite set of } n \text{ obeying (5.5)--(5.6), a split } S_0 \sqcup S_1 \sqcup S_2 \text{ of the primes} \le B \text{ with } X_p \text{ constant on } S_0,\ |X_p| \le \delta \text{ on } S_1,\ M \text{ even},\ \kappa_1, \dots, \kappa_5 \le \delta,\ \sum_{p \in S_1} \operatorname{Var} X_p \ge c_0$
Attestation: none of a proof

No proof of this statement has been merged. A partial assembly merged under attestations/000948.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-257/nodes/variant-6b364d2b/Witness.lean, sha256 38a9ced51f18.
theorem witness : True := trivial

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 →

Relation to the problem’s statement

Checked by the kernel — checked at admission (D-30); no proof of the node is implied.

Relation.lean — the implication between this variant and the problem’s statement, checked by the gate when the variant was admitted and immutable after (D-30). Rendered from targets/erdos-257/nodes/variant-6b364d2b/Relation.lean, sha256 d8c29d0eab38.
-- relation: partial
import Mathlib

theorem relation :
    (∀ (A : Set ℕ), A.Infinite → Irrational (∑' n : A, (1 : ℝ) / (2 ^ n.1 - 1))) →
    Irrational (∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1)) := by
  intro h
  exact h _ Nat.infinite_setOf_prime

In words, unverified — what this relation says; written by t1022-k, drafted with Claude Opus 5.5 (claude-opus-5-5), 2026-10-22; no one has read it against the Lean and signed it.

Draft · drafted, by the model named above

$\displaystyle \Bigl(\forall\, A \subseteq \mathbb{N} \text{ infinite}:\ \sum_{n\in A}\frac{1}{2^n-1} \text{ is irrational}\Bigr) \;\Longrightarrow\; \sum_{p \text{ prime}}\frac{1}{2^p-1} \text{ is irrational}.$

The root's statement implies this variant (relation partial): the set of primes is infinite (Mathlib's Nat.infinite_setOf_prime), and the root's statement applies to it.

Rendered from gloss/3ba04a7ebcfc….md · Improve these words →

Attempts (3)

3 recorded. Routes refuted: none. Failure classes: missing-library 2.

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 t1022-l; merged in f0822873b58e under attestations/000948.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-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-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 20261022T160309Z-t1022-l-partial.lean, extracted by the gate at 1bd7ca70c9ee 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. h1 hole ∀ (q : ℕ), (0 : ℕ) < q → ∃ (c0 : ℝ), (0 : ℝ) < c0 ∧ ∀ (δ : ℝ), (0 : ℝ) < δ → ∃ (K : ℕ) (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ) (H : ℕ) (B : ℕ) (Ns : Finset ℤ) (m : ℤ → (Fin K → Bool) → ℕ) (S1 : Finset ℕ) (S2 : Finset ℕ) (sgn : (Fin K → Bool) → ℝ) (r : (Fin K → Bool) → ℕ → ℤ) (X : ℕ → ℤ → ℝ) (M : ℕ), (∀ (ε : Fin K → Bool), sgn ε = ∏ i : Fin K, if ε i = true then (-1 : ℝ) else (1 : ℝ)) ∧ (∀ (ε : Fin K → Bool) (h : ℕ), r ε h = p0 * (↑h : ℤ) + ∑ k : Fin K, ((↑h : ℤ) - ((↑(↑k : ℕ) : ℤ) + (1 : ℤ))) * if ε k = true then v k else (0 : ℤ)) ∧ (∀ (p : ℕ) (n : ℤ), X p n = (↑q : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * if p ∣ (n + r ε (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) ∧ Even M ∧ (∀ (ε : Fin K → Bool), (↑(P ε) : ℤ) = p0 + ∑ k : Fin K, if ε k = true then v k else (0 : ℤ)) ∧ (∀ (ε : Fin K → Bool), Nat.Prime (P ε)) ∧ Ns.Nonempty ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), (n - ∑ k : Fin K, ((↑(↑k : ℕ) : ℤ) + (1 : ℤ)) * if ε k = true then v k else (0 : ℤ)) = (↑(P ε) : ℤ) * (↑(m n ε) : ℤ)) ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), ∀ k < H, (0 : ℤ) < n + r ε (k + K + (1 : ℕ)) ∧ n + r ε (k + K + (1 : ℕ)) ≤ (↑B : ℤ)) ∧ S1 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ (truncated) hole

    Leaves Irrational (∑' (p : (↑{p : ℕ | Nat.Prime p} : Type)), (1 : ℝ) / ((2 : ℝ) ^ (↑p : ℕ) - (1 : ℝ))), with h1 : ∀ (q : ℕ), (0 : ℕ) < q → ∃ (c0 : ℝ), (0 : ℝ) < c0 ∧ ∀ (δ : ℝ), (0 : ℝ) < δ → ∃ (K : ℕ) (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ) (H : ℕ) (B : ℕ) (Ns : Finset ℤ) (m : ℤ → (Fin K → Bool) → ℕ) (S1 : Finset ℕ) (S2 : Finset ℕ) (sgn : (Fin K → Bool) → ℝ) (r : (Fin K → Bool) → ℕ → ℤ) (X : ℕ → ℤ → ℝ) (M : ℕ), (∀ (ε : Fin K → Bool), sgn ε = ∏ i : Fin K, if ε i = true then (-1 : ℝ) else (1 : ℝ)) ∧ (∀ (ε : Fin K → Bool) (h : ℕ), r ε h = p0 * (↑h : ℤ) + ∑ k : Fin K, ((↑h : ℤ) - ((↑(↑k : ℕ) : ℤ) + (1 : ℤ))) * if ε k = true then v k else (0 : ℤ)) ∧ (∀ (p : ℕ) (n : ℤ), X p n = (↑q : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * if p ∣ (n + r ε (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) ∧ Even M ∧ (∀ (ε : Fin K → Bool), (↑(P ε) : ℤ) = p0 + ∑ k : Fin K, if ε k = true then v k else (0 : ℤ)) ∧ (∀ (ε : Fin K → Bool), Nat.Prime (P ε)) ∧ Ns.Nonempty ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), (n - ∑ k : Fin K, ((↑(↑k : ℕ) : ℤ) + (1 : ℤ)) * if ε k = true then v k else (0 : ℤ)) = (↑(P ε) : ℤ) * (↑(m n ε) : ℤ)) ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), ∀ k < H, (0 : ℤ) < n + r ε (k + K + (1 : ℕ)) ∧ n + r ε (k + K + (1 : ℕ)) ≤ (↑B : ℤ)) ∧ S1 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ (truncated).

    Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , Finset Finset α is the type of finite sets of elements of α. , Finset.range range n is the set of natural numbers less than n. , Even An element a of a type α with addition satisfies Even a if a = r + r, for some r : α. , 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. , Disjoint Two elements of a lattice are disjoint if their inf is the bottom element. , ArithmeticFunction.cardDistinctFactors ω n is the number of distinct prime factors of n. , Real.sqrt The square root of a real number. , Real.pi The number π = 3.14159265... , Fintype.piFinset Given for all a : α a finset t a of δ a, then one can define the finset Fintype.piFinset t of all functions taking values in t a for all a. , Real.exp The real exponential function, defined as the real part of the complex exponential .

    Left open as a hole; its statement is variant-6b364d2b--h1 (open).

    Lean, lines 1004–1036
      have h1 : ∀ q : ℕ, 0 < q → ∃ c0 : ℝ, 0 < c0 ∧ ∀ δ : ℝ, 0 < δ →
          ∃ (K : ℕ) (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ) (H B : ℕ) (Ns : Finset ℤ)
            (m : ℤ → (Fin K → Bool) → ℕ) (S1 S2 : Finset ℕ) (sgn : (Fin K → Bool) → ℝ)
            (r : (Fin K → Bool) → ℕ → ℤ) (X : ℕ → ℤ → ℝ) (M : ℕ),
            (∀ ε, sgn ε = ∏ i, if ε i then (-1 : ℝ) else 1) ∧
            (∀ ε (h : ℕ), r ε h = p0 * h + ∑ k : Fin K, ((h : ℤ) - (k.1 + 1)) * (if ε k then v k else 0)) ∧
            (∀ p n, X p n = (q : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                (if p ∣ (n + r ε (k + K + 1)).toNat then (1 : ℝ) else 0)) / 2 ^ (k + K + 1)) ∧
            Even M ∧
            (∀ ε, (P ε : ℤ) = p0 + ∑ k : Fin K, (if ε k then v k else 0)) ∧ (∀ ε, (P ε).Prime) ∧
            Ns.Nonempty ∧
            (∀ n ∈ Ns, ∀ ε, n - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) = P ε * m n ε) ∧
            (∀ n ∈ Ns, ∀ ε, ∀ k < H, 0 < n + r ε (k + K + 1) ∧ n + r ε (k + K + 1) ≤ B) ∧
            S1 ⊆ (Finset.range (B + 1)).filter Nat.Prime ∧ S2 ⊆ (Finset.range (B + 1)).filter Nat.Prime ∧
            Disjoint S1 S2 ∧
            (∀ p ∈ (Finset.range (B + 1)).filter Nat.Prime, p ∉ S1 → p ∉ S2 →
              ∀ n ∈ Ns, ∀ n' ∈ Ns, X p n = X p n') ∧
            (∀ p ∈ S1, ∀ n ∈ Ns, |X p n| ≤ δ) ∧
            (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' k : ℕ,
                (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) ≤ δ ∧
            (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' k : ℕ, (∑ ε : Fin K → Bool,
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                / 2 ^ (k + H + K + 1) ≤ δ ∧
            Real.sqrt ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S2, X p n) ^ 2) ≤ δ ∧
            ∑ j ∈ Finset.range (M + 1), (2 * Real.pi) ^ j / j.factorial
              * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ j
                - ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ)⁻¹
                  * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), (∑ p : S1, X p (ω p)) ^ j| ≤ δ ∧
            (∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp (2 * Real.pi * Real.exp 1 * X p n)
                + ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
                  Real.exp (-(2 * Real.pi * Real.exp 1) * X p n)) / Real.exp 1 ^ M ≤ δ ∧
            c0 ≤ ∑ p ∈ S1, ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, X p n ^ 2
              - ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, X p n) ^ 2) := sorry

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1004–1036 at 60a537454ac8 ↗

  2. htay have ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((↑x : ℂ) * Complex.I) - ∑ m ∈ Finset.range M, ((↑x : ℂ) * Complex.I) ^ m / (↑m.factorial : ℂ)‖ ≤ |x| ^ M / (↑M.factorial : ℝ)

    Leaves Irrational (∑' (p : (↑{p : ℕ | Nat.Prime p} : Type)), (1 : ℝ) / ((2 : ℝ) ^ (↑p : ℕ) - (1 : ℝ))), with htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((↑x : ℂ) * Complex.I) - ∑ m ∈ Finset.range M, ((↑x : ℂ) * Complex.I) ^ m / (↑m.factorial : ℂ)‖ ≤ |x| ^ M / (↑M.factorial : ℝ).

    Lean, line 1037
      have htay := taylor_all

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1037 at 60a537454ac8 ↗

  3. hq have (0 : ℕ) < r0.den

    Leaves False, with hq : (0 : ℕ) < r0.den.

    Lean, line 1039
      have hq : 0 < r0.den := r0.den_pos

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1039 at 60a537454ac8 ↗

  4. hsum have ∑' (p : (↑{p : ℕ | Nat.Prime p} : Type)), (1 : ℝ) / ((2 : ℝ) ^ (↑p : ℕ) - (1 : ℝ)) = (↑r0.num : ℝ) / (↑r0.den : ℝ)

    Leaves False, with hsum : ∑' (p : (↑{p : ℕ | Nat.Prime p} : Type)), (1 : ℝ) / ((2 : ℝ) ^ (↑p : ℕ) - (1 : ℝ)) = (↑r0.num : ℝ) / (↑r0.den : ℝ).

    Uses library Rat.cast_def.

    Lean, lines 1040–1041
      have hsum : ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) = (r0.num : ℝ) / (r0.den : ℕ) := by
        rw [← hr0, Rat.cast_def]

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1040–1041 at 60a537454ac8 ↗

  5. s5 obtain ∃ (c0 : ℝ), (0 : ℝ) < c0 ∧ ∀ (δ : ℝ), (0 : ℝ) < δ → ∃ (K : ℕ) (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ) (H : ℕ) (B : ℕ) (Ns : Finset ℤ) (m : ℤ → (Fin K → Bool) → ℕ) (S1 : Finset ℕ) (S2 : Finset ℕ) (sgn : (Fin K → Bool) → ℝ) (r : (Fin K → Bool) → ℕ → ℤ) (X : ℕ → ℤ → ℝ) (M : ℕ), (∀ (ε : Fin K → Bool), sgn ε = ∏ i : Fin K, if ε i = true then (-1 : ℝ) else (1 : ℝ)) ∧ (∀ (ε : Fin K → Bool) (h : ℕ), r ε h = p0 * (↑h : ℤ) + ∑ k : Fin K, ((↑h : ℤ) - ((↑(↑k : ℕ) : ℤ) + (1 : ℤ))) * if ε k = true then v k else (0 : ℤ)) ∧ (∀ (p : ℕ) (n : ℤ), X p n = (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * if p ∣ (n + r ε (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) ∧ Even M ∧ (∀ (ε : Fin K → Bool), (↑(P ε) : ℤ) = p0 + ∑ k : Fin K, if ε k = true then v k else (0 : ℤ)) ∧ (∀ (ε : Fin K → Bool), Nat.Prime (P ε)) ∧ Ns.Nonempty ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), (n - ∑ k : Fin K, ((↑(↑k : ℕ) : ℤ) + (1 : ℤ)) * if ε k = true then v k else (0 : ℤ)) = (↑(P ε) : ℤ) * (↑(m n ε) : ℤ)) ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), ∀ k < H, (0 : ℤ) < n + r ε (k + K + (1 : ℕ)) ∧ n + r ε (k + K + (1 : ℕ)) ≤ (↑B : ℤ)) ∧ S1 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ S2 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ Disjoint S1 S2 ∧ (∀ p ∈ Finset.filter Nat.Prime (Finset.r (truncated)

    Leaves False, with c0 : ℝ, hc0 : (0 : ℝ) < c0, hall : ∀ (δ : ℝ), (0 : ℝ) < δ → ∃ (K : ℕ) (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ) (H : ℕ) (B : ℕ) (Ns : Finset ℤ) (m : ℤ → (Fin K → Bool) → ℕ) (S1 : Finset ℕ) (S2 : Finset ℕ) (sgn : (Fin K → Bool) → ℝ) (r : (Fin K → Bool) → ℕ → ℤ) (X : ℕ → ℤ → ℝ) (M : ℕ), (∀ (ε : Fin K → Bool), sgn ε = ∏ i : Fin K, if ε i = true then (-1 : ℝ) else (1 : ℝ)) ∧ (∀ (ε : Fin K → Bool) (h : ℕ), r ε h = p0 * (↑h : ℤ) + ∑ k : Fin K, ((↑h : ℤ) - ((↑(↑k : ℕ) : ℤ) + (1 : ℤ))) * if ε k = true then v k else (0 : ℤ)) ∧ (∀ (p : ℕ) (n : ℤ), X p n = (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * if p ∣ (n + r ε (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) ∧ Even M ∧ (∀ (ε : Fin K → Bool), (↑(P ε) : ℤ) = p0 + ∑ k : Fin K, if ε k = true then v k else (0 : ℤ)) ∧ (∀ (ε : Fin K → Bool), Nat.Prime (P ε)) ∧ Ns.Nonempty ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), (n - ∑ k : Fin K, ((↑(↑k : ℕ) : ℤ) + (1 : ℤ)) * if ε k = true then v k else (0 : ℤ)) = (↑(P ε) : ℤ) * (↑(m n ε) : ℤ)) ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), ∀ k < H, (0 : ℤ) < n + r ε (k + K + (1 : ℕ)) ∧ n + r ε (k + K + (1 : ℕ)) ≤ (↑B : ℤ)) ∧ S1 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ S2 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ Disjoint S1 S2 ∧ (∀ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))), p ∉ S1 → p ∉ S2 → ∀ n ∈ Ns, ∀ n' ∈ Ns, X p n = X p n') ∧ (∀ p ∈ S1, ∀ n ∈ Ns, |X p n| ≤ δ) ∧ (truncated).

    Lean, line 1042
      obtain ⟨c0, hc0, hall⟩ := h1 r0.den hq

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1042 at 60a537454ac8 ↗

  6. hex have Real.exp ((-8 : ℝ) * c0) < (1 : ℝ)

    Leaves False, with hex : Real.exp ((-8 : ℝ) * c0) < (1 : ℝ).

    Uses library Real.exp The real exponential function, defined as the real part of the complex exponential , Real.exp_zero.

    Lean, lines 1043–1045
      have hex : Real.exp (-8 * c0) < 1 := by
        have := Real.exp_lt_exp.mpr (show -8 * c0 < 0 by linarith)
        rwa [Real.exp_zero] at this

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1043–1045 at 60a537454ac8 ↗

    1. hex.this have Real.exp ((-8 : ℝ) * c0) < Real.exp (0 : ℝ)

      Leaves Real.exp ((-8 : ℝ) * c0) < (1 : ℝ), with this : Real.exp ((-8 : ℝ) * c0) < Real.exp (0 : ℝ).

      Uses library Real.exp_lt_exp.

      Lean, line 1044
          have := Real.exp_lt_exp.mpr (show -8 * c0 < 0 by linarith)

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1044 at 60a537454ac8 ↗

  7. h_x3b4 (hδ) have (0 : ℝ) < min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))

    Leaves False, with hδ : (0 : ℝ) < min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)).

    Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential , lt_min.

    Lean, line 1046
      have hδ : 0 < min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := lt_min (by norm_num) (by linarith)

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1046 at 60a537454ac8 ↗

  8. h_x3b41 (hδ1) have min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) ≤ (1 / 4 : ℝ)

    Leaves False, with hδ1 : min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) ≤ (1 / 4 : ℝ).

    Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential , min_le_left.

    Lean, line 1047
      have hδ1 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) ≤ 1 / 4 := min_le_left _ _

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1047 at 60a537454ac8 ↗

  9. h_x3b42 (hδ2) have min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) ≤ ((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)

    Leaves False, with hδ2 : min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) ≤ ((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ).

    Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential , min_le_right.

    Lean, lines 1048–1049
      have hδ2 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) ≤ (1 - Real.exp (-8 * c0)) / 64 :=
        min_le_right _ _

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1048–1049 at 60a537454ac8 ↗

  10. s10 obtain ∃ (K : ℕ) (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ) (H : ℕ) (B : ℕ) (Ns : Finset ℤ) (m : ℤ → (Fin K → Bool) → ℕ) (S1 : Finset ℕ) (S2 : Finset ℕ) (sgn : (Fin K → Bool) → ℝ) (r : (Fin K → Bool) → ℕ → ℤ) (X : ℕ → ℤ → ℝ) (M : ℕ), (∀ (ε : Fin K → Bool), sgn ε = ∏ i : Fin K, if ε i = true then (-1 : ℝ) else (1 : ℝ)) ∧ (∀ (ε : Fin K → Bool) (h : ℕ), r ε h = p0 * (↑h : ℤ) + ∑ k : Fin K, ((↑h : ℤ) - ((↑(↑k : ℕ) : ℤ) + (1 : ℤ))) * if ε k = true then v k else (0 : ℤ)) ∧ (∀ (p : ℕ) (n : ℤ), X p n = (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * if p ∣ (n + r ε (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) ∧ Even M ∧ (∀ (ε : Fin K → Bool), (↑(P ε) : ℤ) = p0 + ∑ k : Fin K, if ε k = true then v k else (0 : ℤ)) ∧ (∀ (ε : Fin K → Bool), Nat.Prime (P ε)) ∧ Ns.Nonempty ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), (n - ∑ k : Fin K, ((↑(↑k : ℕ) : ℤ) + (1 : ℤ)) * if ε k = true then v k else (0 : ℤ)) = (↑(P ε) : ℤ) * (↑(m n ε) : ℤ)) ∧ (∀ n ∈ Ns, ∀ (ε : Fin K → Bool), ∀ k < H, (0 : ℤ) < n + r ε (k + K + (1 : ℕ)) ∧ n + r ε (k + K + (1 : ℕ)) ≤ (↑B : ℤ)) ∧ S1 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ S2 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ Disjoint S1 S2 ∧ (∀ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))), p ∉ S1 → p ∉ S2 → ∀ n ∈ Ns, ∀ n' ∈ Ns, X p n = X p n') ∧ (∀ p ∈ S1, ∀ n ∈ Ns, |X p n| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / ( (truncated)

    Leaves False, with K : ℕ, p0 : ℤ, v : Fin K → ℤ, P : (Fin K → Bool) → ℕ, H : ℕ, B : ℕ, Ns : Finset ℤ, m : ℤ → (Fin K → Bool) → ℕ, S1 : Finset ℕ, S2 : Finset ℕ, sgn : (Fin K → Bool) → ℝ, r : (Fin K → Bool) → ℕ → ℤ, X : ℕ → ℤ → ℝ, M : ℕ, hsgn : ∀ (ε : Fin K → Bool), sgn ε = ∏ i : Fin K, if ε i = true then (-1 : ℝ) else (1 : ℝ), hr : ∀ (ε : Fin K → Bool) (h : ℕ), r ε h = p0 * (↑h : ℤ) + ∑ k : Fin K, ((↑h : ℤ) - ((↑(↑k : ℕ) : ℤ) + (1 : ℤ))) * if ε k = true then v k else (0 : ℤ), hX : ∀ (p : ℕ) (n : ℤ), X p n = (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * if p ∣ (n + r ε (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)), hMeven : Even M, hP : ∀ (ε : Fin K → Bool), (↑(P ε) : ℤ) = p0 + ∑ k : Fin K, if ε k = true then v k else (0 : ℤ), hpr : ∀ (ε : Fin K → Bool), Nat.Prime (P ε), hNs : Ns.Nonempty, hm : ∀ n ∈ Ns, ∀ (ε : Fin K → Bool), (n - ∑ k : Fin K, ((↑(↑k : ℕ) : ℤ) + (1 : ℤ)) * if ε k = true then v k else (0 : ℤ)) = (↑(P ε) : ℤ) * (↑(m n ε) : ℤ), hrange : ∀ n ∈ Ns, ∀ (ε : Fin K → Bool), ∀ k < H, (0 : ℤ) < n + r ε (k + K + (1 : ℕ)) ∧ n + r ε (k + K + (1 : ℕ)) ≤ (↑B : ℤ), hS1 : S1 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))), hS2 : S2 ⊆ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))), hdisj : Disjoint S1 S2, hS0 : ∀ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))), p ∉ S1 → p ∉ S2 → ∀ n ∈ Ns, ∀ n' ∈ Ns, X p n = X p n', hXs : ∀ p ∈ S1, ∀ n ∈ Ns, |X p n| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ), hk1 : (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ), hk2 : (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' (k : ℕ), (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + H + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + H + K + (1 : ℕ)) ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ), hk3 : √((↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S2, X p n) ^ (2 : ℕ)) ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ), hk4 : ∑ j ∈ Finset.range (M + (1 : ℕ)), ((2 : ℝ) * Real.pi) ^ j / (↑j.factorial : ℝ) * |(↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ j - (↑(Fintype.piFinset fun (x : ↥S1) => Ns).card : ℝ)⁻¹ * ∑ ω ∈ Fintype.piFinset fun (x : ↥S1) => Ns, (∑ p : ↥S1, X (↑p : ℕ) (ω p)) ^ j| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ), hk5 : (∏ p ∈ S1, (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp ((2 : ℝ) * Real.pi * Real.exp (1 : ℝ) * X p n) + ∏ p ∈ S1, (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp (-((2 : ℝ) * Real.pi * Real.exp (1 : ℝ)) * X p n)) / Real.exp (1 : ℝ) ^ M ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ), hvar : c0 ≤ ∑ p ∈ S1, ((↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, X p n ^ (2 : ℕ) - ((↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, X p n) ^ (2 : ℕ)).

    Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential , div_pos.

    Lean, lines 1050–1052
      obtain ⟨K, p0, v, P, H, B, Ns, m, S1, S2, sgn, r, X, M, hsgn, hr, hX, hMeven, hP, hpr, hNs, hm, hrange,
        hS1, hS2, hdisj, hS0, hXs, hk1, hk2, hk3, hk4, hk5, hvar⟩ :=
        hall (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) (div_pos hδ (by positivity))

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1050–1052 at 60a537454ac8 ↗

  11. hq1 have (1 : ℝ) ≤ (↑r0.den : ℝ)

    Leaves False, with hq1 : (1 : ℝ) ≤ (↑r0.den : ℝ).

    Lean, line 1053
      have hq1 : (1 : ℝ) ≤ r0.den := by exact_mod_cast hq

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1053 at 60a537454ac8 ↗

  12. hd1 have min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ) ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))

    Leaves False, with hd1 : min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ) ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)).

    Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential , div_le_self.

    Lean, lines 1054–1055
      have hd1 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den
          ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := div_le_self hδ.le hq1

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1054–1055 at 60a537454ac8 ↗

  13. hd2 have (↑r0.den : ℝ) * (min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ)) = min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))

    Leaves False, with hd2 : (↑r0.den : ℝ) * (min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ)) = min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)).

    Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential , mul_div_cancel₀.

    Lean, lines 1056–1058
      have hd2 : (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den)
          = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) :=
        mul_div_cancel₀ _ (by positivity)

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1056–1058 at 60a537454ac8 ↗

  14. hsgn' have cubeSgn = sgn

    Leaves False, with hsgn' : cubeSgn = sgn.

    Uses library funext Function extensionality. If two functions return equal results for all possible arguments, then they are equal. .

    Lean, line 1059
      have hsgn' : cubeSgn = sgn := funext fun ε => by rw [hsgn ε]; rfl

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1059 at 60a537454ac8 ↗

  15. hr' have cubeShift p0 v = r

    Leaves False, with hr' : cubeShift p0 v = r.

    Uses library funext Function extensionality. If two functions return equal results for all possible arguments, then they are equal. .

    Lean, line 1060
      have hr' : cubeShift p0 v = r := funext fun ε => funext fun h => by rw [hr ε h]; rfl

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1060 at 60a537454ac8 ↗

  16. s16 obtain Ns.Nonempty

    Leaves False, with n0 : ℤ, hn0 : n0 ∈ Ns.

    Lean, line 1061
      obtain ⟨n0, hn0⟩ := hNs

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1061 at 60a537454ac8 ↗

  17. key have (1 : ℝ) - (8 : ℝ) * ((4 : ℝ) * min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))) ≤ Real.exp ((-8 : ℝ) * c0)

    Leaves False, with key : (1 : ℝ) - (8 : ℝ) * ((4 : ℝ) * min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))) ≤ Real.exp ((-8 : ℝ) * c0).

    Uses library Exists.intro Existential introduction. , Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , ArithmeticFunction.cardDistinctFactors ω n is the number of distinct prime factors of n. , Finset.range range n is the set of natural numbers less than n. , 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. , Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential .

    Lean, lines 1062–1072
      have key := assembly_core Ns ⟨n0, hn0⟩ S1 X
        (fun n => (r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
                (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)
          + ((r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
              - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1))
          + ∑ p ∈ S2, X p n)
        (∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n0)
        (4 * min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64)) c0 ?_
        (fun p hp n hn => ((hXs p hp n hn).trans hd1).trans hδ1) ?_ ((decor_bound Ns S1 X M hMeven htay).trans (by linarith)) hvar

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1062–1072 at 60a537454ac8 ↗

  18. s18 case routine: linarith

    Leaves False, with key : (1 : ℝ) - (8 : ℝ) * ((4 : ℝ) * min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))) ≤ Real.exp ((-8 : ℝ) * c0).

    Lean, line 1073
      · linarith

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1073 at 60a537454ac8 ↗

  19. s19 case

    Leaves ∀ i ∈ Ns, ∃ (z : ℤ), ∑ p ∈ S1, X p i + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0 + ((↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m i ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) + ((↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) + ∑ p ∈ S2, X p i) = (↑z : ℝ).

    Uses library Exists.intro Existential introduction. .

    Lean, lines 1074–1102
      · intro n hn
        obtain ⟨z, hz⟩ := rational_imp_5_8 r0.num r0.den hq hsum p0 v P hP hpr n (m n) (hm n hn)
        rw [hsgn', hr'] at hz
        refine ⟨z, ?_⟩
        have hu : ∀ k < H, ∀ ε : Fin K → Bool,
            0 < (n + r ε (k + K + 1)).toNat ∧ (n + r ε (k + K + 1)).toNat ≤ B := by
          intro k hk ε
          have h := hrange n hn ε k hk
          constructor <;> omega
        have hsplit := phase_split (r0.den : ℝ) H B sgn (fun k => (2 : ℝ) ^ (k + K + 1))
          (fun k ε => (n + r ε (k + K + 1)).toNat) hu
        beta_reduce at hsplit
        have hPB : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, X p n
            = ∑ p ∈ S1, X p n + ∑ p ∈ S2, X p n
              + ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n := by
          rw [← Finset.sum_union hdisj]
          exact (Finset.sum_sdiff (Finset.union_subset hS1 hS2)).symm.trans (add_comm _ _)
        have hC : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n
            = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n0 := by
          refine Finset.sum_congr rfl fun p hp => ?_
          have hp' := Finset.mem_sdiff.mp hp
          exact hS0 p hp'.1 (fun h => hp'.2 (Finset.mem_union_left _ h))
            (fun h => hp'.2 (Finset.mem_union_right _ h)) n hn n0 hn0
        have hXsum : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime,
            (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                (if p ∣ (n + r ε (k + K + 1)).toNat then (1 : ℝ) else 0)) / 2 ^ (k + K + 1)
            = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, X p n :=
          Finset.sum_congr rfl fun p _ => (hX p n).symm
        linarith

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1074–1102 at 60a537454ac8 ↗

    1. s19.s1 obtain ∃ (z : ℤ), (↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, cubeSgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + cubeShift p0 v ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) + (↑r0.den : ℝ) * ∑ ε : Fin K → Bool, cubeSgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) = (↑z : ℝ)

      Leaves ∃ (z : ℤ), ∑ p ∈ S1, X p n + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0 + ((↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) + ((↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) + ∑ p ∈ S2, X p n) = (↑z : ℝ), with z : ℤ, hz : (↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, cubeSgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + cubeShift p0 v ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) + (↑r0.den : ℝ) * ∑ ε : Fin K → Bool, cubeSgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) = (↑z : ℝ).

      Lean, line 1075
          obtain ⟨z, hz⟩ := rational_imp_5_8 r0.num r0.den hq hsum p0 v P hP hpr n (m n) (hm n hn)

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1075 at 60a537454ac8 ↗

    2. s19.hu have ∀ k < H, ∀ (ε : Fin K → Bool), (0 : ℕ) < (n + r ε (k + K + (1 : ℕ))).toNat ∧ (n + r ε (k + K + (1 : ℕ))).toNat ≤ B

      Leaves ∑ p ∈ S1, X p n + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0 + ((↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) + ((↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) + ∑ p ∈ S2, X p n) = (↑z : ℝ), with hu : ∀ k < H, ∀ (ε : Fin K → Bool), (0 : ℕ) < (n + r ε (k + K + (1 : ℕ))).toNat ∧ (n + r ε (k + K + (1 : ℕ))).toNat ≤ B.

      Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. .

      Lean, lines 1078–1082
          have hu : ∀ k < H, ∀ ε : Fin K → Bool,
              0 < (n + r ε (k + K + 1)).toNat ∧ (n + r ε (k + K + 1)).toNat ≤ B := by
            intro k hk ε
            have h := hrange n hn ε k hk
            constructor <;> omega

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1078–1082 at 60a537454ac8 ↗

      1. s19.hu.h have (0 : ℤ) < n + r ε (k + K + (1 : ℕ)) ∧ n + r ε (k + K + (1 : ℕ)) ≤ (↑B : ℤ)

        Leaves (0 : ℕ) < (n + r ε (k + K + (1 : ℕ))).toNat ∧ (n + r ε (k + K + (1 : ℕ))).toNat ≤ B, with h : (0 : ℤ) < n + r ε (k + K + (1 : ℕ)) ∧ n + r ε (k + K + (1 : ℕ)) ≤ (↑B : ℤ).

        Lean, line 1081
              have h := hrange n hn ε k hk

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1081 at 60a537454ac8 ↗

    3. s19.hsplit have (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ b : Fin K → Bool, sgn b * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r b (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) = ∑ p ∈ Finset.range (B + (1 : ℕ)) with Nat.Prime p, (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ b : Fin K → Bool, sgn b * if p ∣ (n + r b (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))

      Leaves ∑ p ∈ S1, X p n + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0 + ((↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) + ((↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) + ∑ p ∈ S2, X p n) = (↑z : ℝ), with hsplit : (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ b : Fin K → Bool, sgn b * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r b (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) = ∑ p ∈ Finset.range (B + (1 : ℕ)) with Nat.Prime p, (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ b : Fin K → Bool, sgn b * if p ∣ (n + r b (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)).

      Lean, lines 1083–1084
          have hsplit := phase_split (r0.den : ℝ) H B sgn (fun k => (2 : ℝ) ^ (k + K + 1))
            (fun k ε => (n + r ε (k + K + 1)).toNat) hu

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1083–1084 at 60a537454ac8 ↗

    4. s19.hPB have ∑ p ∈ Finset.range (B + (1 : ℕ)) with Nat.Prime p, X p n = ∑ p ∈ S1, X p n + ∑ p ∈ S2, X p n + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n

      Leaves ∑ p ∈ S1, X p n + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0 + ((↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) + ((↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) + ∑ p ∈ S2, X p n) = (↑z : ℝ), with hPB : ∑ p ∈ Finset.range (B + (1 : ℕ)) with Nat.Prime p, X p n = ∑ p ∈ S1, X p n + ∑ p ∈ S2, X p n + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n.

      Uses library Finset.range range n is the set of natural numbers less than n. , 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.sum_union, Finset.sum_sdiff, Finset.union_subset, add_comm.

      Lean, lines 1086–1090
          have hPB : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, X p n
              = ∑ p ∈ S1, X p n + ∑ p ∈ S2, X p n
                + ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n := by
            rw [← Finset.sum_union hdisj]
            exact (Finset.sum_sdiff (Finset.union_subset hS1 hS2)).symm.trans (add_comm _ _)

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1086–1090 at 60a537454ac8 ↗

    5. s19.hC have ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n = ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0

      Leaves ∑ p ∈ S1, X p n + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0 + ((↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) + ((↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) + ∑ p ∈ S2, X p n) = (↑z : ℝ), with hC : ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n = ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0.

      Uses library Finset.range range n is the set of natural numbers less than n. , 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.sum_congr, rfl rfl : a = a is the unique constructor of the equality type. , Finset.mem_union_left, Finset.mem_union_right.

      Lean, lines 1091–1096
          have hC : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n
              = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n0 := by
            refine Finset.sum_congr rfl fun p hp => ?_
            have hp' := Finset.mem_sdiff.mp hp
            exact hS0 p hp'.1 (fun h => hp'.2 (Finset.mem_union_left _ h))
              (fun h => hp'.2 (Finset.mem_union_right _ h)) n hn n0 hn0

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1091–1096 at 60a537454ac8 ↗

      1. s19.hC.hp' have p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ p ∉ S1 ∪ S2

        Leaves X p n = X p n0, with hp' : p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) ∧ p ∉ S1 ∪ S2.

        Uses library Finset.mem_sdiff.

        Lean, line 1094
              have hp' := Finset.mem_sdiff.mp hp

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1094 at 60a537454ac8 ↗

    6. s19.hXsum have ∑ p ∈ Finset.range (B + (1 : ℕ)) with Nat.Prime p, (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * if p ∣ (n + r ε (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) = ∑ p ∈ Finset.range (B + (1 : ℕ)) with Nat.Prime p, X p n

      Leaves ∑ p ∈ S1, X p n + ∑ p ∈ Finset.filter Nat.Prime (Finset.range (B + (1 : ℕ))) \ (S1 ∪ S2), X p n0 + ((↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) + ((↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) + ∑ p ∈ S2, X p n) = (↑z : ℝ), with hXsum : ∑ p ∈ Finset.range (B + (1 : ℕ)) with Nat.Prime p, (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * if p ∣ (n + r ε (k + K + (1 : ℕ))).toNat then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) = ∑ p ∈ Finset.range (B + (1 : ℕ)) with Nat.Prime p, X p n.

      Uses library Finset.range range n is the set of natural numbers less than n. , 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. , Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , Finset.sum_congr, rfl rfl : a = a is the unique constructor of the equality type. .

      Lean, lines 1097–1101
          have hXsum : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime,
              (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                  (if p ∣ (n + r ε (k + K + 1)).toNat then (1 : ℝ) else 0)) / 2 ^ (k + K + 1)
              = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, X p n :=
            Finset.sum_congr rfl fun p _ => (hX p n).symm

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1097–1101 at 60a537454ac8 ↗

  20. s20 case

    Leaves (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m i ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ)) + ((↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))) + ∑ p ∈ S2, X p i| ≤ (4 : ℝ) * min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)).

    Lean, lines 1103–1168
      · refine (avg_abs_add3 Ns _ _ _).trans ?_
        have e1 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
                (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
            ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := by
          have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε
          calc (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
                (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
              ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑ ε : Fin K → Bool, ∑' k : ℕ,
                (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) :=
                mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun n _ =>
                  abs_sgn_tsum_le _ (Nat.cast_nonneg _) sgn hs1 _ fun ε k =>
                    div_nonneg (by split_ifs <;> norm_num) (by positivity))
                  (inv_nonneg.mpr (Nat.cast_nonneg _))
            _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' k : ℕ,
                (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)) := by
                rw [← Finset.mul_sum]; ring
            _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
                mul_le_mul_of_nonneg_left hk1 (Nat.cast_nonneg _)
            _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2
        have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε
        have e2 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
              - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
            ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := by
          have hper : ∀ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
              - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
              ≤ (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                / 2 ^ (k + H + K + 1) := by
            intro n hn
            have hsh : ∀ ε (h : ℕ), (n + r ε h).toNat = P ε * m n ε + P ε * h := by
              intro ε h
              have : n + r ε h = ((P ε * m n ε + P ε * h : ℕ) : ℤ) := by
                rw [← hr', cubeShift_eq, ← hP ε]; push_cast; linear_combination hm n hn ε
              rw [this, Int.toNat_natCast]
            have hG : Summable fun k : ℕ => (∑ ε : Fin K → Bool,
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ))
                / 2 ^ (k + K + 1) := by
              simp_rw [Finset.sum_div]
              refine summable_sum fun ε _ => ?_
              have hb := omega_bound_summable (fun k => P ε * m n ε + P ε * (k + K + 1))
                (P ε * m n ε + P ε * (K + 1)) (P ε) (fun k => le_of_eq (by ring))
              refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) hb
              rw [hsh ε]
              exact div_le_div_of_nonneg_left (by positivity) (by positivity)
                (pow_le_pow_right₀ (by norm_num) (by omega))
            rw [← mul_sub, abs_mul, abs_of_nonneg (Nat.cast_nonneg _)]
            exact mul_le_mul_of_nonneg_left (tail_abs_le _ _ (fun k => abs_sgn_sum_div_le sgn hs1 _
              (fun ε => Nat.cast_nonneg _) _ (by positivity)) hG H) (Nat.cast_nonneg _)
          calc _ ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                / 2 ^ (k + H + K + 1) :=
                mul_le_mul_of_nonneg_left (Finset.sum_le_sum hper) (inv_nonneg.mpr (Nat.cast_nonneg _))
            _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' k : ℕ, (∑ ε : Fin K → Bool,
                (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                / 2 ^ (k + H + K + 1)) := by rw [← Finset.mul_sum]; ring
            _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
                mul_le_mul_of_nonneg_left hk2 (Nat.cast_nonneg _)
            _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2
        have e3 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |∑ p ∈ S2, X p n|
            ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) :=
          ((avg_abs_le_sqrt Ns fun n => ∑ p ∈ S2, X p n).trans hk3).trans hd1
        linarith

    targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1103–1168 at 60a537454ac8 ↗

    1. s20.e1 have (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))

      Leaves (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m i ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))| + (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| + (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |∑ p ∈ S2, X p i| ≤ (4 : ℝ) * min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)), with e1 : (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)).

      Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential .

      Lean, lines 1104–1121
          have e1 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
                  (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
              ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := by
            have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε
            calc (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
                  (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
                ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑ ε : Fin K → Bool, ∑' k : ℕ,
                  (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) :=
                  mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun n _ =>
                    abs_sgn_tsum_le _ (Nat.cast_nonneg _) sgn hs1 _ fun ε k =>
                      div_nonneg (by split_ifs <;> norm_num) (by positivity))
                    (inv_nonneg.mpr (Nat.cast_nonneg _))
              _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' k : ℕ,
                  (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)) := by
                  rw [← Finset.mul_sum]; ring
              _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
                  mul_le_mul_of_nonneg_left hk1 (Nat.cast_nonneg _)
              _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1104–1121 at 60a537454ac8 ↗

      1. s20.e1.hs1 have ∀ (ε : Fin K → Bool), |sgn ε| ≤ (1 : ℝ)

        Leaves (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)), with hs1 : ∀ (ε : Fin K → Bool), |sgn ε| ≤ (1 : ℝ).

        Lean, line 1107
              have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1107 at 60a537454ac8 ↗

      2. s20.e1.s2 calc (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))| ≤ (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (↑r0.den : ℝ) * ∑ ε : Fin K → Bool, ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))

        Closes the goal.

        Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , mul_le_mul_of_nonneg_left, Finset.sum_le_sum In an ordered additive commutative monoid, if each summand f i of one finite sum is less than or equal to the corresponding summand g i of another finite sum, then ∑ i ∈ s, f i ≤ ∑ i ∈ s, g i. , Nat.cast_nonneg Specialisation of Nat.cast_nonneg', which seems to be easier for Lean to use. , div_nonneg, inv_nonneg.

        Lean, lines 1108–1115
              calc (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
                    (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
                  ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑ ε : Fin K → Bool, ∑' k : ℕ,
                    (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) :=
                    mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun n _ =>
                      abs_sgn_tsum_le _ (Nat.cast_nonneg _) sgn hs1 _ fun ε k =>
                        div_nonneg (by split_ifs <;> norm_num) (by positivity))
                      (inv_nonneg.mpr (Nat.cast_nonneg _))

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1108–1115 at 60a537454ac8 ↗

      3. s20.e1.s3 calc

        Closes the goal.

        Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , Finset.mul_sum.

        Lean, lines 1116–1118
                _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' k : ℕ,
                    (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)) := by
                    rw [← Finset.mul_sum]; ring

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1116–1118 at 60a537454ac8 ↗

      4. s20.e1.s4 calc (↑r0.den : ℝ) * ((↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m n ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))) ≤ (↑r0.den : ℝ) * (min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ))

        Closes the goal.

        Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential , mul_le_mul_of_nonneg_left, Nat.cast_nonneg Specialisation of Nat.cast_nonneg', which seems to be easier for Lean to use. .

        Lean, lines 1119–1120
                _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
                    mul_le_mul_of_nonneg_left hk1 (Nat.cast_nonneg _)

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1119–1120 at 60a537454ac8 ↗

      5. s20.e1.s5 calc (↑r0.den : ℝ) * (min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ)) = min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))

        Closes the goal.

        Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential .

        Lean, line 1121
                _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1121 at 60a537454ac8 ↗

    2. s20.hs1 have ∀ (ε : Fin K → Bool), |sgn ε| ≤ (1 : ℝ)

      Leaves (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m i ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))| + (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| + (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |∑ p ∈ S2, X p i| ≤ (4 : ℝ) * min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)), with hs1 : ∀ (ε : Fin K → Bool), |sgn ε| ≤ (1 : ℝ).

      Lean, line 1122
          have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1122 at 60a537454ac8 ↗

    3. s20.e2 have (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))

      Leaves (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m i ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))| + (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| + (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |∑ p ∈ S2, X p i| ≤ (4 : ℝ) * min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)), with e2 : (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)).

      Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , ArithmeticFunction.cardDistinctFactors ω n is the number of distinct prime factors of n. , Finset.range range n is the set of natural numbers less than n. , Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential .

      Lean, lines 1123–1164
          have e2 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
                  (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
                - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                  (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
              ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := by
            have hper : ∀ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
                  (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
                - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                  (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
                ≤ (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
                  (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                  / 2 ^ (k + H + K + 1) := by
              intro n hn
              have hsh : ∀ ε (h : ℕ), (n + r ε h).toNat = P ε * m n ε + P ε * h := by
                intro ε h
                have : n + r ε h = ((P ε * m n ε + P ε * h : ℕ) : ℤ) := by
                  rw [← hr', cubeShift_eq, ← hP ε]; push_cast; linear_combination hm n hn ε
                rw [this, Int.toNat_natCast]
              have hG : Summable fun k : ℕ => (∑ ε : Fin K → Bool,
                  (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ))
                  / 2 ^ (k + K + 1) := by
                simp_rw [Finset.sum_div]
                refine summable_sum fun ε _ => ?_
                have hb := omega_bound_summable (fun k => P ε * m n ε + P ε * (k + K + 1))
                  (P ε * m n ε + P ε * (K + 1)) (P ε) (fun k => le_of_eq (by ring))
                refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) hb
                rw [hsh ε]
                exact div_le_div_of_nonneg_left (by positivity) (by positivity)
                  (pow_le_pow_right₀ (by norm_num) (by omega))
              rw [← mul_sub, abs_mul, abs_of_nonneg (Nat.cast_nonneg _)]
              exact mul_le_mul_of_nonneg_left (tail_abs_le _ _ (fun k => abs_sgn_sum_div_le sgn hs1 _
                (fun ε => Nat.cast_nonneg _) _ (by positivity)) hG H) (Nat.cast_nonneg _)
            calc _ ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
                  (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                  / 2 ^ (k + H + K + 1) :=
                  mul_le_mul_of_nonneg_left (Finset.sum_le_sum hper) (inv_nonneg.mpr (Nat.cast_nonneg _))
              _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' k : ℕ, (∑ ε : Fin K → Bool,
                  (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                  / 2 ^ (k + H + K + 1)) := by rw [← Finset.mul_sum]; ring
              _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
                  mul_le_mul_of_nonneg_left hk2 (Nat.cast_nonneg _)
              _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1123–1164 at 60a537454ac8 ↗

      1. s20.e2.hper have ∀ n ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| ≤ (↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + H + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + H + K + (1 : ℕ))

        Leaves (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)), with hper : ∀ n ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| ≤ (↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + H + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + H + K + (1 : ℕ)).

        Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , ArithmeticFunction.cardDistinctFactors ω n is the number of distinct prime factors of n. , Finset.range range n is the set of natural numbers less than n. , mul_sub Alias of mul_sub_left_distrib. , abs_mul, abs_of_nonneg, Nat.cast_nonneg Specialisation of Nat.cast_nonneg', which seems to be easier for Lean to use. , mul_le_mul_of_nonneg_left.

        Lean, lines 1128–1154
              have hper : ∀ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
                    (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
                  - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
                    (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
                  ≤ (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
                    (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                    / 2 ^ (k + H + K + 1) := by
                intro n hn
                have hsh : ∀ ε (h : ℕ), (n + r ε h).toNat = P ε * m n ε + P ε * h := by
                  intro ε h
                  have : n + r ε h = ((P ε * m n ε + P ε * h : ℕ) : ℤ) := by
                    rw [← hr', cubeShift_eq, ← hP ε]; push_cast; linear_combination hm n hn ε
                  rw [this, Int.toNat_natCast]
                have hG : Summable fun k : ℕ => (∑ ε : Fin K → Bool,
                    (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ))
                    / 2 ^ (k + K + 1) := by
                  simp_rw [Finset.sum_div]
                  refine summable_sum fun ε _ => ?_
                  have hb := omega_bound_summable (fun k => P ε * m n ε + P ε * (k + K + 1))
                    (P ε * m n ε + P ε * (K + 1)) (P ε) (fun k => le_of_eq (by ring))
                  refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) hb
                  rw [hsh ε]
                  exact div_le_div_of_nonneg_left (by positivity) (by positivity)
                    (pow_le_pow_right₀ (by norm_num) (by omega))
                rw [← mul_sub, abs_mul, abs_of_nonneg (Nat.cast_nonneg _)]
                exact mul_le_mul_of_nonneg_left (tail_abs_le _ _ (fun k => abs_sgn_sum_div_le sgn hs1 _
                  (fun ε => Nat.cast_nonneg _) _ (by positivity)) hG H) (Nat.cast_nonneg _)

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1128–1154 at 60a537454ac8 ↗

        1. s20.e2.hper.hsh have ∀ (ε : Fin K → Bool) (h : ℕ), (n + r ε h).toNat = P ε * m n ε + P ε * h

          Leaves |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| ≤ (↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + H + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + H + K + (1 : ℕ)), with hsh : ∀ (ε : Fin K → Bool) (h : ℕ), (n + r ε h).toNat = P ε * m n ε + P ε * h.

          Uses library Int.toNat_natCast.

          Lean, lines 1136–1140
                  have hsh : ∀ ε (h : ℕ), (n + r ε h).toNat = P ε * m n ε + P ε * h := by
                    intro ε h
                    have : n + r ε h = ((P ε * m n ε + P ε * h : ℕ) : ℤ) := by
                      rw [← hr', cubeShift_eq, ← hP ε]; push_cast; linear_combination hm n hn ε
                    rw [this, Int.toNat_natCast]

          targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1136–1140 at 60a537454ac8 ↗

          1. s20.e2.hper.hsh.this have n + r ε h = (↑(P ε * m n ε + P ε * h) : ℤ)

            Leaves (n + r ε h).toNat = P ε * m n ε + P ε * h, with this : n + r ε h = (↑(P ε * m n ε + P ε * h) : ℤ).

            Lean, lines 1138–1139
                      have : n + r ε h = ((P ε * m n ε + P ε * h : ℕ) : ℤ) := by
                        rw [← hr', cubeShift_eq, ← hP ε]; push_cast; linear_combination hm n hn ε

            targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1138–1139 at 60a537454ac8 ↗

        2. s20.e2.hper.hG have Summable fun (k : ℕ) => (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))

          Leaves |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| ≤ (↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + H + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + H + K + (1 : ℕ)), with hG : Summable fun (k : ℕ) => (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)).

          Uses library Summable Summable f means that f has some (infinite) sum with respect to L. , Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , ArithmeticFunction.cardDistinctFactors ω n is the number of distinct prime factors of n. , Finset.sum_div, summable_sum, Summable.of_nonneg_of_le Comparison test of convergence of series of non-negative real numbers. , div_le_div_of_nonneg_left, pow_le_pow_right₀.

          Lean, lines 1141–1151
                  have hG : Summable fun k : ℕ => (∑ ε : Fin K → Bool,
                      (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ))
                      / 2 ^ (k + K + 1) := by
                    simp_rw [Finset.sum_div]
                    refine summable_sum fun ε _ => ?_
                    have hb := omega_bound_summable (fun k => P ε * m n ε + P ε * (k + K + 1))
                      (P ε * m n ε + P ε * (K + 1)) (P ε) (fun k => le_of_eq (by ring))
                    refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) hb
                    rw [hsh ε]
                    exact div_le_div_of_nonneg_left (by positivity) (by positivity)
                      (pow_le_pow_right₀ (by norm_num) (by omega))

          targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1141–1151 at 60a537454ac8 ↗

          1. s20.e2.hper.hG.hb have Summable fun (k : ℕ) => (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (P ε * m n ε + P ε * (k + K + (1 : ℕ)))) : ℝ) / (2 : ℝ) ^ (k + (1 : ℕ))

            Leaves Summable fun (k : ℕ) => (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + K + (1 : ℕ))).toNat) : ℝ) / (2 : ℝ) ^ (k + K + (1 : ℕ)), with hb : Summable fun (k : ℕ) => (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (P ε * m n ε + P ε * (k + K + (1 : ℕ)))) : ℝ) / (2 : ℝ) ^ (k + (1 : ℕ)).

            Uses library le_of_eq.

            Lean, lines 1146–1147
                      have hb := omega_bound_summable (fun k => P ε * m n ε + P ε * (k + K + 1))
                        (P ε * m n ε + P ε * (K + 1)) (P ε) (fun k => le_of_eq (by ring))

            targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1146–1147 at 60a537454ac8 ↗

      2. s20.e2.s2 calc (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| ≤ (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + H + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + H + K + (1 : ℕ))

        Closes the goal.

        Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , ArithmeticFunction.cardDistinctFactors ω n is the number of distinct prime factors of n. , mul_le_mul_of_nonneg_left, Finset.sum_le_sum In an ordered additive commutative monoid, if each summand f i of one finite sum is less than or equal to the corresponding summand g i of another finite sum, then ∑ i ∈ s, f i ≤ ∑ i ∈ s, g i. , inv_nonneg, Nat.cast_nonneg Specialisation of Nat.cast_nonneg', which seems to be easier for Lean to use. .

        Lean, lines 1155–1158
              calc _ ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
                    (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                    / 2 ^ (k + H + K + 1) :=
                    mul_le_mul_of_nonneg_left (Finset.sum_le_sum hper) (inv_nonneg.mpr (Nat.cast_nonneg _))

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1155–1158 at 60a537454ac8 ↗

      3. s20.e2.s3 calc

        Closes the goal.

        Uses library Fin Natural numbers less than some upper bound. , Bool The Boolean values, true and false. , ArithmeticFunction.cardDistinctFactors ω n is the number of distinct prime factors of n. , Finset.mul_sum.

        Lean, lines 1159–1161
                _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' k : ℕ, (∑ ε : Fin K → Bool,
                    (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
                    / 2 ^ (k + H + K + 1)) := by rw [← Finset.mul_sum]; ring

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1159–1161 at 60a537454ac8 ↗

      4. s20.e2.s4 calc (↑r0.den : ℝ) * ((↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' (k : ℕ), (∑ ε : Fin K → Bool, (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (n + r ε (k + H + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + H + K + (1 : ℕ))) ≤ (↑r0.den : ℝ) * (min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ))

        Closes the goal.

        Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential , mul_le_mul_of_nonneg_left, Nat.cast_nonneg Specialisation of Nat.cast_nonneg', which seems to be easier for Lean to use. .

        Lean, lines 1162–1163
                _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
                    mul_le_mul_of_nonneg_left hk2 (Nat.cast_nonneg _)

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1162–1163 at 60a537454ac8 ↗

      5. s20.e2.s5 calc (↑r0.den : ℝ) * (min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)) / (↑r0.den : ℝ)) = min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))

        Closes the goal.

        Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential .

        Lean, line 1164
                _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2

        targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, line 1164 at 60a537454ac8 ↗

    4. s20.e3 have (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |∑ p ∈ S2, X p n| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ))

      Leaves (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' (k : ℕ), (if P ε ^ (2 : ℕ) ∣ P ε * m i ε + P ε * (k + (1 : ℕ)) then (1 : ℝ) else (0 : ℝ)) / (2 : ℝ) ^ (k + (1 : ℕ))| + (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |(↑r0.den : ℝ) * ∑' (k : ℕ), (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ)) - (↑r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε * (↑((ArithmeticFunction.cardDistinctFactors : ℕ → ℕ) (i + r ε (k + K + (1 : ℕ))).toNat) : ℝ)) / (2 : ℝ) ^ (k + K + (1 : ℕ))| + (↑Ns.card : ℝ)⁻¹ * ∑ i ∈ Ns, |∑ p ∈ S2, X p i| ≤ (4 : ℝ) * min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)), with e3 : (↑Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |∑ p ∈ S2, X p n| ≤ min (1 / 4 : ℝ) (((1 : ℝ) - Real.exp ((-8 : ℝ) * c0)) / (64 : ℝ)).

      Uses library Min.min Returns the lesser of its two arguments. , Real.exp The real exponential function, defined as the real part of the complex exponential .

      Lean, lines 1165–1167
          have e3 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |∑ p ∈ S2, X p n|
              ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) :=
            ((avg_abs_le_sqrt Ns fun n => ∑ p ∈ S2, X p n).trans hk3).trans hd1

      targets/erdos-257/nodes/variant-6b364d2b/attempts/20261022T160309Z-t1022-l-partial.lean, lines 1165–1167 at 60a537454ac8 ↗

import Mathlib
import Nodes.«variant-6b364d2b».Context

/-- the summand on pairs `(m, a)`: `(1/2)^m` when `a ∈ A`, `a ∣ m`, `m ≠ 0`. -/
private noncomputable def lamG (A : Set ℕ) [DecidablePred (· ∈ A)] (p : ℕ × ℕ) : ℝ :=
  if p.2 ∈ A ∧ p.2 ∣ p.1 ∧ p.1 ≠ 0 then (1 / 2 : ℝ) ^ p.1 else 0

private lemma lamG_nonneg (A : Set ℕ) [DecidablePred (· ∈ A)] (p : ℕ × ℕ) : 0 ≤ lamG A p := by
  unfold lamG; split_ifs <;> positivity

private lemma lamG_row (A : Set ℕ) [DecidablePred (· ∈ A)] (m : ℕ) :
    ∑' a, lamG A (m, a) = (((Nat.divisors m).filter (· ∈ A)).card : ℝ) * (1 / 2 : ℝ) ^ m := by
  rw [tsum_eq_sum (s := (Nat.divisors m).filter (· ∈ A))]
  · rw [Finset.card_eq_sum_ones, Nat.cast_sum, Finset.sum_mul]
    refine Finset.sum_congr rfl fun a ha => ?_
    simp only [Finset.mem_filter, Nat.mem_divisors] at ha
    simp [lamG, ha.1.1, ha.1.2, ha.2]
  · intro a ha
    simp only [Finset.mem_filter, Nat.mem_divisors, not_and] at ha
    unfold lamG
    rw [if_neg]
    rintro ⟨h1, h2, h3⟩
    exact ha ⟨h2, h3⟩ h1

private lemma lamG_row_summable (A : Set ℕ) [DecidablePred (· ∈ A)] (m : ℕ) : Summable fun a => lamG A (m, a) := by
  apply summable_of_ne_finset_zero (s := (Nat.divisors m).filter (· ∈ A))
  intro a ha
  simp only [Finset.mem_filter, Nat.mem_divisors, not_and] at ha
  unfold lamG
  rw [if_neg]
  rintro ⟨h1, h2, h3⟩
  exact ha ⟨h2, h3⟩ h1

private lemma lamG_summable (A : Set ℕ) [DecidablePred (· ∈ A)] : Summable (lamG A) := by
  rw [summable_prod_of_nonneg (lamG_nonneg A)]
  refine ⟨lamG_row_summable A, ?_⟩
  simp_rw [lamG_row]
  have hg : Summable fun m : ℕ => ((m : ℝ) ^ 1) * (1 / 2 : ℝ) ^ m :=
    summable_pow_mul_geometric_of_norm_lt_one 1 (by norm_num)
  refine Summable.of_nonneg_of_le (fun m => by positivity) (fun m => ?_) hg
  gcongr
  rw [pow_one]
  exact_mod_cast (Finset.card_filter_le _ _).trans (Nat.card_divisors_le_self m)

private lemma lamG_col (A : Set ℕ) [DecidablePred (· ∈ A)] (a : ℕ) :
    ∑' m, lamG A (m, a) = if a ∈ A then (1 : ℝ) / (2 ^ a - 1) else 0 := by
  by_cases ha : a ∈ A
  swap
  · simp [lamG, ha]
  rw [if_pos ha]
  rcases Nat.eq_zero_or_pos a with h0 | hpos
  · subst h0; simp [lamG]
  have hinj : Function.Injective fun k : ℕ => a * (k + 1) := by
    intro x y h; simp only at h
    have := Nat.eq_of_mul_eq_mul_left hpos h; omega
  have hsupp : Function.support (fun m => lamG A (m, a)) ⊆ Set.range fun k : ℕ => a * (k + 1) := by
    intro m hm
    simp only [Function.mem_support, lamG, ne_eq, ite_eq_right_iff, Classical.not_imp] at hm
    obtain ⟨⟨_, ⟨c, hc⟩, hm0⟩, _⟩ := hm
    refine ⟨c - 1, ?_⟩
    have : c ≠ 0 := by rintro rfl; simp at hc; exact hm0 hc
    simp only; rw [hc]; congr 1; omega
  rw [← hinj.tsum_eq hsupp]
  have hterm : ∀ k : ℕ, lamG A (a * (k + 1), a) = (1 / 2 : ℝ) ^ a * ((1 / 2 : ℝ) ^ a) ^ k := by
    intro k
    have : a * (k + 1) ≠ 0 := by positivity
    simp only [lamG, ha, dvd_mul_right, ne_eq, this, not_false_eq_true, and_self, if_true]
    rw [← pow_mul, ← pow_add]; congr 1; ring
  simp_rw [hterm]
  rw [tsum_mul_left, tsum_geometric_of_lt_one (by positivity)
    (pow_lt_one₀ (by norm_num) (by norm_num) hpos.ne')]
  have h2 : (1 : ℝ) < 2 ^ a := one_lt_pow₀ (by norm_num) hpos.ne'
  have h2' : (2 : ℝ) ^ a - 1 ≠ 0 := by linarith
  rw [one_div_pow]
  field_simp

/-- **Lambert identity.** `∑_{a ∈ A} 1/(2^a - 1) = ∑_m #{d ∣ m : d ∈ A} / 2^m`. (In Lean
`1/(2^0 - 1) = 0` and `Nat.divisors 0 = ∅`, so `0 ∈ A` changes neither side.) -/
private theorem lambert_identity (A : Set ℕ) [DecidablePred (· ∈ A)] :
    ∑' a : A, (1 : ℝ) / (2 ^ a.1 - 1) =
      ∑' m : ℕ, ((((Nat.divisors m).filter (· ∈ A)).card : ℕ) : ℝ) / 2 ^ m := by
  have hs := lamG_summable A
  have hrows : ∑' m : ℕ, ((((Nat.divisors m).filter (· ∈ A)).card : ℕ) : ℝ) / 2 ^ m
      = ∑' m, ∑' a, lamG A (m, a) := by
    refine tsum_congr fun m => ?_
    rw [lamG_row, one_div, inv_pow, div_eq_mul_inv]
  have hcols : ∑' a : A, (1 : ℝ) / (2 ^ a.1 - 1) = ∑' a, ∑' m, lamG A (m, a) := by
    rw [tsum_subtype A (fun a => (1 : ℝ) / (2 ^ a - 1))]
    refine tsum_congr fun a => ?_
    rw [lamG_col]; simp [Set.indicator]
  rw [hrows, hcols]
  have hu : Summable (Function.uncurry fun m a => lamG A (m, a)) := hs
  exact hu.tsum_comm

private lemma omega_eq_card_filter (n : ℕ) :
    ArithmeticFunction.cardDistinctFactors n = ((Nat.divisors n).filter (· ∈ {p : ℕ | p.Prime})).card := by
  rw [ArithmeticFunction.cardDistinctFactors_apply, ← List.card_toFinset]
  congr 1
  ext p
  simp only [Finset.mem_filter, Nat.mem_divisors, Set.mem_setOf_eq, List.mem_toFinset,
    Nat.mem_primeFactorsList']
  tauto

/-- Equation (1.7) of Tao–Teräväinen: `∑_p 1/(2^p - 1) = ∑_n ω(n)/2^n`. -/
private theorem lambert_primes :
    ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) =
      ∑' n : ℕ, (ArithmeticFunction.cardDistinctFactors n : ℝ) / 2 ^ n := by
  rw [lambert_identity]
  refine tsum_congr fun n => ?_
  rw [omega_eq_card_filter]

private lemma omega_div_summable :
    Summable fun n : ℕ => (ArithmeticFunction.cardDistinctFactors n : ℝ) / 2 ^ n := by
  have hg : Summable fun m : ℕ => ((m : ℝ) ^ 1) * (1 / 2 : ℝ) ^ m :=
    summable_pow_mul_geometric_of_norm_lt_one 1 (by norm_num)
  refine Summable.of_nonneg_of_le (fun m => by positivity) (fun m => ?_) hg
  rw [pow_one, one_div_pow, div_eq_mul_one_div]
  gcongr
  rw [omega_eq_card_filter]
  exact_mod_cast (Finset.card_filter_le _ _).trans (Nat.card_divisors_le_self m)


/-- Elementary step (2) of the primes route (see postmortem #894): if `∑ f(n)/2^n = a/b`, then
for every `N` the binary tail `∑_{k ≥ 0} f(N+1+k)/2^(k+1)` times `b` is an integer. With
`f = ω` this is the reduction Tao–Teräväinen's argument starts from. -/
private theorem tail_mul_den_int (f : ℕ → ℕ) (hs : Summable (fun n => (f n : ℝ) / 2 ^ n))
    (a : ℤ) (b : ℕ) (hb : 0 < b) (h : ∑' n, (f n : ℝ) / 2 ^ n = a / b) (N : ℕ) :
    ∃ z : ℤ, (b : ℝ) * ∑' k, (f (k + (N + 1)) : ℝ) / 2 ^ (k + 1) = z := by
  have hsplit := hs.sum_add_tsum_nat_add (N + 1)
  have hfin : ∀ n ∈ Finset.range (N + 1), (2 : ℝ) ^ N * ((f n : ℝ) / 2 ^ n) = ((f n * 2 ^ (N - n) : ℕ) : ℝ) := by
    intro n hn
    have hn' : n ≤ N := Nat.lt_succ_iff.mp (Finset.mem_range.mp hn)
    push_cast
    have : (2 : ℝ) ^ N = 2 ^ (N - n) * 2 ^ n := by rw [← pow_add, Nat.sub_add_cancel hn']
    rw [this]; field_simp
  have htail : ∀ k, (2 : ℝ) ^ N * ((f (k + (N + 1)) : ℝ) / 2 ^ (k + (N + 1))) = (f (k + (N + 1)) : ℝ) / 2 ^ (k + 1) := by
    intro k
    have : (2 : ℝ) ^ (k + (N + 1)) = 2 ^ (k + 1) * 2 ^ N := by rw [← pow_add]; ring_nf
    rw [this]; field_simp
  have hI : (2 : ℝ) ^ N * ∑ n ∈ Finset.range (N + 1), (f n : ℝ) / 2 ^ n
      = ((∑ n ∈ Finset.range (N + 1), f n * 2 ^ (N - n) : ℕ) : ℝ) := by
    rw [Finset.mul_sum, Nat.cast_sum]; exact Finset.sum_congr rfl hfin
  have hT : (2 : ℝ) ^ N * ∑' k, (f (k + (N + 1)) : ℝ) / 2 ^ (k + (N + 1))
      = ∑' k, (f (k + (N + 1)) : ℝ) / 2 ^ (k + 1) := by
    rw [← tsum_mul_left]; exact tsum_congr htail
  refine ⟨2 ^ N * a - b * ((∑ n ∈ Finset.range (N + 1), f n * 2 ^ (N - n) : ℕ) : ℤ), ?_⟩
  have hb' : (b : ℝ) ≠ 0 := by exact_mod_cast hb.ne'
  rw [← hT]
  have key : (2 : ℝ) ^ N * (a / b) = ((∑ n ∈ Finset.range (N + 1), f n * 2 ^ (N - n) : ℕ) : ℝ)
      + 2 ^ N * ∑' k, (f (k + (N + 1)) : ℝ) / 2 ^ (k + (N + 1)) := by
    rw [← h, ← hsplit, mul_add, hI]
  push_cast at key ⊢
  have : (b : ℝ) * (2 ^ N * (a / b)) = 2 ^ N * a := by field_simp
  linear_combination this - (b : ℝ) * key

/-- Equation (5.1) of Tao–Teräväinen from rationality: if `∑_p 1/(2^p-1) = a/q`, then for every
`N`, `q · ∑_{h ≥ 1} ω(N+h)/2^h` is an integer. -/
private theorem eq_5_1 (a : ℤ) (q : ℕ) (hq : 0 < q)
    (h : ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) = a / q) (N : ℕ) :
    ∃ z : ℤ, (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (k + (N + 1)) : ℝ) / 2 ^ (k + 1) = z :=
  tail_mul_den_int (fun n => ArithmeticFunction.cardDistinctFactors n) omega_div_summable a q hq
    (by rw [← lambert_primes]; exact h) N


private lemma omega_eq_card_pf (n : ℕ) : ArithmeticFunction.cardDistinctFactors n = n.primeFactors.card := by
  rw [ArithmeticFunction.cardDistinctFactors_apply, ← List.card_toFinset]; rfl

/-- additivity used in (5.2): `ω(p k) + 1_{p ∣ k} = ω(k) + 1` for a prime `p` and `k ≠ 0`. -/
private lemma omega_mul_prime {p k : ℕ} (hp : p.Prime) (hk : k ≠ 0) :
    ArithmeticFunction.cardDistinctFactors (p * k) + (if p ∣ k then 1 else 0)
      = ArithmeticFunction.cardDistinctFactors k + 1 := by
  rw [omega_eq_card_pf, omega_eq_card_pf, Nat.primeFactors_mul hp.ne_zero hk, hp.primeFactors]
  by_cases h : p ∣ k
  · have hm : p ∈ k.primeFactors := Nat.mem_primeFactors.mpr ⟨hp, h, hk⟩
    rw [if_pos h, Finset.singleton_union, Finset.insert_eq_of_mem hm]
  · have hm : p ∉ k.primeFactors := fun hm => h (Nat.mem_primeFactors.mp hm).2.1
    rw [if_neg h, Finset.singleton_union, Finset.card_insert_of_notMem hm]

private lemma omega_shift_summable (c : ℕ) :
    Summable fun k : ℕ => (ArithmeticFunction.cardDistinctFactors (k + c) : ℝ) / 2 ^ (k + 1) := by
  have h1 : Summable fun k : ℕ => ((k : ℝ) ^ 1) * (1 / 2 : ℝ) ^ k :=
    summable_pow_mul_geometric_of_norm_lt_one 1 (by norm_num)
  have h2 : Summable fun k : ℕ => (c : ℝ) * (1 / 2 : ℝ) ^ k := summable_geometric_two.mul_left _
  refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) (h1.add h2)
  have hw : (ArithmeticFunction.cardDistinctFactors (k + c) : ℝ) ≤ k + c := by
    rw [omega_eq_card_filter]
    exact_mod_cast (Finset.card_filter_le _ _).trans (Nat.card_divisors_le_self _)
  have hpos : (0 : ℝ) < 2 ^ k := by positivity
  rw [div_le_iff₀ (by positivity), pow_one, one_div_pow]
  have : ((k : ℝ) * (1 / 2 ^ k) + c * (1 / 2 ^ k)) * 2 ^ (k + 1) = 2 * (k + c) := by
    rw [pow_succ]; field_simp
  rw [this]
  have : (0 : ℝ) ≤ k + c := by positivity
  linarith

private lemma ind_summable (f : ℕ → Prop) [DecidablePred f] :
    Summable fun k : ℕ => (if f k then (1 : ℝ) else 0) / 2 ^ (k + 1) := by
  refine Summable.of_nonneg_of_le (fun k => by split_ifs <;> positivity) (fun k => ?_) summable_geometric_two
  rw [one_div_pow]
  have : (0 : ℝ) < 2 ^ k := by positivity
  split_ifs
  · rw [div_le_div_iff_of_pos_left one_pos (by positivity) this]; rw [pow_succ]; linarith
  · simp

/-- Equation (5.2)–(5.3) of Tao–Teräväinen: from the tail congruences (5.1) at `m`, for a prime `p`
and `n = p m`: `q ∑_{h ≥ 1} ω(n + p h)/2^h + q δ_p(n)` is an integer, where
`δ_p(n) = ∑_{h ≥ 1} 1_{p² ∣ n + p h}/2^h`. -/
private theorem eq_5_2 (q : ℕ)
    (hint : ∀ N : ℕ, ∃ z : ℤ,
      (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (k + (N + 1)) : ℝ) / 2 ^ (k + 1) = z)
    {p : ℕ} (hp : p.Prime) (m : ℕ) :
    ∃ z : ℤ, (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (p * m + p * (k + 1)) : ℝ) / 2 ^ (k + 1)
      + (q : ℝ) * ∑' k, (if p ^ 2 ∣ p * m + p * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) = z := by
  obtain ⟨z, hz⟩ := hint m
  refine ⟨z + q, ?_⟩
  have hterm : ∀ k : ℕ,
      (ArithmeticFunction.cardDistinctFactors (p * m + p * (k + 1)) : ℝ) / 2 ^ (k + 1)
      + (if p ^ 2 ∣ p * m + p * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)
      = (ArithmeticFunction.cardDistinctFactors (k + (m + 1)) : ℝ) / 2 ^ (k + 1) + (1 / 2 : ℝ) ^ (k + 1) := by
    intro k
    have he : p * m + p * (k + 1) = p * (k + (m + 1)) := by ring
    have hd : p ^ 2 ∣ p * (k + (m + 1)) ↔ p ∣ k + (m + 1) := by
      rw [pow_two]; exact Nat.mul_dvd_mul_iff_left hp.pos
    have key := omega_mul_prime hp (show k + (m + 1) ≠ 0 by omega)
    rw [he]
    have key' : (ArithmeticFunction.cardDistinctFactors (p * (k + (m + 1))) : ℝ)
        + (if p ^ 2 ∣ p * (k + (m + 1)) then (1 : ℝ) else 0)
        = (ArithmeticFunction.cardDistinctFactors (k + (m + 1)) : ℝ) + 1 := by
      rw [if_congr hd rfl rfl]
      have := congrArg (fun t : ℕ => (t : ℝ)) key
      simp only [Nat.cast_add, Nat.cast_ite, Nat.cast_one, Nat.cast_zero] at this
      exact this
    rw [one_div_pow, ← add_div, key', add_div]
  have hG : Summable fun k : ℕ => (1 / 2 : ℝ) ^ (k + 1) :=
    (summable_nat_add_iff 1).mpr summable_geometric_two
  have hsA : Summable fun k : ℕ =>
      (ArithmeticFunction.cardDistinctFactors (p * m + p * (k + 1)) : ℝ) / 2 ^ (k + 1) := by
    refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_)
      ((omega_shift_summable (m + 1)).add hG)
    have := hterm k
    have h0 : (0 : ℝ) ≤ (if p ^ 2 ∣ p * m + p * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) := by
      split_ifs <;> positivity
    linarith
  have hsB := ind_summable (fun k => p ^ 2 ∣ p * m + p * (k + 1))
  rw [← mul_add, ← hsA.tsum_add hsB, tsum_congr hterm,
    (omega_shift_summable (m + 1)).tsum_add hG]
  have hg : ∑' k : ℕ, (1 / 2 : ℝ) ^ (k + 1) = 1 := by
    simp_rw [pow_succ]; rw [tsum_mul_right, tsum_geometric_two]; norm_num
  rw [hg, mul_add, hz]; push_cast; ring

/-- `(-1)^{|ε|}` for `ε ∈ {0,1}^K`, as a product of signs. -/
private noncomputable def cubeSgn {K : ℕ} (ε : Fin K → Bool) : ℝ := ∏ i, if ε i then (-1 : ℝ) else 1

private lemma cubeSgn_eq {K : ℕ} (ε : Fin K → Bool) :
    cubeSgn ε = (-1 : ℝ) ^ (Finset.univ.filter fun i => ε i = true).card := by
  unfold cubeSgn
  rw [Finset.prod_ite]; simp

/-- The shifts (5.7): `r_{ε,h} = p₀ h + ∑_k (h - k) ε_k v_k` (coordinates `k = 1, …, K` are
`Fin K` indices `k - 1`). -/
private def cubeShift {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (ε : Fin K → Bool) (h : ℕ) : ℤ :=
  p0 * h + ∑ k : Fin K, ((h : ℤ) - (k.1 + 1)) * (if ε k then v k else 0)

/-- (5.7): `r_{ε,h} = p_ε h - ∑_k k ε_k v_k` with `p_ε = p₀ + ∑_k ε_k v_k`. -/
private lemma cubeShift_eq {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (ε : Fin K → Bool) (h : ℕ) :
    cubeShift p0 v ε h = (p0 + ∑ k : Fin K, (if ε k then v k else 0)) * h
      - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) := by
  unfold cubeShift
  rw [add_mul, Finset.sum_mul, add_sub_assoc, ← Finset.sum_sub_distrib]
  congr 1
  refine Finset.sum_congr rfl fun k _ => ?_
  ring

/-- The cancellation behind (5.8): for `1 ≤ h ≤ K` the alternating sum over the cube of any
function of `r_{ε,h}` vanishes, because `r_{ε,h}` does not depend on `ε_h`. -/
private theorem cube_cancel {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (g : ℤ → ℝ) {h : ℕ} (h1 : 1 ≤ h) (hK : h ≤ K) :
    ∑ ε : Fin K → Bool, cubeSgn ε * g (cubeShift p0 v ε h) = 0 := by
  let j : Fin K := ⟨h - 1, by omega⟩
  let fl : (Fin K → Bool) → (Fin K → Bool) := fun ε => Function.update ε j (!ε j)
  have hinv : Function.Involutive fl := by
    intro ε; funext i
    by_cases hi : i = j
    · subst hi; simp [fl]
    · simp [fl, Function.update_of_ne hi]
  have hshift : ∀ ε, cubeShift p0 v (fl ε) h = cubeShift p0 v ε h := by
    intro ε; unfold cubeShift; congr 1
    refine Finset.sum_congr rfl fun k _ => ?_
    by_cases hk : k = j
    · have : ((h : ℤ) - (k.1 + 1)) = 0 := by rw [hk]; simp only [j]; omega
      rw [this, zero_mul, zero_mul]
    · simp [fl, Function.update_of_ne hk]
  have hsgn : ∀ ε, cubeSgn (fl ε) = - cubeSgn ε := by
    intro ε; unfold cubeSgn
    rw [Fintype.prod_eq_mul_prod_compl j, Fintype.prod_eq_mul_prod_compl j (fun i => if ε i then (-1 : ℝ) else 1)]
    have hrest : ∏ i ∈ ({j}ᶜ : Finset (Fin K)), (if fl ε i then (-1 : ℝ) else 1)
        = ∏ i ∈ ({j}ᶜ : Finset (Fin K)), (if ε i then (-1 : ℝ) else 1) := by
      refine Finset.prod_congr rfl fun i hi => ?_
      have : i ≠ j := by simpa using hi
      simp [fl, Function.update_of_ne this]
    rw [hrest]
    cases hε : ε j <;> simp [fl, hε]
  set S := ∑ ε : Fin K → Bool, cubeSgn ε * g (cubeShift p0 v ε h) with hS
  have : S = -S := by
    let σ : (Fin K → Bool) ≃ (Fin K → Bool) := hinv.toPerm _
    calc S = ∑ ε, cubeSgn (σ ε) * g (cubeShift p0 v (σ ε) h) := (Equiv.sum_comp σ _).symm
      _ = ∑ ε, -(cubeSgn ε * g (cubeShift p0 v ε h)) := by
          refine Finset.sum_congr rfl fun ε _ => ?_
          show cubeSgn (fl ε) * g (cubeShift p0 v (fl ε) h) = _
          rw [hsgn, hshift]; ring
      _ = -S := by rw [Finset.sum_neg_distrib]
  linarith

private lemma omega_bound_summable (f : ℕ → ℕ) (a b : ℕ) (hf : ∀ k, f k ≤ a + b * k) :
    Summable fun k : ℕ => (ArithmeticFunction.cardDistinctFactors (f k) : ℝ) / 2 ^ (k + 1) := by
  have h0 : Summable fun k : ℕ => ((k : ℝ) ^ 1) * (1 / 2 : ℝ) ^ k :=
    summable_pow_mul_geometric_of_norm_lt_one 1 (by norm_num)
  have h1 : Summable fun k : ℕ => (b : ℝ) * (((k : ℝ) ^ 1) * (1 / 2 : ℝ) ^ k) := h0.mul_left _
  have h2 : Summable fun k : ℕ => (a : ℝ) * (1 / 2 : ℝ) ^ k := summable_geometric_two.mul_left _
  refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) (h2.add h1)
  have hw : (ArithmeticFunction.cardDistinctFactors (f k) : ℝ) ≤ a + b * k := by
    rw [omega_eq_card_filter]
    exact_mod_cast (Finset.card_filter_le _ _).trans ((Nat.card_divisors_le_self (f k)).trans (hf k))
  rw [div_le_iff₀ (by positivity), pow_one, one_div_pow]
  have : ((a : ℝ) * (1 / 2 ^ k) + b * (k * (1 / 2 ^ k))) * 2 ^ (k + 1) = 2 * (a + b * k) := by
    rw [pow_succ]; field_simp
  rw [this]
  have : (0 : ℝ) ≤ a + b * k := by positivity
  linarith

/-- Equation (5.8) of Tao–Teräväinen, from (5.1) for all `N`. Hypotheses: `P ε = p_ε` are primes
(5.4), and `n - ∑_k k ε_k v_k = p_ε m_ε` with `m_ε ∈ ℕ` (this is (5.5) together with (5.6)).
Conclusion: `q ∑_{h ≥ 1} ∑_ε (-1)^{|ε|} ω(n + r_{ε,h+K}) / 2^{h+K}
  + q ∑_ε (-1)^{|ε|} δ_{p_ε}(n - ∑_k k ε_k v_k)` is an integer (indices `h = k + 1`). -/
private theorem eq_5_8 (q : ℕ)
    (hint : ∀ N : ℕ, ∃ z : ℤ,
      (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (k + (N + 1)) : ℝ) / 2 ^ (k + 1) = z)
    {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ)
    (hP : ∀ ε, (P ε : ℤ) = p0 + ∑ k : Fin K, (if ε k then v k else 0))
    (hprime : ∀ ε, (P ε).Prime) (n : ℤ) (m : (Fin K → Bool) → ℕ)
    (hm : ∀ ε, n - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) = P ε * m ε) :
    ∃ z : ℤ,
      (q : ℝ) * ∑' k, (∑ ε : Fin K → Bool, cubeSgn ε *
          (ArithmeticFunction.cardDistinctFactors (n + cubeShift p0 v ε (k + K + 1)).toNat : ℝ))
          / 2 ^ (k + K + 1)
      + (q : ℝ) * ∑ ε : Fin K → Bool, cubeSgn ε *
          ∑' k, (if P ε ^ 2 ∣ P ε * m ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) = z := by
  -- the per-vertex congruences (5.2)
  have hz : ∀ ε, ∃ z : ℤ,
      (q : ℝ) * ∑' k, (ArithmeticFunction.cardDistinctFactors (P ε * m ε + P ε * (k + 1)) : ℝ) / 2 ^ (k + 1)
      + (q : ℝ) * ∑' k, (if P ε ^ 2 ∣ P ε * m ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) = z :=
    fun ε => eq_5_2 q hint (hprime ε) (m ε)
  choose z hz using hz
  -- the shift identity n + r_{ε,h} = p_ε m_ε + p_ε h
  have hsh : ∀ ε (h : ℕ), (n + cubeShift p0 v ε h).toNat = P ε * m ε + P ε * h := by
    intro ε h
    have : n + cubeShift p0 v ε h = ((P ε * m ε + P ε * h : ℕ) : ℤ) := by
      rw [cubeShift_eq, ← hP ε]; push_cast; linear_combination hm ε
    rw [this, Int.toNat_natCast]
  let a : (Fin K → Bool) → ℕ → ℝ := fun ε k =>
    (ArithmeticFunction.cardDistinctFactors (P ε * m ε + P ε * (k + 1)) : ℝ) / 2 ^ (k + 1)
  have ha : ∀ ε, Summable (a ε) := fun ε =>
    omega_bound_summable (fun k => P ε * m ε + P ε * (k + 1)) (P ε * m ε + P ε) (P ε)
      (fun k => by ring_nf; omega)
  let F : ℕ → ℝ := fun k => ∑ ε : Fin K → Bool, cubeSgn ε * a ε k
  have hF : Summable F := summable_sum fun ε _ => (ha ε).mul_left _
  -- the first K terms cancel (5.8)
  have hfirst : ∑ i ∈ Finset.range K, F i = 0 := by
    refine Finset.sum_eq_zero fun i hi => ?_
    have hiK : i + 1 ≤ K := Finset.mem_range.mp hi
    have := cube_cancel p0 v (fun t => (ArithmeticFunction.cardDistinctFactors (n + t).toNat : ℝ) / 2 ^ (i + 1))
      (h := i + 1) (by omega) hiK
    simp only [F, a]
    rw [← this]
    refine Finset.sum_congr rfl fun ε _ => ?_
    rw [hsh]
  have htail : ∀ k, F (k + K) = (∑ ε : Fin K → Bool, cubeSgn ε *
      (ArithmeticFunction.cardDistinctFactors (n + cubeShift p0 v ε (k + K + 1)).toNat : ℝ))
      / 2 ^ (k + K + 1) := by
    intro k
    simp only [F, a]
    rw [Finset.sum_div]
    refine Finset.sum_congr rfl fun ε _ => ?_
    rw [hsh, mul_div_assoc]
  have hsum := hF.sum_add_tsum_nat_add K
  rw [hfirst, zero_add] at hsum
  simp_rw [htail] at hsum
  rw [hsum]
  have hswap : ∑' k, F k = ∑ ε : Fin K → Bool, cubeSgn ε * ∑' k, a ε k := by
    simp only [F]
    rw [Summable.tsum_finsetSum fun ε _ => (ha ε).mul_left _]
    refine Finset.sum_congr rfl fun ε _ => ?_
    rw [tsum_mul_left]
  rw [hswap]
  refine ⟨∑ ε : Fin K → Bool, (-1 : ℤ) ^ (Finset.univ.filter fun i => ε i = true).card * z ε, ?_⟩
  push_cast
  rw [Finset.mul_sum, Finset.mul_sum, ← Finset.sum_add_distrib]
  refine Finset.sum_congr rfl fun ε _ => ?_
  rw [← hz ε, cubeSgn_eq]
  simp only [a]
  ring

/-- Rationality of `∑_p 1/(2^p - 1)` implies (5.8) for every prime cube and every admissible `n`:
S1–S4 of the route map (annex on `variant-6b364d2b`) chained together. -/
private theorem rational_imp_5_8 (a : ℤ) (q : ℕ) (hq : 0 < q)
    (h : ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) = a / q)
    {K : ℕ} (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ)
    (hP : ∀ ε, (P ε : ℤ) = p0 + ∑ k : Fin K, (if ε k then v k else 0))
    (hprime : ∀ ε, (P ε).Prime) (n : ℤ) (m : (Fin K → Bool) → ℕ)
    (hm : ∀ ε, n - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) = P ε * m ε) :
    ∃ z : ℤ,
      (q : ℝ) * ∑' k, (∑ ε : Fin K → Bool, cubeSgn ε *
          (ArithmeticFunction.cardDistinctFactors (n + cubeShift p0 v ε (k + K + 1)).toNat : ℝ))
          / 2 ^ (k + K + 1)
      + (q : ℝ) * ∑ ε : Fin K → Bool, cubeSgn ε *
          ∑' k, (if P ε ^ 2 ∣ P ε * m ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) = z :=
  eq_5_8 q (fun N => eq_5_1 a q hq h N) p0 v P hP hprime n m hm

/-- `2^{ω(n)} ≤ n` for `n ≠ 0`, i.e. `ω(n) ≤ log₂ n`: the crude bound `ω(n) ≪ log n` of §5.8. -/
private theorem two_pow_omega_le {n : ℕ} (hn : n ≠ 0) : 2 ^ ArithmeticFunction.cardDistinctFactors n ≤ n := by
  rw [omega_eq_card_pf]
  calc 2 ^ n.primeFactors.card = ∏ p ∈ n.primeFactors, 2 := by rw [Finset.prod_const]
    _ ≤ ∏ p ∈ n.primeFactors, p :=
        Finset.prod_le_prod' fun p hp => (Nat.prime_of_mem_primeFactors hp).two_le
    _ ≤ n := Nat.le_of_dvd (Nat.pos_of_ne_zero hn) (Nat.prod_primeFactors_dvd n)

/-- `e(t) = exp(2πit)`. -/
private noncomputable def ee (t : ℝ) : ℂ := Complex.exp (((2 * Real.pi * t : ℝ) : ℂ) * Complex.I)

private lemma ee_norm (t : ℝ) : ‖ee t‖ = 1 := Complex.norm_exp_ofReal_mul_I _

private lemma ee_add (a b : ℝ) : ee (a + b) = ee a * ee b := by
  unfold ee; rw [← Complex.exp_add]; congr 1; push_cast; ring

private lemma ee_int (z : ℤ) : ee z = 1 := by
  unfold ee
  rw [← Complex.exp_int_mul_two_pi_mul_I z]; congr 1; push_cast; ring

private lemma ee_re (t : ℝ) : (ee t).re = Real.cos (2 * Real.pi * t) :=
  Complex.exp_ofReal_mul_I_re _

private lemma ee_conj (t : ℝ) : (starRingEnd ℂ) (ee t) = ee (-t) := by
  unfold ee; rw [← Complex.exp_conj]; congr 1
  simp only [map_mul, Complex.conj_ofReal, Complex.conj_I]; push_cast; ring

/-- `‖e(t) - 1‖ ≤ 2π|t|`. -/
private lemma ee_sub_one (t : ℝ) : ‖ee t - 1‖ ≤ 2 * Real.pi * |t| := by
  set x : ℝ := 2 * Real.pi * t with hx
  have hre : (ee t - 1).re = Real.cos x - 1 := by
    rw [Complex.sub_re, ee_re, Complex.one_re]
  have him : (ee t - 1).im = Real.sin x := by
    simp only [Complex.sub_im, Complex.one_im, sub_zero, ee]
    exact Complex.exp_ofReal_mul_I_im _
  have hsq : ‖ee t - 1‖ ^ 2 ≤ x ^ 2 := by
    rw [← Complex.normSq_eq_norm_sq, Complex.normSq_apply, hre, him]
    have h1 := Real.one_sub_sq_div_two_le_cos (x := x)
    have h2 := Real.sin_sq_add_cos_sq x
    nlinarith
  have := (sq_le_sq.mp hsq)
  rw [abs_of_nonneg (norm_nonneg _)] at this
  calc ‖ee t - 1‖ ≤ |x| := this
    _ = 2 * Real.pi * |t| := by
      rw [hx, abs_mul, abs_of_pos (by positivity : (0 : ℝ) < 2 * Real.pi)]

/-- `∑∑ (a_i - a_j)^2 = 2N ∑ a^2 - 2 (∑ a)^2`. -/
private lemma sum_sum_sub_sq {ι : Type*} (s : Finset ι) (a : ι → ℝ) :
    ∑ i ∈ s, ∑ j ∈ s, (a i - a j) ^ 2
      = 2 * (s.card : ℝ) * ∑ i ∈ s, a i ^ 2 - 2 * (∑ i ∈ s, a i) ^ 2 := by
  have e1 : ∀ i j, (a i - a j) ^ 2 = a i ^ 2 + a j ^ 2 - 2 * (a i * a j) := fun i j => by ring
  simp only [e1, Finset.sum_sub_distrib, Finset.sum_add_distrib, Finset.sum_const, nsmul_eq_mul,
    ← Finset.mul_sum]
  rw [← Finset.sum_mul]; ring

/-- The variance step of (5.20): for `|X| ≤ 1/4` on a finite nonempty set, with `n` uniform,
`|E e(X)|^2 ≤ 1 - 16 Var X`. -/
private lemma norm_avg_ee_sq_le {ι : Type*} (s : Finset ι) (hs : s.Nonempty) (X : ι → ℝ)
    (hX : ∀ i ∈ s, |X i| ≤ 1 / 4) :
    ‖((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X i)‖ ^ 2
      ≤ 1 - 16 * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X i ^ 2 - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X i) ^ 2) := by
  have hN : (0 : ℝ) < s.card := by exact_mod_cast hs.card_pos
  set S : ℂ := ∑ i ∈ s, ee (X i) with hS
  have hnorm : ‖S‖ ^ 2 = ∑ i ∈ s, ∑ j ∈ s, Real.cos (2 * Real.pi * (X i - X j)) := by
    have h1 : ((‖S‖ ^ 2 : ℝ) : ℂ) = S * (starRingEnd ℂ) S := by
      rw [Complex.mul_conj, Complex.normSq_eq_norm_sq]
    have h2 : S * (starRingEnd ℂ) S = ∑ i ∈ s, ∑ j ∈ s, ee (X i - X j) := by
      rw [hS, map_sum, Finset.sum_mul_sum]
      refine Finset.sum_congr rfl fun i _ => Finset.sum_congr rfl fun j _ => ?_
      rw [ee_conj, sub_eq_add_neg, ee_add]
    have h3 := congrArg Complex.re (h1.trans h2)
    rw [Complex.ofReal_re] at h3
    rw [h3, Complex.re_sum]
    refine Finset.sum_congr rfl fun i _ => ?_
    rw [Complex.re_sum]
    exact Finset.sum_congr rfl fun j _ => ee_re _
  have hcos : ∀ i ∈ s, ∀ j ∈ s,
      Real.cos (2 * Real.pi * (X i - X j)) ≤ 1 - 8 * (X i - X j) ^ 2 := by
    intro i hi j hj
    have hij : |X i - X j| ≤ 1 / 2 := by
      have := abs_sub (X i) (X j); linarith [hX i hi, hX j hj]
    have hpi : |2 * Real.pi * (X i - X j)| ≤ Real.pi := by
      rw [abs_mul, abs_of_pos (by positivity : (0 : ℝ) < 2 * Real.pi)]
      nlinarith [Real.pi_pos]
    have := Real.cos_le_one_sub_mul_cos_sq hpi
    have hp : Real.pi ≠ 0 := Real.pi_ne_zero
    calc Real.cos (2 * Real.pi * (X i - X j)) ≤ 1 - 2 / Real.pi ^ 2 * (2 * Real.pi * (X i - X j)) ^ 2 :=
          this
      _ = 1 - 8 * (X i - X j) ^ 2 := by field_simp; ring
  have hsum : ‖S‖ ^ 2 ≤ (s.card : ℝ) ^ 2 - 8 * (2 * (s.card : ℝ) * ∑ i ∈ s, X i ^ 2
      - 2 * (∑ i ∈ s, X i) ^ 2) := by
    rw [hnorm, ← sum_sum_sub_sq]
    calc ∑ i ∈ s, ∑ j ∈ s, Real.cos (2 * Real.pi * (X i - X j))
        ≤ ∑ i ∈ s, ∑ j ∈ s, (1 - 8 * (X i - X j) ^ 2) :=
          Finset.sum_le_sum fun i hi => Finset.sum_le_sum fun j hj => hcos i hi j hj
      _ = (s.card : ℝ) ^ 2 - 8 * ∑ i ∈ s, ∑ j ∈ s, (X i - X j) ^ 2 := by
          simp only [Finset.sum_sub_distrib, Finset.sum_const, nsmul_eq_mul, mul_one,
            ← Finset.mul_sum]
          ring
  rw [norm_mul, mul_pow, norm_inv, Complex.norm_real, Real.norm_eq_abs, abs_of_pos hN]
  have hN2 : (0 : ℝ) < (s.card : ℝ) ^ 2 := by positivity
  rw [inv_pow, inv_mul_le_iff₀ hN2]
  have : (s.card : ℝ) ^ 2 * (1 - 16 * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X i ^ 2
      - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X i) ^ 2))
      = (s.card : ℝ) ^ 2 - 8 * (2 * (s.card : ℝ) * ∑ i ∈ s, X i ^ 2
      - 2 * (∑ i ∈ s, X i) ^ 2) := by
    field_simp; ring
  rw [this]; exact hsum

/-- The product step of (5.20): `|∏_p E e(X_p)| ≤ exp(-8 ∑_p Var X_p)`. -/
private lemma norm_prod_avg_le {ι : Type*} (s : Finset ι) (hs : s.Nonempty) (S1 : Finset ℕ)
    (X : ℕ → ι → ℝ) (hX : ∀ p ∈ S1, ∀ i ∈ s, |X p i| ≤ 1 / 4) :
    ‖∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i)‖
      ≤ Real.exp (-8 * ∑ p ∈ S1, ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2)) := by
  have hsq : ‖∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i)‖ ^ 2
      ≤ Real.exp (-8 * ∑ p ∈ S1, ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2)) ^ 2 := by
    rw [norm_prod, ← Finset.prod_pow, sq, ← Real.exp_add, Finset.mul_sum, ← Finset.sum_add_distrib,
      Real.exp_sum]
    refine Finset.prod_le_prod (fun p _ => by positivity) fun p hp => ?_
    refine (norm_avg_ee_sq_le s hs (X p) (hX p hp)).trans ?_
    have := Real.add_one_le_exp (-8 * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2) + -8 * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2))
    linarith
  have := sq_le_sq.mp hsq
  rwa [abs_of_nonneg (norm_nonneg _), abs_of_pos (Real.exp_pos _)] at this

/-- S5–S7 assembled: if every `Y + C₀ + W` is an integer, `E|W| ≤ δ`, the decorrelation defect is
at most `δ`, `|X_p| ≤ 1/4` and `∑ Var X_p ≥ c₀`, then `1 - 8δ ≤ exp(-8c₀)`. -/
private lemma assembly_core {ι : Type*} (s : Finset ι) (hs : s.Nonempty) (S1 : Finset ℕ)
    (X : ℕ → ι → ℝ) (W : ι → ℝ) (C0 δ c0 : ℝ)
    (hint : ∀ i ∈ s, ∃ z : ℤ, (∑ p ∈ S1, X p i) + C0 + W i = z)
    (hX : ∀ p ∈ S1, ∀ i ∈ s, |X p i| ≤ 1 / 4)
    (hW : (s.card : ℝ)⁻¹ * ∑ i ∈ s, |W i| ≤ δ)
    (hdec : ‖((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (∑ p ∈ S1, X p i)
        - ∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i)‖ ≤ δ)
    (hvar : c0 ≤ ∑ p ∈ S1, ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2)) :
    1 - 8 * δ ≤ Real.exp (-8 * c0) := by
  have hN : (0 : ℝ) < s.card := by exact_mod_cast hs.card_pos
  have hδ : 0 ≤ δ := le_trans (by positivity) hW
  -- each phase is `e(-C₀) e(-W)`
  have hpt : ∀ i ∈ s, ‖ee (∑ p ∈ S1, X p i) - ee (-C0)‖ ≤ 2 * Real.pi * |W i| := by
    intro i hi
    obtain ⟨z, hz⟩ := hint i hi
    have : ee (∑ p ∈ S1, X p i) = ee (-C0) * ee (-W i) := by
      have e1 : (∑ p ∈ S1, X p i) = (z : ℝ) + -C0 + -W i := by linarith
      rw [e1, ee_add, ee_add, ee_int, one_mul]
    rw [this, ← mul_sub_one, norm_mul, ee_norm, one_mul]
    have := ee_sub_one (-W i); rwa [abs_neg] at this
  set A : ℂ := ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (∑ p ∈ S1, X p i) with hA
  have hA1 : ‖A - ee (-C0)‖ ≤ 2 * Real.pi * δ := by
    have hc : ee (-C0) = ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (-C0) := by
      have : (s.card : ℂ) ≠ 0 := by exact_mod_cast hN.ne'
      rw [Finset.sum_const, nsmul_eq_mul, ← mul_assoc, Complex.ofReal_natCast,
        inv_mul_cancel₀ this, one_mul]
    rw [hA, hc, ← mul_sub, ← Finset.sum_sub_distrib, norm_mul, norm_inv, Complex.norm_real,
      Real.norm_eq_abs, abs_of_pos hN]
    calc (s.card : ℝ)⁻¹ * ‖∑ i ∈ s, (ee (∑ p ∈ S1, X p i) - ee (-C0))‖
        ≤ (s.card : ℝ)⁻¹ * ∑ i ∈ s, 2 * Real.pi * |W i| := by
          exact mul_le_mul_of_nonneg_left
            ((norm_sum_le _ _).trans (Finset.sum_le_sum hpt)) (inv_nonneg.mpr hN.le)
      _ = 2 * Real.pi * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, |W i|) := by rw [← Finset.mul_sum]; ring
      _ ≤ 2 * Real.pi * δ := mul_le_mul_of_nonneg_left hW (by linarith [Real.pi_pos])
  have hA2 : 1 - 2 * Real.pi * δ ≤ ‖A‖ := by
    have := norm_sub_norm_le (ee (-C0)) (ee (-C0) - A)
    rw [sub_sub_cancel, ee_norm, norm_sub_rev] at this
    linarith
  have hP := norm_prod_avg_le s hs S1 X hX
  have hP2 : ‖A‖ - δ ≤ ‖∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i)‖ := by
    have := norm_sub_norm_le A (A - ∏ p ∈ S1, ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (X p i))
    rw [sub_sub_cancel] at this
    linarith
  have hexp : Real.exp (-8 * ∑ p ∈ S1, ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i ^ 2
          - ((s.card : ℝ)⁻¹ * ∑ i ∈ s, X p i) ^ 2)) ≤ Real.exp (-8 * c0) :=
    Real.exp_le_exp.mpr (by linarith)
  have hpi : Real.pi < 3.15 := Real.pi_lt_d2
  nlinarith

/-- Independence: the average over `Ns^{S₁}` of a product is the product of averages. -/
private lemma sum_pi_prod {R : Type*} [CommSemiring R] (S1 : Finset ℕ) (Ns : Finset ℤ)
    (f : ℕ → ℤ → R) :
    ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), ∏ p : S1, f p (ω p) = ∏ p ∈ S1, ∑ n ∈ Ns, f p n := by
  rw [← Finset.prod_univ_sum (fun _ : S1 => Ns) (fun p n => f p n)]
  exact Finset.prod_coe_sort S1 (fun p => ∑ n ∈ Ns, f p n)

private lemma card_pi (S1 : Finset ℕ) (Ns : Finset ℤ) :
    ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ) = (Ns.card : ℝ) ^ S1.card := by
  rw [Fintype.card_piFinset, Finset.prod_const, Finset.card_univ, Fintype.card_coe]; push_cast; rfl

/-- `E' e(∑ X'_p) = ∏ E e(X_p)` for independent copies. -/
private lemma avg_pi_ee (S1 : Finset ℕ) (Ns : Finset ℤ) (X : ℕ → ℤ → ℝ) :
    (((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ) : ℂ)⁻¹
        * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), ee (∑ p : S1, X p (ω p))
      = ∏ p ∈ S1, ((Ns.card : ℝ) : ℂ)⁻¹ * ∑ n ∈ Ns, ee (X p n) := by
  have hprod : ∀ ω : S1 → ℤ, ee (∑ p : S1, X p (ω p)) = ∏ p : S1, ee (X p (ω p)) := by
    intro ω
    unfold ee
    rw [← Complex.exp_sum]; congr 1; push_cast; rw [Finset.mul_sum, Finset.sum_mul]
  simp_rw [hprod]
  rw [sum_pi_prod S1 Ns (fun p n => ee (X p n)), card_pi, Finset.prod_mul_distrib, Finset.prod_const]
  push_cast
  rw [inv_pow]

/-- `E' exp(c ∑ X'_p) = ∏ E exp(c X_p)`. -/
private lemma avg_pi_exp (S1 : Finset ℕ) (Ns : Finset ℤ) (X : ℕ → ℤ → ℝ) (c : ℝ) :
    ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ)⁻¹
        * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), Real.exp (c * ∑ p : S1, X p (ω p))
      = ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp (c * X p n) := by
  have hprod : ∀ ω : S1 → ℤ, Real.exp (c * ∑ p : S1, X p (ω p)) = ∏ p : S1, Real.exp (c * X p (ω p)) := by
    intro ω; rw [← Real.exp_sum, Finset.mul_sum]
  simp_rw [hprod]
  rw [sum_pi_prod S1 Ns (fun p n => Real.exp (c * X p n)), card_pi, Finset.prod_mul_distrib,
    Finset.prod_const, inv_pow]

/-- `t^M/M! ≤ e^t + e^{-t}` for even `M`. -/
private lemma pow_div_fact_le_exp_add {M : ℕ} (hM : Even M) (t : ℝ) :
    t ^ M / M.factorial ≤ Real.exp t + Real.exp (-t) := by
  have h1 := Real.pow_div_factorial_le_exp |t| (abs_nonneg t) M
  have h2 : t ^ M = |t| ^ M := (hM.pow_abs t).symm
  rw [h2]
  rcases le_total 0 t with ht | ht
  · rw [abs_of_nonneg ht] at h1 ⊢; linarith [Real.exp_pos (-t)]
  · rw [abs_of_nonpos ht] at h1 ⊢; linarith [Real.exp_pos t]

/-- The sharp Taylor bound for `e(y)`, from the one for `exp(ix)`. -/
private lemma ee_taylor (htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial) (M : ℕ) (y : ℝ) :
    ‖ee y - ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (y : ℂ) ^ m‖
      ≤ (2 * Real.pi) ^ M / M.factorial * |y| ^ M := by
  have h := htay (2 * Real.pi * y) M
  have e1 : ∑ m ∈ Finset.range M, (((2 * Real.pi * y : ℝ) : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)
      = ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (y : ℂ) ^ m :=
    Finset.sum_congr rfl fun m _ => by push_cast; ring
  rw [e1, abs_mul, abs_of_pos (by positivity : (0 : ℝ) < 2 * Real.pi), mul_pow] at h
  unfold ee
  calc _ ≤ (2 * Real.pi) ^ M * |y| ^ M / M.factorial := h
    _ = _ := by ring

/-- An average of `e(Z)` is the Taylor part plus the average remainder. -/
private lemma avg_ee_split {γ : Type*} (u : Finset γ) (Z : γ → ℝ) (M : ℕ) :
    ((u.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ u, ee (Z i)
      = ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((u.card : ℝ)⁻¹ * ∑ i ∈ u, Z i ^ m : ℝ) : ℂ)
        + ((u.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ u, (ee (Z i) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Z i : ℂ) ^ m) := by
  have hC : ((u.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ u, ∑ m ∈ Finset.range M,
        (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Z i : ℂ) ^ m
      = ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((u.card : ℝ)⁻¹ * ∑ i ∈ u, Z i ^ m : ℝ) : ℂ) := by
    rw [Finset.sum_comm, Finset.mul_sum]
    refine Finset.sum_congr rfl fun m _ => ?_
    rw [← Finset.mul_sum]; push_cast; ring
  rw [Finset.sum_sub_distrib, mul_sub, hC]; ring

private lemma norm_avg_rem_le {γ : Type*} (u : Finset γ) (Z : γ → ℝ) (M : ℕ)
    (htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial) :
    ‖((u.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ u, (ee (Z i) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Z i : ℂ) ^ m)‖
      ≤ (2 * Real.pi) ^ M / M.factorial * ((u.card : ℝ)⁻¹ * ∑ i ∈ u, |Z i| ^ M) := by
  rw [norm_mul, norm_inv, Complex.norm_real, Real.norm_eq_abs,
    abs_of_nonneg (Nat.cast_nonneg _)]
  calc (u.card : ℝ)⁻¹ * ‖∑ i ∈ u, (ee (Z i) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Z i : ℂ) ^ m)‖
      ≤ (u.card : ℝ)⁻¹ * ∑ i ∈ u, (2 * Real.pi) ^ M / M.factorial * |Z i| ^ M :=
        mul_le_mul_of_nonneg_left ((norm_sum_le _ _).trans
          (Finset.sum_le_sum fun i _ => ee_taylor htay M (Z i))) (inv_nonneg.mpr (Nat.cast_nonneg _))
    _ = _ := by rw [← Finset.mul_sum]; ring

private lemma norm_coef (m : ℕ) :
    ‖(2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)‖ = (2 * Real.pi) ^ m / m.factorial := by
  rw [norm_div, norm_pow, Complex.norm_natCast, norm_mul, Complex.norm_I, mul_one, norm_mul,
    Complex.norm_ofNat, Complex.norm_real, Real.norm_eq_abs, abs_of_pos Real.pi_pos]

/-- The comparison at the start of §5.4: `|E e(Y) - E e(Y')| ≤ ∑_{m<M} (2π)^m/m! |E Y^m - E Y'^m|
+ (2π)^M/M! (E|Y|^M + E|Y'|^M)`. -/
private lemma avg_ee_compare {α β : Type*} (s : Finset α) (t : Finset β) (Y : α → ℝ) (Y' : β → ℝ)
    (M : ℕ) (htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial) :
    ‖((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, ee (Y i) - ((t.card : ℝ) : ℂ)⁻¹ * ∑ j ∈ t, ee (Y' j)‖
      ≤ ∑ m ∈ Finset.range M, (2 * Real.pi) ^ m / m.factorial
          * |(s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m - (t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m|
        + (2 * Real.pi) ^ M / M.factorial * ((s.card : ℝ)⁻¹ * ∑ i ∈ s, |Y i| ^ M
          + (t.card : ℝ)⁻¹ * ∑ j ∈ t, |Y' j| ^ M) := by
  rw [avg_ee_split s Y M, avg_ee_split t Y' M]
  set Rs := ((s.card : ℝ) : ℂ)⁻¹ * ∑ i ∈ s, (ee (Y i) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Y i : ℂ) ^ m)
  set Rt := ((t.card : ℝ) : ℂ)⁻¹ * ∑ j ∈ t, (ee (Y' j) - ∑ m ∈ Finset.range M,
            (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ) * (Y' j : ℂ) ^ m)
  have hRs := norm_avg_rem_le s Y M htay
  have hRt := norm_avg_rem_le t Y' M htay
  have hmain : ‖∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m : ℝ) : ℂ)
        - ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m : ℝ) : ℂ)‖
      ≤ ∑ m ∈ Finset.range M, (2 * Real.pi) ^ m / m.factorial
          * |(s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m - (t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m| := by
    rw [← Finset.sum_sub_distrib]
    refine (norm_sum_le _ _).trans (Finset.sum_le_sum fun m _ => ?_)
    rw [← mul_sub, norm_mul, norm_coef, ← Complex.ofReal_sub, Complex.norm_real, Real.norm_eq_abs]
  set A := ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m : ℝ) : ℂ)
        - ∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m : ℝ) : ℂ) with hA
  have h3 : ‖A + Rs + -Rt‖ ≤ ‖A‖ + ‖Rs‖ + ‖-Rt‖ := norm_add₃_le
  rw [norm_neg] at h3
  have heq : ‖(∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((s.card : ℝ)⁻¹ * ∑ i ∈ s, Y i ^ m : ℝ) : ℂ) + Rs)
        - (∑ m ∈ Finset.range M, (2 * Real.pi * Complex.I) ^ m / (m.factorial : ℂ)
          * (((t.card : ℝ)⁻¹ * ∑ j ∈ t, Y' j ^ m : ℝ) : ℂ) + Rt)‖ = ‖A + Rs + -Rt‖ := by
    congr 1; rw [hA]; ring
  rw [heq]
  linarith

/-- (5.16)–(5.17) give the decorrelation: `|E e(∑ X_p) - ∏ E e(X_p)| ≤ 2κ₄ + 2κ₅`, with `X'_p`
independent copies (uniform on `Ns^{S₁}`) and `e = exp 1` in `κ₅`. -/
private lemma decor_bound (Ns : Finset ℤ) (S1 : Finset ℕ) (X : ℕ → ℤ → ℝ) (M : ℕ) (hM : Even M)
    (htay : ∀ (x : ℝ) (M : ℕ), ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial) :
    ‖((Ns.card : ℝ) : ℂ)⁻¹ * ∑ n ∈ Ns, ee (∑ p ∈ S1, X p n)
        - ∏ p ∈ S1, ((Ns.card : ℝ) : ℂ)⁻¹ * ∑ n ∈ Ns, ee (X p n)‖
      ≤ 2 * (∑ m ∈ Finset.range (M + 1), (2 * Real.pi) ^ m / m.factorial
          * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ m
            - ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ)⁻¹
              * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), (∑ p : S1, X p (ω p)) ^ m|)
        + 2 * ((∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
              Real.exp (2 * Real.pi * Real.exp 1 * X p n)
            + ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
              Real.exp (-(2 * Real.pi * Real.exp 1) * X p n)) / Real.exp 1 ^ M) := by
  set Ω := Fintype.piFinset (fun _ : S1 => Ns) with hΩ
  rw [← avg_pi_ee S1 Ns X]
  have hc := avg_ee_compare Ns Ω (fun n => ∑ p ∈ S1, X p n) (fun ω => ∑ p : S1, X p (ω p)) M htay
  beta_reduce at hc
  -- |Y|^M = Y^M
  have habs : ∀ y : ℝ, |y| ^ M = y ^ M := fun y => hM.pow_abs y
  simp only [habs] at hc
  -- the kappa_5 bound on the independent M-th moment
  have hk5 : (2 * Real.pi) ^ M / M.factorial
        * ((Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M)
      ≤ (∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp (2 * Real.pi * Real.exp 1 * X p n)
          + ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
              Real.exp (-(2 * Real.pi * Real.exp 1) * X p n)) / Real.exp 1 ^ M := by
    rw [← avg_pi_exp S1 Ns X, ← avg_pi_exp S1 Ns X, ← mul_add, ← Finset.sum_add_distrib]
    have hE : (0 : ℝ) < Real.exp 1 ^ M := by positivity
    rw [mul_div_assoc, Finset.sum_div, mul_left_comm]
    refine mul_le_mul_of_nonneg_left ?_ (inv_nonneg.mpr (Nat.cast_nonneg _))
    rw [Finset.mul_sum]
    refine Finset.sum_le_sum fun ω _ => ?_
    have := pow_div_fact_le_exp_add hM (2 * Real.pi * Real.exp 1 * ∑ p : S1, X p (ω p))
    rw [le_div_iff₀ hE]
    calc (2 * Real.pi) ^ M / M.factorial * (∑ p : S1, X p (ω p)) ^ M * Real.exp 1 ^ M
        = (2 * Real.pi * Real.exp 1 * ∑ p : S1, X p (ω p)) ^ M / M.factorial := by ring
      _ ≤ _ := by rw [neg_mul]; exact this
  have hsplit : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ M
      ≤ |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ M
          - (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M|
        + (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M := by
    have := le_abs_self ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ M
          - (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M)
    linarith
  rw [Finset.sum_range_succ]
  have hcoef : (0 : ℝ) ≤ (2 * Real.pi) ^ M / M.factorial := by positivity
  have hterms : 0 ≤ ∑ m ∈ Finset.range M, (2 * Real.pi) ^ m / m.factorial
          * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ m
            - (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ m| :=
    Finset.sum_nonneg fun m _ => mul_nonneg (by positivity) (abs_nonneg _)
  have hlast : 0 ≤ (2 * Real.pi) ^ M / M.factorial
          * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ M
            - (Ω.card : ℝ)⁻¹ * ∑ ω ∈ Ω, (∑ p : S1, X p (ω p)) ^ M| :=
    mul_nonneg hcoef (abs_nonneg _)
  have := mul_le_mul_of_nonneg_left hsplit hcoef
  nlinarith

private lemma taylor_term_deriv (m : ℕ) (t : ℝ) :
    HasDerivAt (fun s : ℝ => ((s : ℂ) * Complex.I) ^ (m + 1) / ((m + 1).factorial : ℂ))
      (Complex.I * (((t : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))) t := by
  have h0 : HasDerivAt (fun s : ℝ => (s : ℂ) * Complex.I) (1 * Complex.I) t :=
    ((hasDerivAt_id t).ofReal_comp).mul_const Complex.I
  have h1 := (h0.pow (m + 1)).div_const ((m + 1).factorial : ℂ)
  refine h1.congr_deriv ?_
  rw [Nat.factorial_succ]
  push_cast
  have hf : (m.factorial : ℂ) ≠ 0 := by exact_mod_cast (Nat.factorial_pos m).ne'
  have hm : ((m : ℂ) + 1) ≠ 0 := by exact_mod_cast Nat.succ_ne_zero m
  field_simp

private lemma taylor_deriv (M : ℕ) (t : ℝ) :
    HasDerivAt (fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range (M + 1), ((s : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))
      (Complex.I * (Complex.exp ((t : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range M, ((t : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))) t := by
  have he : HasDerivAt (fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I))
      (Complex.exp ((t : ℂ) * Complex.I) * (1 * Complex.I)) t :=
    (((hasDerivAt_id t).ofReal_comp).mul_const Complex.I).cexp
  have hs : HasDerivAt (fun s : ℝ => ∑ m ∈ Finset.range M,
        ((s : ℂ) * Complex.I) ^ (m + 1) / ((m + 1).factorial : ℂ))
      (∑ m ∈ Finset.range M, Complex.I * (((t : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))) t :=
    HasDerivAt.fun_sum fun m _ => taylor_term_deriv m t
  have hfun : (fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range (M + 1), ((s : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))
      = fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I) - (∑ m ∈ Finset.range M,
        ((s : ℂ) * Complex.I) ^ (m + 1) / ((m + 1).factorial : ℂ) + 1) := by
    funext s; rw [Finset.sum_range_succ']; simp
  rw [hfun]
  refine (he.sub (hs.add_const 1)).congr_deriv ?_
  rw [← Finset.mul_sum]; ring

private lemma taylor_nonneg (M : ℕ) : ∀ x : ℝ, 0 ≤ x →
    ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ x ^ M / M.factorial := by
  induction M with
  | zero =>
    intro x _
    simp [Complex.norm_exp_ofReal_mul_I]
  | succ M ih =>
    intro x hx
    have hB : ∀ t : ℝ, HasDerivAt (fun s : ℝ => s ^ (M + 1) / ((M + 1).factorial : ℝ))
        (t ^ M / (M.factorial : ℝ)) t := by
      intro t
      refine ((hasDerivAt_pow (M + 1) t).div_const ((M + 1).factorial : ℝ)).congr_deriv ?_
      rw [Nat.factorial_succ]; push_cast
      have hf : (M.factorial : ℝ) ≠ 0 := by exact_mod_cast (Nat.factorial_pos M).ne'
      field_simp
    have := image_norm_le_of_norm_deriv_right_le_deriv_boundary
      (f := fun s : ℝ => Complex.exp ((s : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range (M + 1), ((s : ℂ) * Complex.I) ^ m / (m.factorial : ℂ))
      (a := 0) (b := x)
      (f' := fun t => Complex.I * (Complex.exp ((t : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range M, ((t : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)))
      (fun t _ => (taylor_deriv M t).continuousAt.continuousWithinAt)
      (fun t _ => (taylor_deriv M t).hasDerivWithinAt)
      (B := fun s => s ^ (M + 1) / ((M + 1).factorial : ℝ)) (B' := fun t => t ^ M / (M.factorial : ℝ))
      (by simp [Finset.sum_range_succ']) hB
      (fun t ht => by
        rw [norm_mul, Complex.norm_I, one_mul]
        exact ih t ht.1)
      (Set.right_mem_Icc.mpr hx)
    simpa using this

/-- The sharp Taylor bound for `exp(ix)`, every real `x`: `|e^{ix} - ∑_{m<M} (ix)^m/m!| ≤ |x|^M/M!`. -/
private lemma taylor_all (x : ℝ) (M : ℕ) :
    ‖Complex.exp ((x : ℂ) * Complex.I)
      - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)‖
      ≤ |x| ^ M / M.factorial := by
  rcases le_total 0 x with hx | hx
  · rw [abs_of_nonneg hx]; exact taylor_nonneg M x hx
  · have h := taylor_nonneg M (-x) (by linarith)
    rw [abs_of_nonpos hx]
    have hc : Complex.exp ((x : ℂ) * Complex.I)
        - ∑ m ∈ Finset.range M, ((x : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)
        = (starRingEnd ℂ) (Complex.exp (((-x : ℝ) : ℂ) * Complex.I)
          - ∑ m ∈ Finset.range M, (((-x : ℝ) : ℂ) * Complex.I) ^ m / (m.factorial : ℂ)) := by
      have hz : (starRingEnd ℂ) (((-x : ℝ) : ℂ) * Complex.I) = (x : ℂ) * Complex.I := by
        simp only [map_mul, Complex.conj_ofReal, Complex.conj_I]; push_cast; ring
      rw [map_sub, map_sum, ← Complex.exp_conj, hz]
      congr 1
      refine Finset.sum_congr rfl fun m _ => ?_
      rw [map_div₀, map_pow, hz, map_natCast]
    rw [hc, Complex.norm_conj]
    exact h

/-- `ω(u) = #{p ≤ B prime : p ∣ u}` for `0 < u ≤ B`. -/
private lemma omega_eq_sum_primes_le {u B : ℕ} (hu : 0 < u) (huB : u ≤ B) :
    (ArithmeticFunction.cardDistinctFactors u : ℝ)
      = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, (if p ∣ u then (1 : ℝ) else 0) := by
  rw [Finset.sum_boole, omega_eq_card_pf]
  congr 2
  ext p
  simp only [Nat.mem_primeFactors, Finset.mem_filter, Finset.mem_range]
  constructor
  · rintro ⟨hp, hd, -⟩
    have := Nat.le_of_dvd hu hd
    exact ⟨⟨by omega, hp⟩, hd⟩
  · rintro ⟨⟨-, hp⟩, hd⟩
    exact ⟨hp, hd, hu.ne'⟩

/-- (5.12)–(5.13): the truncated phase is `∑_p X_p`, the sum over the primes up to `B`. -/
private lemma phase_split {β : Type*} [Fintype β] (q : ℝ) (H B : ℕ) (c : β → ℝ) (d : ℕ → ℝ)
    (u : ℕ → β → ℕ) (hu : ∀ k < H, ∀ b, 0 < u k b ∧ u k b ≤ B) :
    q * ∑ k ∈ Finset.range H, (∑ b, c b * (ArithmeticFunction.cardDistinctFactors (u k b) : ℝ)) / d k
      = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime,
          q * ∑ k ∈ Finset.range H, (∑ b, c b * (if p ∣ u k b then (1 : ℝ) else 0)) / d k := by
  rw [← Finset.mul_sum, Finset.sum_comm]
  congr 1
  refine Finset.sum_congr rfl fun k hk => ?_
  rw [← Finset.sum_div, Finset.sum_comm]
  congr 1
  refine Finset.sum_congr rfl fun b _ => ?_
  have h := hu k (Finset.mem_range.mp hk) b
  rw [← Finset.mul_sum, ← omega_eq_sum_primes_le h.1 h.2]

private lemma avg_abs_add3 {ι : Type*} (s : Finset ι) (a b c : ι → ℝ) :
    (s.card : ℝ)⁻¹ * ∑ i ∈ s, |a i + b i + c i|
      ≤ (s.card : ℝ)⁻¹ * ∑ i ∈ s, |a i| + (s.card : ℝ)⁻¹ * ∑ i ∈ s, |b i|
        + (s.card : ℝ)⁻¹ * ∑ i ∈ s, |c i| := by
  rw [← mul_add, ← mul_add, ← Finset.sum_add_distrib, ← Finset.sum_add_distrib]
  exact mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun i _ => abs_add_three _ _ _)
    (inv_nonneg.mpr (Nat.cast_nonneg _))

/-- `E|f| ≤ (E f²)^{1/2}` (Cauchy–Schwarz), used for `κ₃` as defined in (5.14). -/
private lemma avg_abs_le_sqrt {ι : Type*} (s : Finset ι) (f : ι → ℝ) :
    (s.card : ℝ)⁻¹ * ∑ i ∈ s, |f i| ≤ Real.sqrt ((s.card : ℝ)⁻¹ * ∑ i ∈ s, f i ^ 2) := by
  have h0 : 0 ≤ (s.card : ℝ)⁻¹ * ∑ i ∈ s, |f i| :=
    mul_nonneg (inv_nonneg.mpr (Nat.cast_nonneg _)) (Finset.sum_nonneg fun i _ => abs_nonneg _)
  rw [← Real.sqrt_sq h0]
  apply Real.sqrt_le_sqrt
  have hcs := sq_sum_le_card_mul_sum_sq (s := s) (f := fun i => |f i|)
  simp only [sq_abs] at hcs
  rcases Nat.eq_zero_or_pos s.card with h | h
  · simp [h]
  · have hN : (0 : ℝ) < s.card := by exact_mod_cast h
    rw [mul_pow, inv_pow]
    calc ((s.card : ℝ) ^ 2)⁻¹ * (∑ i ∈ s, |f i|) ^ 2
        ≤ ((s.card : ℝ) ^ 2)⁻¹ * ((s.card : ℝ) * ∑ i ∈ s, f i ^ 2) :=
          mul_le_mul_of_nonneg_left (by exact_mod_cast hcs) (by positivity)
      _ = (s.card : ℝ)⁻¹ * ∑ i ∈ s, f i ^ 2 := by field_simp

/-- `|(-1)^{|ε|}| = 1`. -/
private lemma abs_sgn_le_one {K : ℕ} (ε : Fin K → Bool) :
    |∏ i, (if ε i then (-1 : ℝ) else 1)| ≤ 1 := by
  rw [Finset.abs_prod]
  refine le_of_eq (Finset.prod_eq_one fun i _ => ?_)
  split_ifs <;> simp

/-- `|q ∑_ε (-1)^{|ε|} ∑_k g| ≤ q ∑_ε ∑_k g` for `g ≥ 0`: the `δ_p` terms of (5.8) against `κ₁`. -/
private lemma abs_sgn_tsum_le {K : ℕ} (q : ℝ) (hq : 0 ≤ q) (sgn : (Fin K → Bool) → ℝ)
    (hsgn : ∀ ε, |sgn ε| ≤ 1) (g : (Fin K → Bool) → ℕ → ℝ) (hg : ∀ ε k, 0 ≤ g ε k) :
    |q * ∑ ε, sgn ε * ∑' k, g ε k| ≤ q * ∑ ε, ∑' k, g ε k := by
  rw [abs_mul, abs_of_nonneg hq]
  refine mul_le_mul_of_nonneg_left ((Finset.abs_sum_le_sum_abs _ _).trans
    (Finset.sum_le_sum fun ε _ => ?_)) hq
  rw [abs_mul, abs_of_nonneg (tsum_nonneg (hg ε))]
  exact mul_le_of_le_one_left (tsum_nonneg (hg ε)) (hsgn ε)

/-- Truncation (5.11): `|∑ T - ∑_{k<H} T| ≤ ∑_{k} G(k+H)` when `|T| ≤ G` and `G` is summable. -/
private lemma tail_abs_le (T G : ℕ → ℝ) (hT : ∀ k, |T k| ≤ G k) (hG : Summable G) (H : ℕ) :
    |∑' k, T k - ∑ k ∈ Finset.range H, T k| ≤ ∑' k, G (k + H) := by
  have hTs : Summable T :=
    Summable.of_norm_bounded hG (fun k => by rw [Real.norm_eq_abs]; exact hT k)
  rw [← hTs.sum_add_tsum_nat_add H, add_sub_cancel_left]
  have hTH : Summable fun k => T (k + H) := (summable_nat_add_iff H).mpr hTs
  have hGH : Summable fun k => G (k + H) := (summable_nat_add_iff H).mpr hG
  rw [abs_le]
  constructor
  · rw [← tsum_neg]
    exact Summable.tsum_le_tsum (fun k => neg_le_of_abs_le (hT (k + H))) hGH.neg hTH
  · exact Summable.tsum_le_tsum (fun k => le_of_abs_le (hT (k + H))) hTH hGH

private lemma abs_sgn_sum_div_le {β : Type*} [Fintype β] (sgn : β → ℝ) (hs : ∀ b, |sgn b| ≤ 1)
    (w : β → ℝ) (hw : ∀ b, 0 ≤ w b) (d : ℝ) (hd : 0 < d) :
    |(∑ b, sgn b * w b) / d| ≤ (∑ b, w b) / d := by
  rw [abs_div, abs_of_pos hd]
  exact div_le_div_of_nonneg_right ((Finset.abs_sum_le_sum_abs _ _).trans
    (Finset.sum_le_sum fun b _ => by
      rw [abs_mul, abs_of_nonneg (hw b)]; exact mul_le_of_le_one_left (hw b) (hs b))) hd.le

/-- Erdős 257 for `A` the set of primes: `∑_p 1/(2^p - 1) = ∑_{n ≥ 1} ω(n)/2^n` is irrational
(this is also Erdős problem 69). Known: T. Tao and J. Teräväinen, *Quantitative correlations and
some problems on prime factors of consecutive integers*, arXiv:2512.01739 (v2, 2026), Theorem 1.3
(§1.3); earlier conditional on a uniform prime tuples conjecture (Pratt 2024). The paper only
sketches the analogous case of the prime powers ("we leave the details … to the interested
reader"), so that case is not stated here. -/
theorem Opn.erdos_257_primes :
    Irrational (∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1)) := by
  -- approach: Tao–Teräväinen §5: rationality forces (5.8) on every prime cube; averaging e(phase) over n and the variance bound (5.20) contradict Theorem 5.1
  -- licence: CC-BY-4.0
  -- label: Tao–Teräväinen Theorem 5.1 (technical reduction)
  -- claim: $\forall q \ge 1\ \exists c_0 > 0\ \forall \delta > 0:\ \text{a prime cube } p_\varepsilon = p_0 + \textstyle\sum_k \varepsilon_k v_k,
  -- \text{ a finite set of } n \text{ obeying (5.5)--(5.6), a split } S_0 \sqcup S_1 \sqcup S_2 \text{ of the primes} \le B
  -- \text{ with } X_p \text{ constant on } S_0,\ |X_p| \le \delta \text{ on } S_1,\ M \text{ even},\ \kappa_1, \dots, \kappa_5 \le \delta,\ \sum_{p \in S_1} \operatorname{Var} X_p \ge c_0$
  -- Theorem 5.1 of arXiv:2512.01739v2 (p. 49) for n uniform on the finite set Ns, where
  -- X_p(n) = q sum_{1<=h<=H} sum_eps (-1)^|eps| 1[p | n + r_{eps,h+K}] / 2^(h+K) is (5.13), and
  -- kappa_1, kappa_2, kappa_3 are (5.10), (5.11), (5.14) as printed (expectation and sums swapped).
  -- kappa_4, kappa_5 are (5.16), (5.17) as printed, with X'_p independent copies (n uniform on
  -- Ns^{S_1}) and e = exp 1 in (5.17). One part is taken from the paper's proof rather than its
  -- statement: (5.19), |X_p| small on S_1 (shown in section 5.6 from (5.36)). It does not mention the sum or
  -- rationality: it is about prime factors of shifted integers, and in the paper its proof rests on
  -- the paper's Theorem 3.1 and on Lemma 5.2.
  have h1 : ∀ q : ℕ, 0 < q → ∃ c0 : ℝ, 0 < c0 ∧ ∀ δ : ℝ, 0 < δ →
      ∃ (K : ℕ) (p0 : ℤ) (v : Fin K → ℤ) (P : (Fin K → Bool) → ℕ) (H B : ℕ) (Ns : Finset ℤ)
        (m : ℤ → (Fin K → Bool) → ℕ) (S1 S2 : Finset ℕ) (sgn : (Fin K → Bool) → ℝ)
        (r : (Fin K → Bool) → ℕ → ℤ) (X : ℕ → ℤ → ℝ) (M : ℕ),
        (∀ ε, sgn ε = ∏ i, if ε i then (-1 : ℝ) else 1) ∧
        (∀ ε (h : ℕ), r ε h = p0 * h + ∑ k : Fin K, ((h : ℤ) - (k.1 + 1)) * (if ε k then v k else 0)) ∧
        (∀ p n, X p n = (q : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (if p ∣ (n + r ε (k + K + 1)).toNat then (1 : ℝ) else 0)) / 2 ^ (k + K + 1)) ∧
        Even M ∧
        (∀ ε, (P ε : ℤ) = p0 + ∑ k : Fin K, (if ε k then v k else 0)) ∧ (∀ ε, (P ε).Prime) ∧
        Ns.Nonempty ∧
        (∀ n ∈ Ns, ∀ ε, n - ∑ k : Fin K, ((k.1 : ℤ) + 1) * (if ε k then v k else 0) = P ε * m n ε) ∧
        (∀ n ∈ Ns, ∀ ε, ∀ k < H, 0 < n + r ε (k + K + 1) ∧ n + r ε (k + K + 1) ≤ B) ∧
        S1 ⊆ (Finset.range (B + 1)).filter Nat.Prime ∧ S2 ⊆ (Finset.range (B + 1)).filter Nat.Prime ∧
        Disjoint S1 S2 ∧
        (∀ p ∈ (Finset.range (B + 1)).filter Nat.Prime, p ∉ S1 → p ∉ S2 →
          ∀ n ∈ Ns, ∀ n' ∈ Ns, X p n = X p n') ∧
        (∀ p ∈ S1, ∀ n ∈ Ns, |X p n| ≤ δ) ∧
        (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) ≤ δ ∧
        (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' k : ℕ, (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
            / 2 ^ (k + H + K + 1) ≤ δ ∧
        Real.sqrt ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S2, X p n) ^ 2) ≤ δ ∧
        ∑ j ∈ Finset.range (M + 1), (2 * Real.pi) ^ j / j.factorial
          * |(Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (∑ p ∈ S1, X p n) ^ j
            - ((Fintype.piFinset (fun _ : S1 => Ns)).card : ℝ)⁻¹
              * ∑ ω ∈ Fintype.piFinset (fun _ : S1 => Ns), (∑ p : S1, X p (ω p)) ^ j| ≤ δ ∧
        (∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, Real.exp (2 * Real.pi * Real.exp 1 * X p n)
            + ∏ p ∈ S1, (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns,
              Real.exp (-(2 * Real.pi * Real.exp 1) * X p n)) / Real.exp 1 ^ M ≤ δ ∧
        c0 ≤ ∑ p ∈ S1, ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, X p n ^ 2
          - ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, X p n) ^ 2) := sorry
  have htay := taylor_all
  rintro ⟨r0, hr0⟩
  have hq : 0 < r0.den := r0.den_pos
  have hsum : ∑' p : {p : ℕ | p.Prime}, (1 : ℝ) / (2 ^ p.1 - 1) = (r0.num : ℝ) / (r0.den : ℕ) := by
    rw [← hr0, Rat.cast_def]
  obtain ⟨c0, hc0, hall⟩ := h1 r0.den hq
  have hex : Real.exp (-8 * c0) < 1 := by
    have := Real.exp_lt_exp.mpr (show -8 * c0 < 0 by linarith)
    rwa [Real.exp_zero] at this
  have hδ : 0 < min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := lt_min (by norm_num) (by linarith)
  have hδ1 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) ≤ 1 / 4 := min_le_left _ _
  have hδ2 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) ≤ (1 - Real.exp (-8 * c0)) / 64 :=
    min_le_right _ _
  obtain ⟨K, p0, v, P, H, B, Ns, m, S1, S2, sgn, r, X, M, hsgn, hr, hX, hMeven, hP, hpr, hNs, hm, hrange,
    hS1, hS2, hdisj, hS0, hXs, hk1, hk2, hk3, hk4, hk5, hvar⟩ :=
    hall (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) (div_pos hδ (by positivity))
  have hq1 : (1 : ℝ) ≤ r0.den := by exact_mod_cast hq
  have hd1 : min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den
      ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := div_le_self hδ.le hq1
  have hd2 : (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den)
      = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) :=
    mul_div_cancel₀ _ (by positivity)
  have hsgn' : cubeSgn = sgn := funext fun ε => by rw [hsgn ε]; rfl
  have hr' : cubeShift p0 v = r := funext fun ε => funext fun h => by rw [hr ε h]; rfl
  obtain ⟨n0, hn0⟩ := hNs
  have key := assembly_core Ns ⟨n0, hn0⟩ S1 X
    (fun n => (r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)
      + ((r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
          - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1))
      + ∑ p ∈ S2, X p n)
    (∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n0)
    (4 * min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64)) c0 ?_
    (fun p hp n hn => ((hXs p hp n hn).trans hd1).trans hδ1) ?_ ((decor_bound Ns S1 X M hMeven htay).trans (by linarith)) hvar
  · linarith
  · intro n hn
    obtain ⟨z, hz⟩ := rational_imp_5_8 r0.num r0.den hq hsum p0 v P hP hpr n (m n) (hm n hn)
    rw [hsgn', hr'] at hz
    refine ⟨z, ?_⟩
    have hu : ∀ k < H, ∀ ε : Fin K → Bool,
        0 < (n + r ε (k + K + 1)).toNat ∧ (n + r ε (k + K + 1)).toNat ≤ B := by
      intro k hk ε
      have h := hrange n hn ε k hk
      constructor <;> omega
    have hsplit := phase_split (r0.den : ℝ) H B sgn (fun k => (2 : ℝ) ^ (k + K + 1))
      (fun k ε => (n + r ε (k + K + 1)).toNat) hu
    beta_reduce at hsplit
    have hPB : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, X p n
        = ∑ p ∈ S1, X p n + ∑ p ∈ S2, X p n
          + ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n := by
      rw [← Finset.sum_union hdisj]
      exact (Finset.sum_sdiff (Finset.union_subset hS1 hS2)).symm.trans (add_comm _ _)
    have hC : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n
        = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime \ (S1 ∪ S2), X p n0 := by
      refine Finset.sum_congr rfl fun p hp => ?_
      have hp' := Finset.mem_sdiff.mp hp
      exact hS0 p hp'.1 (fun h => hp'.2 (Finset.mem_union_left _ h))
        (fun h => hp'.2 (Finset.mem_union_right _ h)) n hn n0 hn0
    have hXsum : ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime,
        (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (if p ∣ (n + r ε (k + K + 1)).toNat then (1 : ℝ) else 0)) / 2 ^ (k + K + 1)
        = ∑ p ∈ (Finset.range (B + 1)).filter Nat.Prime, X p n :=
      Finset.sum_congr rfl fun p _ => (hX p n).symm
    linarith
  · refine (avg_abs_add3 Ns _ _ _).trans ?_
    have e1 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
        ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := by
      have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε
      calc (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑ ε : Fin K → Bool, sgn ε * ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)|
          ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑ ε : Fin K → Bool, ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1) :=
            mul_le_mul_of_nonneg_left (Finset.sum_le_sum fun n _ =>
              abs_sgn_tsum_le _ (Nat.cast_nonneg _) sgn hs1 _ fun ε k =>
                div_nonneg (by split_ifs <;> norm_num) (by positivity))
              (inv_nonneg.mpr (Nat.cast_nonneg _))
        _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑ ε : Fin K → Bool, ∑' k : ℕ,
            (if P ε ^ 2 ∣ P ε * m n ε + P ε * (k + 1) then (1 : ℝ) else 0) / 2 ^ (k + 1)) := by
            rw [← Finset.mul_sum]; ring
        _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
            mul_le_mul_of_nonneg_left hk1 (Nat.cast_nonneg _)
        _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2
    have hs1 : ∀ ε, |sgn ε| ≤ 1 := fun ε => by rw [hsgn ε]; exact abs_sgn_le_one ε
    have e2 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
          - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
        ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := by
      have hper : ∀ n ∈ Ns, |(r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)
          - (r0.den : ℝ) * ∑ k ∈ Finset.range H, (∑ ε : Fin K → Bool, sgn ε *
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ)) / 2 ^ (k + K + 1)|
          ≤ (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
            / 2 ^ (k + H + K + 1) := by
        intro n hn
        have hsh : ∀ ε (h : ℕ), (n + r ε h).toNat = P ε * m n ε + P ε * h := by
          intro ε h
          have : n + r ε h = ((P ε * m n ε + P ε * h : ℕ) : ℤ) := by
            rw [← hr', cubeShift_eq, ← hP ε]; push_cast; linear_combination hm n hn ε
          rw [this, Int.toNat_natCast]
        have hG : Summable fun k : ℕ => (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + K + 1)).toNat : ℝ))
            / 2 ^ (k + K + 1) := by
          simp_rw [Finset.sum_div]
          refine summable_sum fun ε _ => ?_
          have hb := omega_bound_summable (fun k => P ε * m n ε + P ε * (k + K + 1))
            (P ε * m n ε + P ε * (K + 1)) (P ε) (fun k => le_of_eq (by ring))
          refine Summable.of_nonneg_of_le (fun k => by positivity) (fun k => ?_) hb
          rw [hsh ε]
          exact div_le_div_of_nonneg_left (by positivity) (by positivity)
            (pow_le_pow_right₀ (by norm_num) (by omega))
        rw [← mul_sub, abs_mul, abs_of_nonneg (Nat.cast_nonneg _)]
        exact mul_le_mul_of_nonneg_left (tail_abs_le _ _ (fun k => abs_sgn_sum_div_le sgn hs1 _
          (fun ε => Nat.cast_nonneg _) _ (by positivity)) hG H) (Nat.cast_nonneg _)
      calc _ ≤ (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, (r0.den : ℝ) * ∑' k : ℕ, (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
            / 2 ^ (k + H + K + 1) :=
            mul_le_mul_of_nonneg_left (Finset.sum_le_sum hper) (inv_nonneg.mpr (Nat.cast_nonneg _))
        _ = (r0.den : ℝ) * ((Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, ∑' k : ℕ, (∑ ε : Fin K → Bool,
            (ArithmeticFunction.cardDistinctFactors (n + r ε (k + H + K + 1)).toNat : ℝ))
            / 2 ^ (k + H + K + 1)) := by rw [← Finset.mul_sum]; ring
        _ ≤ (r0.den : ℝ) * (min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) / r0.den) :=
            mul_le_mul_of_nonneg_left hk2 (Nat.cast_nonneg _)
        _ = min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) := hd2
    have e3 : (Ns.card : ℝ)⁻¹ * ∑ n ∈ Ns, |∑ p ∈ S2, X p n|
        ≤ min (1 / 4 : ℝ) ((1 - Real.exp (-8 * c0)) / 64) :=
      ((avg_abs_le_sqrt Ns fun n => ∑ p ∈ S2, X p n).trans hk3).trans hd1
    linarith
Annexes (1), in full

Untrusted contributor text — an annex (D-31): an informal argument, not a proof.

Untrusted: annex (D-31), by t1022-l, drafted with Claude Opus 5.5 (Claude Code), agent t1022-q (author), filed under t1022-l's token on the lead's instruction (daily token cap); from the paper, read, 2026-10-22T15:13:52Z, licence CC-BY-4.0. Rendered from targets/erdos-257/nodes/variant-6b364d2b/annex/d65df7a2fcbd6a58cd93667ffea1a5176dac2249ee16b9c4943fc37c64a8eb1e.md.

Primes case (Erdős 69 = Erdős 257 for A = primes): a route map of Tao–Teräväinen's proof for a Lean partial

Source: T. Tao and J. Teräväinen, Quantitative correlations and some problems on prime factors of consecutive integers, arXiv:2512.01739v2, Theorem 1.3 (§1.3, p. 4), proved in §5 (pp. 44–56), using Theorem 3.1 (p. 24, proved in §3). Equation and page numbers below are the paper's. Written 2026-10-22 from the paper itself, read in full for §1.3, §3's statement and §5. Nothing here is new mathematics. The point is to say which steps are elementary, which are standard analytic number theory, and which are new, and for each, whether current Mathlib has its inputs. Mathlib names were checked with the network's fast check (#check, Lean 4.33.1 and the Mathlib nearest the erdos-257 pin), not from memory.

Notation (paper, §1.6 and (1.1)): ω(n) = Σ_p 1_{p|n}; log_2 x = log log x, log_3 x = log log log x, log_4 x = log log log log x; e(t) = exp(2πit). In Lean, ω is ArithmeticFunction.cardDistinctFactors.

What §5 uses: Theorem 3.1, Lemma 5.2, the definitions (1.1), (1.19) and (1.21), Mertens' theorem, the prime number theorem (only in Chebyshev strength), the Chinese remainder theorem and Selberg's sieve. Sections 2 and 4 (Theorems 1.1, 1.2, 1.5–1.7) are not used. §5 cites Section 2 only for the names "near / far / very far shifts" and "small / medium / large primes".

The chain, in the paper's order

S1. Lambert identity, (1.7). Σ_p 1/(2^p − 1) = Σ_{n≥1} ω(n)/2^n. It follows by expanding each term as a geometric series and swapping the sums. Elementary. Proved in Lean (a helper, not a node): lambert_identity (any A ⊆ ℕ: Σ_{a∈A} 1/(2^a−1) = Σ_m #{d | m : d ∈ A}/2^m) and lambert_primes, in the run's evidence file Q/tt_steps_S1_S4.lean (network repository, engineering/evidence/testers-2026-10-22-e257/), which also holds S2, S3 and S4's cancellation. Fast check okay, axioms propext/choice/Quot.sound only. Mathlib inputs used: summable_prod_of_nonneg, Summable.tsum_comm, Function.Injective.tsum_eq, tsum_geometric_of_lt_one, summable_pow_mul_geometric_of_norm_lt_one, Nat.card_divisors_le_self. (Mathlib's tsum_pow_div_one_sub_eq_tsum_sigma covers only A = ℕ.)

S2. Tails, (5.1). If the sum is a/q, then q Σ_{h≥1} ω(n+h)/2^h ≡ 0 (mod 1) for every n ∈ ℕ: multiply by 2^n and subtract the integer Σ_{m≤n} ω(m) 2^{n−m}. Elementary. Proved in Lean: eq_5_1 in the same file, built on agent L's tail_mul_den_int.

S3. Dilation, (5.2)–(5.3). Fix a prime p. Since ω(pm) = ω(m) + 1 − 1_{p|m}, we have, for p | n, q Σ_{h≥1} ω(n+ph)/2^h ≡ −q δ_p(n) (mod 1), where δ_p(n) := Σ_{h≥1} 1_{p²|n+ph}/2^h. Depends on S2 (applied at n/p) and Σ_h 2^{−h} = 1. Elementary. Proved in Lean: omega_mul_prime (ω(pk) + 1_{p|k} = ω(k) + 1) and eq_5_2. Inputs: Nat.primeFactors_mul, Nat.Prime.primeFactors, ArithmeticFunction.cardDistinctFactors_apply.

S4. Cube and alternating sum, (5.4)–(5.8). Take p_0 ∈ ℕ and v_1, …, v_K ∈ ℤ such that the numbers p_ε := p_0 + ε_1 v_1 + … + ε_K v_K are distinct primes for all ε ∈ {0,1}^K (5.4). Take n with n ≡ Σ_k k ε_k v_k (mod p_ε) for every ε (5.5), and with n > Σ_k k|v_k| (5.6). Apply S3 with p = p_ε at n − Σ_k k ε_k v_k. The shifts are r_{ε,h} := p_ε h − Σ_k k ε_k v_k = p_0 h + Σ_k (h−k) ε_k v_k (5.7). For 1 ≤ h ≤ K, r_{ε,h} does not depend on ε_h. So the alternating sum Σ_ε (−1)^{|ε|} ω(n + r_{ε,h}) vanishes for those h, and the result is (5.8): q Σ_{h≥1} Σ_ε (−1)^{|ε|} ω(n + r_{ε,h+K}) / 2^{h+K} ≡ −q Σ_ε (−1)^{|ε|} δ_{p_ε}(n − Σ_k k ε_k v_k) (mod 1). Elementary (CRT plus finite combinatorics). The cancellation is proved in Lean: cubeShift_eq is (5.7), and cube_cancel says that for 1 ≤ h ≤ K, Σ_ε (−1)^{|ε|} g(r_{ε,h}) = 0 for every g. Finding n is CRT: Nat.chineseRemainderOfFinset and Ideal.quotientInfRingEquivPiQuotient are present.

S5. Exponentiate, average and truncate, (5.9)–(5.11). Apply t ↦ e(t) and average over a random n obeying (5.5)–(5.6). This gives E e(q Σ_{h≥1} …) = 1 + O(κ_1), with κ_1 = Σ_ε Σ_h P(p_ε² | n + r_{ε,h}) / 2^h (5.10). Cutting at h ≤ H costs O(κ_2), with κ_2 = Σ_{h>H} Σ_ε E ω(n + r_{ε,h+K}) / 2^{h+K} (5.11). Elementary (e is 2π-Lipschitz). Mathlib: Complex.exp, Complex.norm_exp_sub_one_le.

S6. Split the primes, (5.12)–(5.15). By (1.1), the phase is Σ_p X_p, where X_p := q Σ_{1≤h≤H} Σ_ε (−1)^{|ε|} 1_{p | n + r_{ε,h+K}} / 2^{h+K} (5.13). Partition the primes as S_0 ⊎ S_1 ⊎ S_2, with X_p deterministic for p ∈ S_0. Taylor expansion and Cauchy–Schwarz then give E e(Σ_{p∈S_1} X_p) = 1 + O(κ_1 + κ_2 + κ_3), with κ_3 := (E (Σ_{p∈S_2} X_p)²)^{1/2} (5.14), (5.15). Elementary.

S7. Variance bound, (5.16)–(5.20). Compare with jointly independent copies X'_p, by Taylor expansion to an even order M: κ_4 (5.16). The bound e^t + e^{−t} ≥ t^M/M! gives κ_5 (5.17). This yields Π_{p∈S_1} E e(X_p) = 1 + O(Σ_{j≤5} κ_j). If X_p = o(1) on S_1 (5.19), then 1 − cos t ≍ t² gives Σ_{p∈S_1} Var X_p ≪ Σ_j κ_j (5.20). Elementary probability on a finite uniform space. Mathlib: ProbabilityTheory.variance, ProbabilityTheory.iIndepFun, ProbabilityTheory.IndepFun.variance_add, Real.cos_le_one_sub_mul_cos_sq (for |x| ≤ π), Real.cos_bound, Real.add_one_le_exp.

S8. Theorem 5.1 (technical reduction, p. 49). Along a sequence x → ∞, there exist K, H, M (M even), p_0, v_1, …, v_K with the p_ε distinct primes, a partition S_0, S_1, S_2 of the primes, and a random n obeying (5.5) and (5.6), such that:

  • κ_1, …, κ_5 = o(1) (5.18);
  • X_p is deterministic on S_0;
  • Σ_{p∈S_1} Var X_p ≫ 1 (5.21).

S1–S7 together with Theorem 5.1 contradict rationality. Its proof is §5.5–§5.14:

  • S8a, parameters (5.22)–(5.29). All are explicit iterated-log functions of x. As printed: K ≈ 2 log_4 x / log 2 (so 2^K ≈ (log_3 x)^2), H ≈ (log_3 x)^2, M = 2⌊log_2 x⌋, H_+ ≈ (log_2 x)^2, P = exp((log_3 x)^3), R = x^{1/(log_2 x)^2}, R_+ = x^{1/100}, with L in between. Read the floors and exponents from the PDF itself; text extraction drops the super- and subscripts. Every estimate below uses only the hierarchy (5.30).
  • S8b, Lemma 5.2 (p. 50). There exist p_0 ≥ 1 and v_1, …, v_K such that all p_ε are distinct primes in [P/2, P], and all r_{ε,h+K} (ε ∈ {0,1}^K, 1 ≤ h ≤ H) are distinct. Proof: a Hilbert cube lemma via monotonicity of Gowers norms, ‖f‖_{U^K} ≥ ‖f‖_{U^1} ≫ 1/log P on ℤ/5Pℤ for the indicator of the primes in [P/2, P], plus a union bound. Standard additive combinatorics. Needs #{p ∈ [P/2, P]} ≫ P/log P, which is PNT or Chebyshev with constants sharper than log 4.
  • S8c, set-up (5.32)–(5.36). Define the discriminant W, the prime classes (small: p | W or p = p_ε; medium: up to R; large: R < p ≤ R_+; very large) and S_0 = small, S_1 = medium, S_2 = the rest. Take n uniform on [x/2, x] subject to (5.5) and n ≡ 0 (mod W^♭) (5.33). The progression has spacing Q ≪ P^{2^K} W^♭ (5.34), by CRT and θ(P) ≪ P. Equidistribution (5.35) holds for every u-periodic f with u coprime to Q. Also X_p = O(2^{−K}) off S_0 (5.36). Elementary, plus Chebyshev's upper bound.
  • S8d, κ_1 (§5.7). Elementary from (5.35).
  • S8e, κ_2 (§5.8). Uses ω(n) ≪ log n, at most O(1) prime factors above R_+, (5.35) and Mertens' theorem Σ_{p≤y} 1/p = log log y + O(1). Standard.
  • S8f, κ_4 (§5.9). Elementary from (5.35) and (5.36).
  • S8g, κ_5 (§5.10). Taylor expansion, (5.37), (5.38) and Mertens. Standard.
  • S8h, variance (§5.11). Var X_p ≍ 1/(2^K p), so by Mertens Σ_{S_1} Var X_p ≫ (log_2 R − log_2 W)/2^K, which gives (5.21). Standard.
  • S8i, κ_3 on large primes, (5.39) (§5.13). Near-orthogonality of X_p X_{p'} from (5.35), with no collisions of residue classes, plus Mertens. Standard.
  • S8j, κ_3 on very large primes, (5.40) (§5.14). This is where the paper's new input enters.
    • Reduce to (5.41), which is trivial, and to the decorrelation (5.42).
    • Write Σ_{R_+<p<2x} 1_{p|n} = Σ_ℓ (1 − g_ℓ(n) + O(1_{n∈E_ℓ})) (5.43). Here g_ℓ is completely multiplicative with g_ℓ(p) = 1_{p∉J_ℓ}, and the J_ℓ are L blocks of (R_+, 2x] of equal log log length. The exceptional sets E_ℓ are sparse by Mertens (5.44).
    • Then apply Theorem 3.1(i) with g_1 = g_ℓ and g_2 = g_ℓ' or 1. This requires the equidistribution hypothesis (3.1) for g_ℓ in progressions of modulus O(log^c x). That is proved by inclusion–exclusion to depth 101, plus Selberg's sieve: O(N/log N) integers ≤ 2N have no prime factor ≤ R_+. Condition (3.2) holds because J_ℓ ⊂ (R_+, 2x]. The exceptional set of Theorem 3.1 is why x runs along a subsequence.
    • New input: Theorem 3.1. The rest is standard.

S9. Theorem 3.1 (quantitative correlation estimate, p. 24; proof in §3). Let g_1, g_2 be 1-bounded multiplicative functions, 1 ≤ L ≤ log X, and in the equidistributed case (i) assume g_1 is real with Σ_{N<n≤2N, n≡a (q)} g_1(n) = (N/q) δ_N + O(N L^{−1}) (3.1), and (3.2). Then there is a set E ⊂ [√X, X] of logarithmic density ≪ L^{−c} such that (W/N) Σ_{N<n≤2N} (g_1(n+h_1) − δ_N) g_2(n+h_2) 1_{n≡b (W)} ≪ L^{−c} for all N ∈ [√X, X] \ E, all W ≤ L^c, and all distinct h_1, h_2 and b of size O(L^c) (3.4). This extends Pilatte's quantitative Chowla bound (arXiv ref. [42] of the paper) and uses Pilatte's decoupling inequality (Theorem 3.3). This is the paper's new analytic content.

Mathlib (Lean 4.33.1), checked by #check on the fast checker

Present:

  • ArithmeticFunction.cardDistinctFactors and cardDistinctFactors_mul
  • Nat.primeFactors_mul
  • Nat.chineseRemainderOfFinset
  • Nat.bertrand (one prime in (n, 2n])
  • Chebyshev.theta_le_log4_mul_x (θ(x) ≤ x log 4), Chebyshev.theta_ge, Chebyshev.psi_le, Chebyshev.psi_ge
  • Nat.infinite_setOf_prime_and_eq_mod (Dirichlet)
  • Nat.Primes.not_summable_one_div (Σ 1/p diverges, nothing quantitative)
  • Nat.smoothNumbersUpTo_card_le, Nat.roughNumbersUpTo_card_le
  • BoundingSieve.siftedSum_le_mainSum_errSum_of_upperMoebius (an upper-bound sieve framework)
  • ThreeAPFree, roth_3ap_theorem
  • ProbabilityTheory.variance, ProbabilityTheory.iIndepFun, PMF.uniformOfFinset
  • Real.cos_le_one_sub_mul_cos_sq, Real.cos_bound, Complex.norm_exp_sub_one_le

Absent (unknown identifier):

  • any Gowers norm (GowersNorm, gowersNorm)
  • a Selberg-sieve bound by name (SelbergSieve.selberg_bound_simple)
  • a quantitative Mertens theorem
  • any asymptotic or proportion count of primes in [P/2, P]
  • Nat.Coprime.card_primeFactors_mul

The Chebyshev bounds present do not give a positive proportion of primes in [P/2, P]: θ(P) − θ(P/2) ≥ P log 2 − (P/2) log 4 − error = −error. So S8b needs either sharper Chebyshev constants or PNT.

Status by step:

  • S1–S7: inputs present. S1, S2, S3 and S4's cancellation are proved (helpers, fast-checked, standard axioms only).
  • S8a, S8c, S8d, S8f: elementary, inputs present (θ(P) ≪ P is present).
  • S8b: needs Gowers norm monotonicity (elementary, absent) and a prime proportion in [P/2, P] (absent).
  • S8e, S8g, S8h, S8i: need Mertens' second theorem (absent).
  • S8j: needs Theorem 3.1 (absent, far) and a Selberg-sieve upper bound for rough numbers. Mathlib's BoundingSieve framework is a start, but no stated bound was found.
  • S9: absent and far. It is a research-level formalisation.
Which steps could be holes of an honest partial

A hole must be honestly smaller than the node and true. On that bar:

  • Good holes: self-contained published theorems, each strictly weaker than the node and reusable.
    • (a) Lemma 5.2: a Hilbert cube of distinct primes in [P/2, P] with distinct shifts r_{ε,h+K}, for K, H as functions of P. A qualitative form also works: for all K, H there exist p_0, v with the p_ε distinct primes and the r_{ε,h+K} distinct.
    • (b) Mertens' second theorem.
    • (c) #{p ∈ [P/2, P]} ≫ P/log P.
    • (d) Theorem 3.1(i), stated with logarithmic density written out as an integral.
    • (e) The Selberg upper bound for integers ≤ 2N free of prime factors ≤ R_+.
  • The catch is the assembly. A partial whose holes are only (a)–(e) must carry all of S3–S8 in Lean: the averaging over a CRT progression, every κ_j bound, and o(1) bookkeeping along x → ∞ under the hierarchy (5.30). That is roughly ten pages of estimates, far beyond one session.
  • A smaller honest partial. Prove S1–S7 and leave Theorem 5.1 as the single hole, stated with explicit quantifiers: there is c > 0 such that for every q ≥ 1 and ε > 0 there are parameters and a finite set of n (the random variable is uniform on it) with κ_1 + … + κ_5 ≤ ε, |X_p| ≤ ε on S_1, X_p constant on S_0, and Σ_{S_1} Var X_p ≥ c. The hole no longer mentions the sum, rationality or tails. It is a statement about ω on consecutive shifts, and it is exactly the paper's analytic content, so it is smaller in the sense of D-12, not easier. The assembly (S3–S7 with explicit constants) is perhaps 400–800 lines of Lean. The constant in (5.20) must be made explicit to state the hole honestly, so the hole has to be written together with the assembly, not before it.
  • Not a hole. "For every b there is N with b·τ_N ∉ ℤ", where τ_N is the binary tail of ω. It is the node restated after S1–S2, and the gate's offload rule would rightly treat it as no progress (see postmortem #894).

The next prover's cheapest real progress, in order:

  1. Assemble (5.8) from eq_5_2 and cube_cancel (drafted, not yet checked), then S5–S7 with explicit constants.
  2. Mertens' second theorem and a prime proportion in [P/2, P]. These are library work, valuable beyond this node, and each could be a related variant.
  3. Lemma 5.2.
  4. Then the S1–S7 partial with Theorem 5.1 as its hole.
Acknowledged findings (1)

Untrusted contributor text — the prover’s justification for a finding.

Untrusted: acknowledgment of a div-zero finding at 1 / (2 ^ ↑p - 1)

Real division; the divisor 2^n - 1 is zero only at n = 0, where Lean's junk value makes the term 0, so the sum is the sum over the positive elements, exactly as in the root's acknowledged division.

How to read this page

No explainer yet. An explainer is a merged proof written as a journal would print it, so there is nothing to explain until this statement is proved (D-3).

How to read a statement’s page →