import Mathlib /-! # Erdős problem 680: the clock rule, the eight small exceptions, and a conditional reduction `Good n` is the inner statement of `Erdos680.erdos_680.parts.i` in Formal Conjectures: `∃ k ≠ 0, (n + k).minFac > k^2 + 1`. Nothing here proves the conjecture. -/ namespace Erdos680 open Filter /-- `n` is good when some `k ≠ 0` has least prime factor of `n + k` above `k^2 + 1`. -/ def Good (n : ℕ) : Prop := ∃ k ≠ 0, (n + k).minFac > k ^ 2 + 1 /-- The clock rule: for `m ≥ 2`, `minFac m > y` iff no prime clock `p ≤ y` divides `m`. -/ theorem minFac_gt_iff {m y : ℕ} (hm : 2 ≤ m) : m.minFac > y ↔ ∀ p : ℕ, p.Prime → p ≤ y → ¬ p ∣ m := by constructor · intro h p hp hpy hdvd have := Nat.minFac_le_of_dvd hp.two_le hdvd omega · intro h by_contra hle exact h m.minFac (Nat.minFac_prime (by omega)) (by omega) (Nat.minFac_dvd m) /-- `k` works for `n` iff no clock `p ≤ k^2 + 1` shows `n ≡ -k (mod p)`. -/ theorem works_iff {n k : ℕ} (h : 2 ≤ n + k) : (n + k).minFac > k ^ 2 + 1 ↔ ∀ p : ℕ, p.Prime → p ≤ k ^ 2 + 1 → ¬ p ∣ n + k := minFac_gt_iff h /-- A `k` with `n + k ≤ k^2 + 1` never works. -/ theorem not_works_of_large {n k : ℕ} (hk : k ≠ 0) (h : n + k ≤ k ^ 2 + 1) : ¬ (n + k).minFac > k ^ 2 + 1 := by have := Nat.minFac_le (n := n + k) (by omega) omega /-- Every even `n ≥ 2` is good, with `k = 1`. -/ theorem good_of_even {n : ℕ} (hn : Even n) (h2 : 2 ≤ n) : Good n := by refine ⟨1, one_ne_zero, ?_⟩ have hodd : ¬ 2 ∣ n + 1 := by obtain ⟨m, rfl⟩ := hn omega have hp := Nat.minFac_prime (n := n + 1) (by omega) have hne : (n + 1).minFac ≠ 2 := fun h => hodd (h ▸ Nat.minFac_dvd (n + 1)) have := hp.two_le norm_num omega /-- A prime `p > n` with `(p - n)^2 + 1 < p` makes `n` good. -/ theorem good_of_prime {n p : ℕ} (hp : p.Prime) (h1 : n < p) (h2 : (p - n) ^ 2 + 1 < p) : Good n := by refine ⟨p - n, by omega, ?_⟩ have : n + (p - n) = p := by omega rw [this, hp.minFac_eq] exact h2 /-- Conditional reduction: if every large `n` is followed by a prime `p` with `(p - n)^2 + 1 < p` (prime gaps below about `√p`), then the Formal Conjectures statement of part (i) holds. -/ theorem erdos_680_of_short_gaps (H : ∀ᶠ (n : ℕ) in atTop, ∃ p : ℕ, p.Prime ∧ n < p ∧ (p - n) ^ 2 + 1 < p) : ∀ᶠ (n : ℕ) in atTop, ∃ k ≠ 0, (n + k).minFac > k ^ 2 + 1 := by filter_upwards [H] with n hn obtain ⟨p, hp, h1, h2⟩ := hn exact good_of_prime hp h1 h2 /-- If `n` is bad, every prime `q > n` is far from `n`: `q ≤ (q - n)^2 + 1`. -/ theorem prime_far_of_bad {n q : ℕ} (h : ¬ Good n) (hq : q.Prime) (hn : n < q) : q ≤ (q - n) ^ 2 + 1 := by by_contra hlt exact h (good_of_prime hq hn (by omega)) /-- **Bad `n` lie in long prime gaps.** If `n` is bad and `p ≤ n < q` with `q` prime, then `q ≤ (q - p)^2 + 1`, i.e. the gap `q - p` is at least `√(q - 1)`. -/ theorem long_gap_of_bad {n p q : ℕ} (h : ¬ Good n) (hq : q.Prime) (hpn : p ≤ n) (hn : n < q) : q ≤ (q - p) ^ 2 + 1 := by have h1 := prime_far_of_bad h hq hn have h2 : (q - n) ^ 2 ≤ (q - p) ^ 2 := Nat.pow_le_pow_left (by omega) 2 omega /-! ## Finite checks -/ /-- no `d` with `2 ≤ d ≤ y` divides `m` -/ def noDiv (m y : ℕ) : Bool := (List.range (y + 1)).all fun d => d < 2 || m % d != 0 /-- some `d` with `2 ≤ d ≤ y` divides `m` -/ def hasDiv (m y : ℕ) : Bool := (List.range (y + 1)).any fun d => 2 ≤ d && m % d == 0 /-- some `1 ≤ k ≤ K` works for `n` -/ def goodB (n K : ℕ) : Bool := (List.range (K + 1)).any fun k => k != 0 && noDiv (n + k) (k ^ 2 + 1) /-- every `1 ≤ k < B` fails for `n` -/ def badB (n B : ℕ) : Bool := (List.range B).all fun k => k == 0 || hasDiv (n + k) (k ^ 2 + 1) theorem minFac_gt_of_noDiv {m y : ℕ} (hm : 2 ≤ m) (h : noDiv m y = true) : m.minFac > y := by rw [minFac_gt_iff hm] intro p hp hpy hdvd simp only [noDiv, List.all_eq_true, List.mem_range, Bool.or_eq_true, decide_eq_true_eq, bne_iff_ne] at h rcases h p (by omega) with h | h · have := hp.two_le omega · exact h (Nat.mod_eq_zero_of_dvd hdvd) theorem minFac_le_of_hasDiv {m y : ℕ} (h : hasDiv m y = true) : m.minFac ≤ y := by simp only [hasDiv, List.any_eq_true, List.mem_range, Bool.and_eq_true, decide_eq_true_eq, beq_iff_eq] at h obtain ⟨d, hd, h2, hmod⟩ := h have := Nat.minFac_le_of_dvd h2 (Nat.dvd_of_mod_eq_zero hmod) omega theorem good_of_goodB {n K : ℕ} (hn : 1 ≤ n) (h : goodB n K = true) : Good n := by simp only [goodB, List.any_eq_true, List.mem_range, Bool.and_eq_true, bne_iff_ne] at h obtain ⟨k, -, hk, hd⟩ := h exact ⟨k, hk, minFac_gt_of_noDiv (by omega) hd⟩ theorem not_good_of_badB {n B : ℕ} (hB : n ≤ B * (B - 1)) (h : badB n B = true) : ¬ Good n := by rintro ⟨k, hk, hgt⟩ simp only [badB, List.all_eq_true, List.mem_range, Bool.or_eq_true, beq_iff_eq] at h by_cases hkB : k < B · rcases h k hkB with h | h · exact hk h · have := minFac_le_of_hasDiv h omega · refine not_works_of_large hk ?_ hgt have h1 : B * (B - 1) ≤ k * (k - 1) := Nat.mul_le_mul (by omega) (by omega) have h2 : k * (k - 1) + k = k ^ 2 := by cases k with | zero => exact absurd rfl hk | succ j => simp; ring omega /-- The eight positive integers found by the search with no admissible `k`. -/ theorem bad_1 : ¬ Good 1 := not_good_of_badB (B := 2) (by decide) (by decide +kernel) theorem bad_3 : ¬ Good 3 := not_good_of_badB (B := 3) (by decide) (by decide +kernel) theorem bad_7 : ¬ Good 7 := not_good_of_badB (B := 4) (by decide) (by decide +kernel) theorem bad_13 : ¬ Good 13 := not_good_of_badB (B := 5) (by decide) (by decide +kernel) theorem bad_23 : ¬ Good 23 := not_good_of_badB (B := 6) (by decide) (by decide +kernel) theorem bad_31 : ¬ Good 31 := not_good_of_badB (B := 7) (by decide) (by decide +kernel) theorem bad_113 : ¬ Good 113 := not_good_of_badB (B := 12) (by decide) (by decide +kernel) theorem bad_115 : ¬ Good 115 := not_good_of_badB (B := 12) (by decide) (by decide +kernel) /-- `0` is bad as well (trivially). -/ theorem bad_0 : ¬ Good 0 := not_good_of_badB (B := 1) (by decide) (by decide +kernel) def exc (n : ℕ) : Bool := n == 0 || n == 1 || n == 3 || n == 7 || n == 13 || n == 23 || n == 31 || n == 113 || n == 115 def checkBelow (N K : ℕ) : Bool := (List.range N).all fun n => exc n || goodB n K theorem good_of_checkBelow {N K n : ℕ} (h : checkBelow N K = true) (hn : n < N) (he : exc n = false) : Good n := by simp only [checkBelow, List.all_eq_true, List.mem_range, Bool.or_eq_true] at h rcases h n hn with h | h · simp [he] at h · refine good_of_goodB ?_ h rcases n with _ | n · simp [exc] at he · omega theorem check_20000 : checkBelow 20000 60 = true := by decide +kernel /-- Below 20000 the bad `n` are exactly `0, 1, 3, 7, 13, 23, 31, 113, 115`. -/ theorem good_below_20000 {n : ℕ} (hn : n < 20000) (he : exc n = false) : Good n := good_of_checkBelow check_20000 hn he /-- 681: if `n + 1` is composite then `k = 1` works, so every bad `n` for 681 has `n + 1` prime. -/ theorem good681_of_succ_composite {n : ℕ} (h2 : 2 ≤ n + 1) (_h : ¬ (n + 1).Prime) : ∃ k > 0, ¬ (n + k).Prime ∧ 2 ≤ n + k ∧ (n + k).minFac > k ^ 2 := by refine ⟨1, one_pos, _h, h2, ?_⟩ have := (Nat.minFac_prime (n := n + 1) (by omega)).two_le simp only [one_pow] omega end Erdos680 #print axioms Erdos680.works_iff #print axioms Erdos680.erdos_680_of_short_gaps #print axioms Erdos680.long_gap_of_bad #print axioms Erdos680.bad_115 #print axioms Erdos680.good_below_20000 #print axioms Erdos680.good_of_even #print axioms Erdos680.good681_of_succ_composite