import Kwas.Erdos1094Gap2 /-! Erdős Problem 1094: a counting criterion (continues namespace Erdos1093). For an exception `(n, k)` the primes `p` in `(k/2, B]` all avoid the windows of `exc_no_window`. If `2 B² ≤ n + B (k + 1)`, two such primes never have the same quotient `n / p`, so there are at most `⌊2n/k⌋ - ⌊n/B⌋ + 1` of them. More primes than that in `(k/2, B]` exclude the exception. -/ namespace Erdos1093 /-- Two distinct primes of `(k/2, B]` have different quotients `n / p` when `(n, k)` is an exception and `2 B² ≤ n + B (k + 1)`. -/ theorem exc_div_ne {n k B p p' : ℕ} (he : (n, k) ∈ Exc) (hn : B * (2 * B) ≤ n + B * (k + 1)) (hp : p.Prime) (hp' : p'.Prime) (h1 : k < 2 * p) (hlt : p < p') (h2' : p' ≤ B) (hB : B ≤ k) : n / p ≠ n / p' := by intro heq have hq1 : n / p' * p' ≤ n := Nat.div_mul_le_self n p' have hq2 : n < (n / p + 1) * p := by have := Nat.lt_succ_iff.2 (le_refl (n / p)) exact (Nat.div_lt_iff_lt_mul hp.pos).1 this rw [heq] at hq2 have hbad : n / p' * p' + k ≤ n + p' := by by_contra h push Not at h exact exc_no_window (N := n / p') he hp' (by omega) (by omega) hq1 h generalize n / p' = q at hq1 hq2 hbad obtain ⟨t, rfl⟩ : ∃ t, p' = p + 1 + t := ⟨p' - p - 1, by omega⟩ have e1 : q * (p + 1 + t) = q * p + q + q * t := by ring have e2 : (q + 1) * p = q * p + p := by ring have he' : q + 1 + (k + 1) ≤ 2 * B := by omega have hf : (q + 1 + (k + 1)) * B ≤ 2 * B * B := Nat.mul_le_mul_right B he' have e3 : (q + 1 + (k + 1)) * B = (q + 1) * B + B * (k + 1) := by ring have e4 : 2 * B * B = B * (2 * B) := by ring have hg : (q + 1) * B ≤ n := by omega have hh : (q + 1) * p ≤ (q + 1) * B := Nat.mul_le_mul_left _ (by omega) omega /-- **Counting bound.** For an exception `(n, k)` and `B ≤ k` with `2 B² ≤ n + B (k + 1)`, the number of primes in `(k/2, B]` is at most `⌊2n/k⌋ + 1 - ⌊n/B⌋`. -/ theorem exc_card_primes_le {n k B : ℕ} (he : (n, k) ∈ Exc) (hB : B ≤ k) (hn : B * (2 * B) ≤ n + B * (k + 1)) : ((Finset.Ioc (k / 2) B).filter Nat.Prime).card ≤ 2 * n / k + 1 - n / B := by have hk : 0 < k := he.1 have hcard : (Finset.Icc (n / B) (2 * n / k)).card = 2 * n / k + 1 - n / B := Nat.card_Icc _ _ rw [← hcard] apply Finset.card_le_card_of_injOn (fun p => n / p) · intro p hp simp only [Finset.coe_filter, Finset.mem_Ioc, Set.mem_ofPred_eq] at hp obtain ⟨⟨h1, h2⟩, hpr⟩ := hp have hp0 : 0 < p := hpr.pos simp only [Finset.coe_Icc, Set.mem_Icc] constructor · exact Nat.div_le_div_left h2 hp0 · rw [Nat.le_div_iff_mul_le hk] have h3 : n / p * p ≤ n := Nat.div_mul_le_self n p have h4 : n / p * k ≤ n / p * (2 * p) := Nat.mul_le_mul_left _ (by omega) have e : n / p * (2 * p) = 2 * (n / p * p) := by ring omega · intro p hp p' hp' heq simp only [Finset.coe_filter, Finset.mem_Ioc, Set.mem_ofPred_eq] at hp hp' obtain ⟨⟨h1, h2⟩, hpr⟩ := hp obtain ⟨⟨h1', h2'⟩, hpr'⟩ := hp' have heq' : n / p = n / p' := heq by_contra hne rcases Nat.lt_or_gt_of_ne hne with hlt | hlt · exact exc_div_ne he hn hpr hpr' (by omega) hlt h2' hB heq' · exact exc_div_ne he hn hpr' hpr (by omega) hlt h2 hB heq'.symm /-- **Counting criterion.** If `B ≤ k`, `2 B² ≤ n + B (k + 1)` and the interval `(k/2, B]` contains more than `⌊2n/k⌋ + 1 - ⌊n/B⌋` primes, then `(n, k)` is not an exception of Erdős problem 1094. -/ theorem not_exc_of_count {n k B : ℕ} (hB : B ≤ k) (hn : B * (2 * B) ≤ n + B * (k + 1)) (hcount : 2 * n / k + 1 - n / B < ((Finset.Ioc (k / 2) B).filter Nat.Prime).card) : (n, k) ∉ Exc := by intro he have := exc_card_primes_le he hB hn omega /-- Two primes of `(k/2, B]` with the same quotient `n / p` are at distance at most `W` when `(n, k)` is an exception and `2 B ≤ k + 1 + (W + 1) ⌊n/B⌋`. -/ theorem exc_same_quot_close {n k B W p p' : ℕ} (he : (n, k) ∈ Exc) (hW : 2 * B ≤ k + 1 + (W + 1) * (n / B)) (hp : p.Prime) (hp' : p'.Prime) (h1 : k < 2 * p) (hlt : p < p') (h2' : p' ≤ B) (hB : B ≤ k) (heq : n / p = n / p') : p' ≤ p + W := by have hq1 : n / p' * p' ≤ n := Nat.div_mul_le_self n p' have hq2 : n < (n / p + 1) * p := by have := Nat.lt_succ_iff.2 (le_refl (n / p)) exact (Nat.div_lt_iff_lt_mul hp.pos).1 this rw [heq] at hq2 have hbad : n / p' * p' + k ≤ n + p' := by by_contra h push Not at h exact exc_no_window (N := n / p') he hp' (by omega) (by omega) hq1 h have hq0 : n / B ≤ n / p' := Nat.div_le_div_left h2' hp'.pos generalize n / p' = q at hq1 hq2 hbad hq0 generalize n / B = q0 at hW hq0 by_contra hcon push Not at hcon obtain ⟨t, rfl⟩ : ∃ t, p' = p + W + 1 + t := ⟨p' - p - W - 1, by omega⟩ have e1 : q * (p + W + 1 + t) = q * p + (W + 1) * q + q * t := by ring have e2 : (q + 1) * p = q * p + p := by ring have h3 : (W + 1) * q0 ≤ (W + 1) * q := Nat.mul_le_mul_left _ hq0 omega /-- **Counting bound, general form.** For an exception `(n, k)`, `B ≤ k` and `W` with `2 B ≤ k + 1 + (W + 1) ⌊n/B⌋`, the number of primes in `(k/2, B]` is at most `(W + 1) (⌊2n/k⌋ + 1 - ⌊n/B⌋)`. -/ theorem exc_card_primes_le_mul {n k B W : ℕ} (he : (n, k) ∈ Exc) (hB : B ≤ k) (hW : 2 * B ≤ k + 1 + (W + 1) * (n / B)) : ((Finset.Ioc (k / 2) B).filter Nat.Prime).card ≤ (W + 1) * (2 * n / k + 1 - n / B) := by have hk : 0 < k := he.1 have hcard : (Finset.Icc (n / B) (2 * n / k)).card = 2 * n / k + 1 - n / B := Nat.card_Icc _ _ rw [← hcard] set S := (Finset.Ioc (k / 2) B).filter Nat.Prime with hS have hmem : ∀ p ∈ S, k < 2 * p ∧ p ≤ B ∧ p.Prime := by intro p hp rw [hS, Finset.mem_filter, Finset.mem_Ioc] at hp exact ⟨by omega, hp.1.2, hp.2⟩ have h1 : S.card ≤ (W + 1) * (S.image fun p => n / p).card := by apply Finset.card_le_mul_card_image intro q _ rcases (S.filter fun p => n / p = q).eq_empty_or_nonempty with h0 | hne · rw [h0]; simp · have hsub : (S.filter fun p => n / p = q) ⊆ Finset.Icc ((S.filter fun p => n / p = q).min' hne) ((S.filter fun p => n / p = q).min' hne + W) := by intro x hx have hmin := Finset.min'_le _ x hx have hamem := Finset.min'_mem (S.filter fun p => n / p = q) hne rw [Finset.mem_Icc] refine ⟨hmin, ?_⟩ rcases Nat.eq_or_lt_of_le hmin with heq | hlt · omega · rw [Finset.mem_filter] at hx hamem obtain ⟨ha1, ha2, ha3⟩ := hmem _ hamem.1 obtain ⟨hx1, hx2, hx3⟩ := hmem _ hx.1 exact exc_same_quot_close he hW ha3 hx3 ha1 hlt hx2 hB (hamem.2.trans hx.2.symm) calc (S.filter fun p => n / p = q).card ≤ (Finset.Icc ((S.filter fun p => n / p = q).min' hne) ((S.filter fun p => n / p = q).min' hne + W)).card := Finset.card_le_card hsub _ = W + 1 := by rw [Nat.card_Icc]; omega have h2 : (S.image fun p => n / p).card ≤ (Finset.Icc (n / B) (2 * n / k)).card := by apply Finset.card_le_card intro q hq rw [Finset.mem_image] at hq obtain ⟨p, hp, rfl⟩ := hq obtain ⟨hp1, hp2, hp3⟩ := hmem p hp rw [Finset.mem_Icc] constructor · exact Nat.div_le_div_left hp2 hp3.pos · rw [Nat.le_div_iff_mul_le hk] have h3 : n / p * p ≤ n := Nat.div_mul_le_self n p have h4 : n / p * k ≤ n / p * (2 * p) := Nat.mul_le_mul_left _ (by omega) have e : n / p * (2 * p) = 2 * (n / p * p) := by ring omega calc S.card ≤ (W + 1) * (S.image fun p => n / p).card := h1 _ ≤ (W + 1) * (Finset.Icc (n / B) (2 * n / k)).card := Nat.mul_le_mul_left _ h2 /-- **Counting criterion, general form.** If `B ≤ k`, `2 B ≤ k + 1 + (W + 1) ⌊n/B⌋` and `(k/2, B]` contains more than `(W + 1) (⌊2n/k⌋ + 1 - ⌊n/B⌋)` primes, then `(n, k)` is not an exception of Erdős problem 1094. -/ theorem not_exc_of_count_mul {n k B W : ℕ} (hB : B ≤ k) (hW : 2 * B ≤ k + 1 + (W + 1) * (n / B)) (hcount : (W + 1) * (2 * n / k + 1 - n / B) < ((Finset.Ioc (k / 2) B).filter Nat.Prime).card) : (n, k) ∉ Exc := by intro he have := exc_card_primes_le_mul he hB hW omega /-- **Exact form.** For an exception `(n, k)` every prime `p` of `(k/2, k]` lies in the interval `(n/(q+1), (n-k)/(q-1)]` with `q = ⌊n/p⌋` (the complement of the windows of `exc_no_window`). -/ theorem exc_prime_mem_bad {n k p : ℕ} (he : (n, k) ∈ Exc) (hp : p.Prime) (h1 : k < 2 * p) (h2 : p ≤ k) : 2 ≤ n / p ∧ n / (n / p + 1) < p ∧ p ≤ (n - k) / (n / p - 1) := by have hk : 0 < k := he.1 have h2k : 2 * k ≤ n := he.2.1 have hq1 : n / p * p ≤ n := Nat.div_mul_le_self n p have hq2 : n < (n / p + 1) * p := by have := Nat.lt_succ_iff.2 (le_refl (n / p)) exact (Nat.div_lt_iff_lt_mul hp.pos).1 this have hbad : n / p * p + k ≤ n + p := by by_contra h push Not at h exact exc_no_window (N := n / p) he hp h1 h2 hq1 h have hq : 2 ≤ n / p := by rw [Nat.le_div_iff_mul_le hp.pos]; omega refine ⟨hq, ?_, ?_⟩ · rw [Nat.div_lt_iff_lt_mul (by omega)] calc n < (n / p + 1) * p := hq2 _ = p * (n / p + 1) := Nat.mul_comm _ _ · rw [Nat.le_div_iff_mul_le (by omega)] generalize n / p = q at hq1 hq2 hbad hq obtain ⟨r, rfl⟩ : ∃ r, q = r + 2 := ⟨q - 2, by omega⟩ have e1 : (r + 2) * p = p * (r + 2 - 1) + p := by have : r + 2 - 1 = r + 1 := by omega rw [this]; ring omega /-- **Counting bound, exact form.** For an exception `(n, k)` and `B ≤ k`, the number of primes in `(k/2, B]` is at most the number of integers in the intervals `(n/(q+1), (n-k)/(q-1)]`, `⌊n/B⌋ ≤ q ≤ ⌊2n/k⌋`. -/ theorem exc_card_primes_le_sum {n k B : ℕ} (he : (n, k) ∈ Exc) (hB : B ≤ k) : ((Finset.Ioc (k / 2) B).filter Nat.Prime).card ≤ ∑ q ∈ Finset.Icc (n / B) (2 * n / k), ((n - k) / (q - 1) - n / (q + 1)) := by have hk : 0 < k := he.1 have hsub : (Finset.Ioc (k / 2) B).filter Nat.Prime ⊆ (Finset.Icc (n / B) (2 * n / k)).biUnion fun q => Finset.Ioc (n / (q + 1)) ((n - k) / (q - 1)) := by intro p hp rw [Finset.mem_filter, Finset.mem_Ioc] at hp obtain ⟨⟨h1, h2⟩, hpr⟩ := hp obtain ⟨-, hb1, hb2⟩ := exc_prime_mem_bad he hpr (by omega) (by omega) rw [Finset.mem_biUnion] refine ⟨n / p, ?_, Finset.mem_Ioc.2 ⟨hb1, hb2⟩⟩ rw [Finset.mem_Icc] constructor · exact Nat.div_le_div_left h2 hpr.pos · rw [Nat.le_div_iff_mul_le hk] have h3 : n / p * p ≤ n := Nat.div_mul_le_self n p have h4 : n / p * k ≤ n / p * (2 * p) := Nat.mul_le_mul_left _ (by omega) have e : n / p * (2 * p) = 2 * (n / p * p) := by ring omega calc ((Finset.Ioc (k / 2) B).filter Nat.Prime).card ≤ ((Finset.Icc (n / B) (2 * n / k)).biUnion fun q => Finset.Ioc (n / (q + 1)) ((n - k) / (q - 1))).card := Finset.card_le_card hsub _ ≤ ∑ q ∈ Finset.Icc (n / B) (2 * n / k), (Finset.Ioc (n / (q + 1)) ((n - k) / (q - 1))).card := Finset.card_biUnion_le _ = ∑ q ∈ Finset.Icc (n / B) (2 * n / k), ((n - k) / (q - 1) - n / (q + 1)) := by simp only [Nat.card_Ioc] /-- **Counting criterion, exact form.** If `B ≤ k` and `(k/2, B]` contains more primes than there are integers in the intervals `(n/(q+1), (n-k)/(q-1)]`, `⌊n/B⌋ ≤ q ≤ ⌊2n/k⌋`, then `(n, k)` is not an exception. -/ theorem not_exc_of_count_sum {n k B : ℕ} (hB : B ≤ k) (hcount : ∑ q ∈ Finset.Icc (n / B) (2 * n / k), ((n - k) / (q - 1) - n / (q + 1)) < ((Finset.Ioc (k / 2) B).filter Nat.Prime).card) : (n, k) ∉ Exc := by intro he have := exc_card_primes_le_sum he hB omega end Erdos1093 #print axioms Erdos1093.exc_card_primes_le #print axioms Erdos1093.not_exc_of_count #print axioms Erdos1093.exc_card_primes_le_mul #print axioms Erdos1093.not_exc_of_count_mul #print axioms Erdos1093.exc_prime_mem_bad #print axioms Erdos1093.exc_card_primes_le_sum #print axioms Erdos1093.not_exc_of_count_sum