import Kwas.Erdos1094Gap /-! Erdős Problem 1094: the gap criterion for every `n ≥ 2k` (continues namespace Erdos1093). `not_exc_of_gap` needs `4k ≤ n`. Here the same conclusion is proved from `2k ≤ n`, which is the whole range of the problem. -/ namespace Erdos1093 /-- **Gap criterion on the whole range `n ≥ 2k`.** Suppose every interval `(a, a + L]` with `k < 2 (a + 1)` and `a + L ≤ k` contains a prime. If `2 * k ≤ n` and `8 * n * (L + 1) ≤ k * k`, then `(n, k)` is not an exception of Erdős problem 1094. -/ theorem not_exc_of_gap_two {n k L : ℕ} (h2 : 2 * k ≤ n) (hL : 8 * n * (L + 1) ≤ k * k) (hgap : ∀ a : ℕ, k < 2 * (a + 1) → a + L ≤ k → ∃ p : ℕ, p.Prime ∧ a < p ∧ p ≤ a + L) : (n, k) ∉ Exc := by intro he have hk : 0 < k := he.1 have hM1 : (2 * n / k) * k ≤ 2 * n := Nat.div_mul_le_self _ _ have hM2 : 2 * n < (2 * n / k + 1) * k := by have := Nat.lt_succ_iff.2 (le_refl (2 * n / k)) exact (Nat.div_lt_iff_lt_mul hk).1 this have hM4 : 4 ≤ 2 * n / k := by rw [Nat.le_div_iff_mul_le hk]; omega obtain ⟨m, hm⟩ : ∃ m, 2 * n / k = m + 4 := ⟨2 * n / k - 4, by omega⟩ rw [hm] at hM1 hM2 obtain ⟨d, rfl⟩ : ∃ d, n = k + d := ⟨n - k, by omega⟩ have hNpos : 0 < m + 2 := by omega -- window ((n-k)/(m+2), n/(m+3)] have ha1 : (d / (m + 2)) * (m + 2) ≤ d := Nat.div_mul_le_self _ _ have ha2 : d < (d / (m + 2) + 1) * (m + 2) := by have := Nat.lt_succ_iff.2 (le_refl (d / (m + 2))) exact (Nat.div_lt_iff_lt_mul hNpos).1 this generalize d / (m + 2) = a at ha1 ha2 have e1 : (m + 4) * k = k * m + 4 * k := by ring have e2 : (m + 4 + 1) * k = k * m + 5 * k := by ring have hi : k * m + 2 * k ≤ 2 * d := by linarith have hii : 2 * d < k * m + 3 * k := by linarith have h2a : k < 2 * (a + 1) := by by_contra h push Not at h have h1 : 2 * (a + 1) * (m + 2) ≤ k * (m + 2) := Nat.mul_le_mul_right _ h have h3 : 2 * (a + 1) * (m + 2) = 2 * ((a + 1) * (m + 2)) := by ring have h4 : k * (m + 2) = k * m + 2 * k := by ring omega -- 4 (m + 4) (L + 1) ≤ k have hG : 4 * (m + 4) * (L + 1) ≤ k := by have h1 : 4 * (m + 4) * (L + 1) * k ≤ k * k := by calc 4 * (m + 4) * (L + 1) * k = 4 * (L + 1) * ((m + 4) * k) := by ring _ ≤ 4 * (L + 1) * (2 * (k + d)) := Nat.mul_le_mul_left _ hM1 _ = 8 * (k + d) * (L + 1) := by ring _ ≤ k * k := hL exact Nat.le_of_mul_le_mul_right h1 hk -- the window is at least L long: 2 L (m+3)(m+2) ≤ (m+1) k have hW : 2 * (L * (m + 3) * (m + 2)) ≤ (m + 1) * k := by calc 2 * (L * (m + 3) * (m + 2)) ≤ 2 * (L * (m + 3) * (m + 2)) + (2 * L * m * m + 10 * L * m + 4 * L + 4 * m * m + 20 * m + 16) := Nat.le_add_right _ _ _ = (m + 1) * (4 * (m + 4) * (L + 1)) := by ring _ ≤ (m + 1) * k := Nat.mul_le_mul_left _ hG have e3 : (m + 1) * k = k * m + k := by ring have e4 : k * (m + 2) = k * m + 2 * k := by ring have hkey : d + L * (m + 3) * (m + 2) ≤ k * (m + 2) := by linarith have hkey2 : (a + L) * (m + 3) * (m + 2) ≤ (k + d) * (m + 2) := by have h5 : a * (m + 2) * (m + 3) ≤ d * (m + 3) := Nat.mul_le_mul_right _ ha1 have e5 : (a + L) * (m + 3) * (m + 2) = a * (m + 2) * (m + 3) + L * (m + 3) * (m + 2) := by ring have e6 : d * (m + 3) = d * (m + 2) + d := by ring have e7 : (k + d) * (m + 2) = k * (m + 2) + d * (m + 2) := by ring linarith have hlen : (a + L) * (m + 3) ≤ k + d := Nat.le_of_mul_le_mul_right hkey2 hNpos have hb : a + L ≤ k := by by_contra h push Not at h have h1 : (k + 1) * (m + 3) ≤ (a + L) * (m + 3) := Nat.mul_le_mul_right _ h have h3 : (k + 1) * (m + 3) = k * m + 3 * k + m + 3 := by ring linarith obtain ⟨p, hp, hap, hpL⟩ := hgap a h2a hb refine exc_no_window (N := m + 3) he hp (by omega) (by omega) ?_ ?_ · calc (m + 3) * p ≤ (m + 3) * (a + L) := Nat.mul_le_mul_left _ hpL _ = (a + L) * (m + 3) := Nat.mul_comm _ _ _ ≤ k + d := hlen · have h1 : (a + 1) * (m + 2) ≤ p * (m + 2) := Nat.mul_le_mul_right _ hap have e : (m + 3) * p = p * (m + 2) + p := by ring omega /-- The same statement with the hypothesis phrased through consecutive primes: if every prime `q < k` is followed by a prime at distance at most `L`, then there is no exception with `2k ≤ n` and `8 n (L + 1) ≤ k²`. -/ theorem not_exc_of_prime_gap {n k L : ℕ} (h2 : 2 * k ≤ n) (hL : 8 * n * (L + 1) ≤ k * k) (hgap : ∀ q : ℕ, q.Prime → q < k → ∃ p : ℕ, p.Prime ∧ q < p ∧ p ≤ q + L) : (n, k) ∉ Exc := by intro he have hk : 0 < k := he.1 have hk16 : 16 ≤ k := by by_contra h push Not at h have h1 : 8 * (2 * k) * 1 ≤ 8 * n * (L + 1) := Nat.mul_le_mul (Nat.mul_le_mul_left 8 h2) (by omega) have h3 : k * k ≤ 15 * k := Nat.mul_le_mul_right k (by omega) omega have hL1 : 1 ≤ L := by obtain ⟨p, _, h2p, hpL⟩ := hgap 2 Nat.prime_two (by omega) omega refine not_exc_of_gap_two h2 hL ?_ he intro a ha haL have ha2 : 2 ≤ a := by omega have hq : (Nat.findGreatest Nat.Prime a).Prime := Nat.findGreatest_spec ha2 Nat.prime_two have hqa : Nat.findGreatest Nat.Prime a ≤ a := Nat.findGreatest_le a have hmax : ∀ r : ℕ, r.Prime → r ≤ a → r ≤ Nat.findGreatest Nat.Prime a := fun r hr hra => Nat.le_findGreatest hra hr generalize Nat.findGreatest Nat.Prime a = q at hq hqa hmax obtain ⟨p, hp, hqp, hpq⟩ := hgap q hq (by omega) refine ⟨p, hp, ?_, by omega⟩ by_contra h push Not at h have := hmax p hp h omega end Erdos1093 #print axioms Erdos1093.not_exc_of_gap_two #print axioms Erdos1093.not_exc_of_prime_gap