import Mathlib/-!# A counterexample to a conjecture in OEIS A346149A346149: `a(n)` is the least `k > 1` such that `n ^ k + n + 1` is prime, or `0` if there isno such `k`. It was conjectured that `a(n) > 0` whenever `n ≢ 1 (mod 3)`.We show that `n = 243779134570275685188` (which is `≡ 0 (mod 3)`) is a counterexample:`n ^ k + n + 1` is composite for every `k > 1`. The proof is a covering argument.* If `k ≡ 2 (mod 3)` then `n ^ 2 + n + 1 ∣ n ^ k + n + 1`, because `n ^ 3 ≡ 1` modulo`n ^ 2 + n + 1`. For `k > 2` this is a proper divisor; for `k = 2` the number`n ^ 2 + n + 1` itself is divisible by `43`.* Otherwise `k` lies in one of eleven residue classes `k ≡ r (mod o)`, each of which comeswith a small prime `p` such that `n ^ o ≡ 1 (mod p)` and `p ∣ n ^ r + n + 1`, so that`p ∣ n ^ k + n + 1`. These classes cover all residues modulo `180` not `≡ 2 (mod 3)`.-/namespace A346149/-! ### General lemmas -//-- If `n ^ o ≡ 1 (mod p)` and `p ∣ n ^ r + n + 1`, then `p ∣ n ^ k + n + 1` for every`k = r + o * t` in the residue class of `r` modulo `o`. -/theorem dvd_of_period {n p o r : ℕ} (ho : n ^ o ≡ 1 [MOD p]) (hr : p ∣ n ^ r + n + 1) (t : ℕ) :p ∣ n ^ (r + o * t) + n + 1 := byhave h : n ^ (r + o * t) ≡ n ^ r [MOD p] := bysimpa [pow_add, pow_mul] using (ho.pow t).mul_left (n ^ r)exact (Nat.modEq_zero_iff_dvd.mp(((h.add_right n).add_right 1).trans (Nat.modEq_zero_iff_dvd.mpr hr)))/-- `n ^ 3 ≡ 1` modulo `n ^ 2 + n + 1`, since `n ^ 3 + (n ^ 2 + n + 1) = 1 + (n ^ 2 + n + 1) * n`. -/theorem cube_modEq_one (n : ℕ) : n ^ 3 ≡ 1 [MOD n ^ 2 + n + 1] := byunfold Nat.ModEqrw [← Nat.add_mod_right (n ^ 3), show n ^ 3 + (n ^ 2 + n + 1) = 1 + (n ^ 2 + n + 1) * n by ring,Nat.add_mul_mod_self_left]/-- `n ^ 2 + n + 1` divides `n ^ k + n + 1` whenever `k ≡ 2 (mod 3)`. -/theorem sq_add_dvd (n t : ℕ) : n ^ 2 + n + 1 ∣ n ^ (2 + 3 * t) + n + 1 :=dvd_of_period (cube_modEq_one n) dvd_rfl t/-! ### The counterexample -//-- The counterexample. -/abbrev n : ℕ := 243779134570275685188/-- The covering system: triples `(p, r, o)` with `n ^ o ≡ 1 (mod p)` and `p ∣ n ^ r + n + 1`,so that `p ∣ n ^ k + n + 1` whenever `k ≡ r (mod o)`. -/def cover : List (ℕ × ℕ × ℕ) :=[(5, 0, 4), (7, 1, 6), (11, 1, 5), (13, 10, 12), (19, 0, 9), (31, 9, 15), (37, 3, 18),(331, 0, 15), (757, 30, 36), (3511, 33, 45), (9091, 42, 45)]/-- Each triple `(p, r, o)` of `cover` has the stated properties (and `1 < p < n`, `o ∣ 180`). -/theorem cover_spec : ∀ e ∈ cover, 2 ≤ e.1 ∧ e.1 < n ∧ e.2.2 ∣ 180 ∧n ^ e.2.2 ≡ 1 [MOD e.1] ∧ e.1 ∣ n ^ e.2.1 + n + 1 := bydecide +kernel/-- The classes `r mod o` of `cover` contain every residue modulo `180` that is not`≡ 2 (mod 3)`. -/theorem cover_complete : ∀ j < 180, j % 3 = 2 ∨ ∃ e ∈ cover, j % e.2.2 = e.2.1 := bydecide +kernel/-- The case `k = 2`: `43` divides `n ^ 2 + n + 1`. -/theorem fortythree_dvd : 43 ∣ n ^ 2 + n + 1 := by decide +kernel/-- `n ^ k + n + 1` is composite for every `k > 1`. -/theorem not_prime (k : ℕ) (hk : 1 < k) : ¬ Nat.Prime (n ^ k + n + 1) := byhave hbig : n < n ^ k + n + 1 := by omegarcases cover_complete (k % 180) (Nat.mod_lt _ (by norm_num)) with h3 | ⟨e, he, hr⟩· -- `k ≡ 2 (mod 3)`rw [Nat.mod_mod_of_dvd k (by norm_num : 3 ∣ 180)] at h3obtain ⟨t, rfl⟩ : ∃ t, k = 2 + 3 * t := ⟨k / 3, by omega⟩rcases Nat.eq_zero_or_pos t with rfl | ht· exact Nat.not_prime_of_dvd_of_lt fortythree_dvd (by norm_num) (by norm_num)· refine Nat.not_prime_of_dvd_of_lt (sq_add_dvd n t) (by norm_num) ?_have : n ^ 2 < n ^ (2 + 3 * t) := Nat.pow_lt_pow_right (by norm_num) (by omega)omega· -- `k ≡ r (mod o)` for some `(p, r, o)` in the covering systemobtain ⟨hp2, hpn, ho180, ho, hdvd⟩ := cover_spec e herw [Nat.mod_mod_of_dvd k ho180] at hrhave hk' : k = e.2.1 + e.2.2 * (k / e.2.2) := by rw [← hr, Nat.mod_add_div]rw [hk'] at hbig ⊢exact Nat.not_prime_of_dvd_of_lt (dvd_of_period ho hdvd _) hp2 (hpn.trans hbig)end A346149/-- **Counterexample to the conjecture in OEIS A346149.** The number`n = 243779134570275685188` is not `≡ 1 (mod 3)`, yet `n ^ k + n + 1` is composite forevery `k > 1`. -/theorem a346149_counterexample : (243779134570275685188 : ℕ) % 3 ≠ 1 ∧∀ k : ℕ, 1 < k → ¬ Nat.Prime (243779134570275685188 ^ k + 243779134570275685188 + 1) :=⟨by norm_num, A346149.not_prime⟩#print axioms a346149_counterexample