Lean / janjarosz05/0kwas / Erdos1094Count.lean
Lean readerKwas/Erdos1094Count.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports Kwas.Erdos1094Gap2, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
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' := byintro heqhave hq1 : n / p' * p' ≤ n := Nat.div_mul_le_self n p'have hq2 : n < (n / p + 1) * p := byhave := Nat.lt_succ_iff.2 (le_refl (n / p))exact (Nat.div_lt_iff_lt_mul hp.pos).1 thisrw [heq] at hq2have hbad : n / p' * p' + k ≤ n + p' := byby_contra hpush Not at hexact exc_no_window (N := n / p') he hp' (by omega) (by omega) hq1 hgeneralize n / p' = q at hq1 hq2 hbadobtain ⟨t, rfl⟩ : ∃ t, p' = p + 1 + t := ⟨p' - p - 1, by omega⟩have e1 : q * (p + 1 + t) = q * p + q + q * t := by ringhave e2 : (q + 1) * p = q * p + p := by ringhave he' : q + 1 + (k + 1) ≤ 2 * B := by omegahave 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 ringhave e4 : 2 * B * B = B * (2 * B) := by ringhave hg : (q + 1) * B ≤ n := by omegahave 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 ofprimes 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 := byhave hk : 0 < k := he.1have 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 hpsimp only [Finset.coe_filter, Finset.mem_Ioc, Set.mem_ofPred_eq] at hpobtain ⟨⟨h1, h2⟩, hpr⟩ := hphave hp0 : 0 < p := hpr.possimp 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 phave 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 ringomega· intro p hp p' hp' heqsimp only [Finset.coe_filter, Finset.mem_Ioc, Set.mem_ofPred_eq] at hp hp'obtain ⟨⟨h1, h2⟩, hpr⟩ := hpobtain ⟨⟨h1', h2'⟩, hpr'⟩ := hp'have heq' : n / p = n / p' := heqby_contra hnercases 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 morethan `⌊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 := byintro hehave := exc_card_primes_le he hB hnomega/-- Two primes of `(k/2, B]` with the same quotient `n / p` are at distance at most `W` when `(n, k)` is anexception 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 := byhave hq1 : n / p' * p' ≤ n := Nat.div_mul_le_self n p'have hq2 : n < (n / p + 1) * p := byhave := Nat.lt_succ_iff.2 (le_refl (n / p))exact (Nat.div_lt_iff_lt_mul hp.pos).1 thisrw [heq] at hq2have hbad : n / p' * p' + k ≤ n + p' := byby_contra hpush Not at hexact exc_no_window (N := n / p') he hp' (by omega) (by omega) hq1 hhave hq0 : n / B ≤ n / p' := Nat.div_le_div_left h2' hp'.posgeneralize n / p' = q at hq1 hq2 hbad hq0generalize n / B = q0 at hW hq0by_contra hconpush Not at hconobtain ⟨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 ringhave e2 : (q + 1) * p = q * p + p := by ringhave h3 : (W + 1) * q0 ≤ (W + 1) * q := Nat.mul_le_mul_left _ hq0omega/-- **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) := byhave hk : 0 < k := he.1have 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 hShave hmem : ∀ p ∈ S, k < 2 * p ∧ p ≤ B ∧ p.Prime := byintro p hprw [hS, Finset.mem_filter, Finset.mem_Ioc] at hpexact ⟨by omega, hp.1.2, hp.2⟩have h1 : S.card ≤ (W + 1) * (S.image fun p => n / p).card := byapply Finset.card_le_mul_card_imageintro 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) := byintro x hxhave hmin := Finset.min'_le _ x hxhave hamem := Finset.min'_mem (S.filter fun p => n / p = q) hnerw [Finset.mem_Icc]refine ⟨hmin, ?_⟩rcases Nat.eq_or_lt_of_le hmin with heq | hlt· omega· rw [Finset.mem_filter] at hx hamemobtain ⟨ha1, ha2, ha3⟩ := hmem _ hamem.1obtain ⟨hx1, hx2, hx3⟩ := hmem _ hx.1exact 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]; omegahave h2 : (S.image fun p => n / p).card ≤ (Finset.Icc (n / B) (2 * n / k)).card := byapply Finset.card_le_cardintro q hqrw [Finset.mem_image] at hqobtain ⟨p, hp, rfl⟩ := hqobtain ⟨hp1, hp2, hp3⟩ := hmem p hprw [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 phave 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 ringomegacalc 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]` containsmore 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 := byintro hehave := exc_card_primes_le_mul he hB hWomega/-- **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) := byhave hk : 0 < k := he.1have h2k : 2 * k ≤ n := he.2.1have hq1 : n / p * p ≤ n := Nat.div_mul_le_self n phave hq2 : n < (n / p + 1) * p := byhave := Nat.lt_succ_iff.2 (le_refl (n / p))exact (Nat.div_lt_iff_lt_mul hp.pos).1 thishave hbad : n / p * p + k ≤ n + p := byby_contra hpush Not at hexact exc_no_window (N := n / p) he hp h1 h2 hq1 hhave hq : 2 ≤ n / p := byrw [Nat.le_div_iff_mul_le hp.pos]; omegarefine ⟨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 hqobtain ⟨r, rfl⟩ : ∃ r, q = r + 2 := ⟨q - 2, by omega⟩have e1 : (r + 2) * p = p * (r + 2 - 1) + p := byhave : r + 2 - 1 = r + 1 := by omegarw [this]; ringomega/-- **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)) := byhave hk : 0 < k := he.1have 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)) := byintro p hprw [Finset.mem_filter, Finset.mem_Ioc] at hpobtain ⟨⟨h1, h2⟩, hpr⟩ := hpobtain ⟨-, 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 phave 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 ringomegacalc ((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)) := bysimp only [Nat.card_Ioc]/-- **Counting criterion, exact form.** If `B ≤ k` and `(k/2, B]` contains more primes than there areintegers 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 := byintro hehave := exc_card_primes_le_sum he hBomegaend 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