import Mathlib /-! # The exact two-class pair correlation `J(d) = ∏_p ρ_p(d)/p` solveathome twin-primes job #30 (formalize lane). Source: header of `research/06-variance-theorem.js`. Let `P = p_n#` and call `r mod P` a twin slot when `r mod p ∉ {0, p - 2}` for every prime `p ≤ p_n`, i.e. `r` and `r + 2` are both units mod `P`. For an integer `d` the number of `r ∈ ℤ/P` with `r` and `r + d` both twin slots is `∏_{p ≤ p_n} ρ_p(d)`, where for odd `p` `ρ_p(d) = p - 2` if `d ≡ 0`, `p - 3` if `d ≡ ±2`, `p - 4` otherwise (mod `p`), and `ρ_2(d) = 1` if `d` is even, `0` if odd. * `pairCount m d`: the number of `r : ZMod m` with `r`, `r + 2`, `r + d`, `r + d + 2` all units. * `pairCount_mul`: multiplicative over coprime moduli (CRT as `ZMod.chineseRemainder`). * `pairCount_prime`: at a prime, `pairCount p d = rho p d`, by counting the complement of `{0, -2, -d, -2 - d}` with a case split on `d mod p`. At `p = 3` with `d ≡ ±2` the count is `0 = p - 3`; at `p = 2` the stated `ρ_2` is checked on both classes. * `pairCount_primorial`: `pairCount (primorial n) d = ∏_{p ≤ n prime} rho p d`. * `density_primorial`: `J(d) = pairCount / P = ∏ rho p d / p` over `ℚ`. * `pair_count_residues`: the same count over `r < P` in divisibility form, `¬p ∣ r ∧ ¬p ∣ r + 2 ∧ ¬p ∣ r + d ∧ ¬p ∣ r + d + 2` for every prime `p ≤ n` (for `d : ℕ`; the count depends only on `d mod P`). The multiplicativity and base-case pattern follows `Census.lean` of return #77 (job #24, same handle). The statement is the repo's; no discrepancy was found at any prime, including `p = 2, 3`. -/ namespace PairCorrelation open Finset /-- `r` and `r + d` are both twin slots mod `m`. -/ def Pair (m : ℕ) (d : ℤ) (r : ZMod m) : Prop := IsUnit r ∧ IsUnit (r + 2) ∧ IsUnit (r + d) ∧ IsUnit (r + d + 2) /-- The number of `r : ZMod m` with `r` and `r + d` both twin slots. -/ noncomputable def pairCount (m : ℕ) (d : ℤ) : ℕ := Nat.card {r : ZMod m // Pair m d r} /-- The repo's local factor `ρ_p(d)`. -/ def rho (p : ℕ) (d : ℤ) : ℕ := if p = 2 then (if (d : ZMod 2) = 0 then 1 else 0) else if (d : ZMod p) = 0 then p - 2 else if (d : ZMod p) = 2 ∨ (d : ZMod p) = -2 then p - 3 else p - 4 theorem pairCount_mul {m n : ℕ} (h : m.Coprime n) (d : ℤ) : pairCount (m * n) d = pairCount m d * pairCount n d := by let e := ZMod.chineseRemainder h have key : ∀ r : ZMod (m * n), Pair (m * n) d r ↔ (Pair m d (e.toEquiv r).1 ∧ Pair n d (e.toEquiv r).2) := by intro r have h1 : IsUnit r ↔ IsUnit (e r) := (isUnit_map_iff e r).symm have h2 : IsUnit (r + 2) ↔ IsUnit (e r + 2) := by rw [← isUnit_map_iff e (r + 2), map_add, map_ofNat] have h3 : IsUnit (r + d) ↔ IsUnit (e r + d) := by rw [← isUnit_map_iff e (r + d), map_add, map_intCast] have h4 : IsUnit (r + d + 2) ↔ IsUnit (e r + d + 2) := by rw [← isUnit_map_iff e (r + d + 2), map_add, map_add, map_intCast, map_ofNat] change _ ↔ (Pair m d (e r).1 ∧ Pair n d (e r).2) unfold Pair rw [h1, h2, h3, h4] simp only [Prod.isUnit_iff, Prod.fst_add, Prod.snd_add, Prod.fst_intCast, Prod.snd_intCast, Prod.fst_ofNat, Prod.snd_ofNat] tauto unfold pairCount rw [← Nat.card_prod] exact Nat.card_congr ((e.toEquiv.subtypeEquiv key).trans Equiv.subtypeProdEquivProd) theorem two_ne_zero_of_prime_ne_two {p : ℕ} (hp : p.Prime) (h2 : p ≠ 2) : (2 : ZMod p) ≠ 0 := by intro h have h' : ((2 : ℕ) : ZMod p) = 0 := by exact_mod_cast h rw [CharP.cast_eq_zero_iff (ZMod p) p] at h' exact h2 ((Nat.prime_dvd_prime_iff_eq hp Nat.prime_two).mp h') /-- Over `ZMod 2`: `2 - #{0, -2, -c, -2 - c} = ρ_2`. -/ theorem card_two : ∀ c : ZMod 2, 2 - ({0, -2, -c, -2 - c} : Finset (ZMod 2)).card = if c = 0 then 1 else 0 := by decide theorem pairCount_prime {p : ℕ} (hp : p.Prime) (d : ℤ) : pairCount p d = rho p d := by have := Fact.mk hp have hiff : ∀ r : ZMod p, Pair p d r ↔ r ∈ (univ \ {0, -2, -(d : ZMod p), -2 - (d : ZMod p)} : Finset (ZMod p)) := by intro r have e1 : r + 2 = 0 ↔ r = -2 := by constructor <;> intro h <;> linear_combination h have e2 : r + d = 0 ↔ r = -(d : ZMod p) := by constructor <;> intro h <;> linear_combination h have e3 : r + d + 2 = 0 ↔ r = -2 - (d : ZMod p) := by constructor <;> intro h <;> linear_combination h simp only [Pair, isUnit_iff_ne_zero, ne_eq, e1, e2, e3, mem_sdiff, mem_univ, true_and, mem_insert, mem_singleton, not_or] have hcard : pairCount p d = p - ({0, -2, -(d : ZMod p), -2 - (d : ZMod p)} : Finset (ZMod p)).card := by unfold pairCount rw [Nat.card_congr (Equiv.subtypeEquivRight hiff), Nat.card_eq_fintype_card, Fintype.card_coe, card_sdiff_of_subset (subset_univ _), card_univ, ZMod.card] rw [hcard] unfold rho by_cases hp2 : p = 2 · subst hp2 rw [if_pos rfl] exact card_two _ rw [if_neg hp2] have h2 := two_ne_zero_of_prime_ne_two hp hp2 have h4 : (4 : ZMod p) ≠ 0 := by have : (4 : ZMod p) = 2 * 2 := by norm_num rw [this] exact mul_ne_zero h2 h2 have h02 : (0 : ZMod p) ≠ -2 := fun h => h2 (by linear_combination h) by_cases hd0 : (d : ZMod p) = 0 · rw [if_pos hd0, hd0, neg_zero, sub_zero] have hS : ({0, -2, 0, -2} : Finset (ZMod p)) = {0, -2} := by ext x simp only [mem_insert, mem_singleton] tauto rw [hS, card_pair h02] rw [if_neg hd0] by_cases hdpm : (d : ZMod p) = 2 ∨ (d : ZMod p) = -2 · rw [if_pos hdpm] rcases hdpm with hd | hd · -- d ≡ 2: the classes are 0, -2, -4 rw [hd] have hS : ({0, -2, -2, -2 - 2} : Finset (ZMod p)) = {0, -2, -2 - 2} := by ext x simp only [mem_insert, mem_singleton] tauto have hA : (0 : ZMod p) ∉ ({-2, -2 - 2} : Finset (ZMod p)) := by simp only [mem_insert, mem_singleton, not_or] exact ⟨h02, fun h => h4 (by linear_combination h)⟩ have hB : (-2 : ZMod p) ≠ -2 - 2 := fun h => h2 (by linear_combination h) rw [hS, card_insert_of_notMem hA, card_pair hB] · -- d ≡ -2: the classes are 0, -2, 2 rw [hd, neg_neg, sub_neg_eq_add, neg_add_cancel] have hS : ({0, -2, 2, 0} : Finset (ZMod p)) = {0, -2, 2} := by ext x simp only [mem_insert, mem_singleton] tauto have hA : (0 : ZMod p) ∉ ({-2, 2} : Finset (ZMod p)) := by simp only [mem_insert, mem_singleton, not_or] exact ⟨h02, fun h => h2 (by linear_combination -h)⟩ have hB : (-2 : ZMod p) ≠ 2 := fun h => h4 (by linear_combination -h) rw [hS, card_insert_of_notMem hA, card_pair hB] · rw [if_neg hdpm] rw [not_or] at hdpm obtain ⟨hdn2, hdm2⟩ := hdpm have hA : (0 : ZMod p) ∉ ({-2, -(d : ZMod p), -2 - (d : ZMod p)} : Finset (ZMod p)) := by simp only [mem_insert, mem_singleton, not_or] exact ⟨h02, fun h => hd0 (by linear_combination h), fun h => hdm2 (by linear_combination h)⟩ have hB : (-2 : ZMod p) ∉ ({-(d : ZMod p), -2 - (d : ZMod p)} : Finset (ZMod p)) := by simp only [mem_insert, mem_singleton, not_or] exact ⟨fun h => hdn2 (by linear_combination h), fun h => hd0 (by linear_combination h)⟩ have hC : -(d : ZMod p) ≠ -2 - (d : ZMod p) := fun h => h2 (by linear_combination h) rw [card_insert_of_notMem hA, card_insert_of_notMem hB, card_pair hC] theorem pairCount_one (d : ℤ) : pairCount 1 d = 1 := by unfold pairCount have h : ∀ r : ZMod 1, Pair 1 d r := fun r => ⟨isUnit_of_subsingleton _, isUnit_of_subsingleton _, isUnit_of_subsingleton _, isUnit_of_subsingleton _⟩ rw [Nat.card_congr (Equiv.subtypeUnivEquiv h), Nat.card_eq_fintype_card, ZMod.card] theorem pairCount_prod (S : Finset ℕ) (hS : ∀ p ∈ S, p.Prime) (d : ℤ) : pairCount (∏ p ∈ S, p) d = ∏ p ∈ S, rho p d := by induction S using Finset.induction_on with | empty => simpa using pairCount_one d | @insert a s ha ih => have hpa : a.Prime := hS a (mem_insert_self a s) have hs : ∀ p ∈ s, p.Prime := fun p hp => hS p (mem_insert_of_mem hp) have hcop : a.Coprime (∏ p ∈ s, p) := Nat.Coprime.prod_right fun p hp => (Nat.coprime_primes hpa (hs p hp)).mpr (fun h => ha (by rw [h]; exact hp)) rw [prod_insert ha, prod_insert ha, pairCount_mul hcop, pairCount_prime hpa, ih hs] /-- **The count.** `#{r ∈ ℤ/p_n# : r, r + d twin slots} = ∏_{p ≤ n prime} ρ_p(d)`. -/ theorem pairCount_primorial (n : ℕ) (d : ℤ) : pairCount (primorial n) d = ∏ p ∈ filter Nat.Prime (range (n + 1)), rho p d := pairCount_prod _ (fun _ hp => (mem_filter.mp hp).2) d end PairCorrelation namespace PairCorrelation open Finset /-- **The density.** `J(d) = pairCount / P = ∏_{p ≤ n prime} ρ_p(d) / p` over `ℚ`. -/ theorem density_primorial (n : ℕ) (d : ℤ) : (pairCount (primorial n) d : ℚ) / (primorial n : ℕ) = ∏ p ∈ filter Nat.Prime (range (n + 1)), (rho p d : ℚ) / p := by rw [pairCount_primorial, prod_div_distrib] simp only [primorial, Nat.cast_prod] /-- The count over the naturals below `m`, in coprimality form (for `d : ℕ`). -/ theorem pairCount_eq_card_residues {m : ℕ} (hm : 0 < m) (d : ℕ) : pairCount m d = ((range m).filter (fun r => r.Coprime m ∧ (r + 2).Coprime m ∧ (r + d).Coprime m ∧ (r + d + 2).Coprime m)).card := by have : NeZero m := ⟨hm.ne'⟩ unfold pairCount rw [← Nat.card_eq_finsetCard] apply Nat.card_congr refine { toFun := fun r => ⟨r.1.val, by obtain ⟨h1, h2, h3, h4⟩ := r.2 rw [mem_filter, mem_range] refine ⟨ZMod.val_lt r.1, ?_, ?_, ?_, ?_⟩ · rw [← ZMod.isUnit_iff_coprime, ZMod.natCast_zmod_val] exact h1 · rw [← ZMod.isUnit_iff_coprime] push_cast [ZMod.natCast_zmod_val] at h2 ⊢ exact h2 · rw [← ZMod.isUnit_iff_coprime] push_cast [ZMod.natCast_zmod_val] at h3 ⊢ exact h3 · rw [← ZMod.isUnit_iff_coprime] push_cast [ZMod.natCast_zmod_val] at h4 ⊢ exact h4⟩ invFun := fun s => ⟨(s.1 : ZMod m), by have hs := s.2 rw [mem_filter, mem_range] at hs refine ⟨(ZMod.isUnit_iff_coprime _ _).mpr hs.2.1, ?_, ?_, ?_⟩ · have := (ZMod.isUnit_iff_coprime (s.1 + 2) m).mpr hs.2.2.1 push_cast at this ⊢ exact this · have := (ZMod.isUnit_iff_coprime (s.1 + d) m).mpr hs.2.2.2.1 push_cast at this ⊢ exact this · have := (ZMod.isUnit_iff_coprime (s.1 + d + 2) m).mpr hs.2.2.2.2 push_cast at this ⊢ exact this⟩ left_inv := fun r => by ext; simp right_inv := fun s => by ext have hs := s.2 rw [mem_filter, mem_range] at hs simp [ZMod.val_natCast, Nat.mod_eq_of_lt hs.1] } theorem coprime_primorial_iff (r n : ℕ) : r.Coprime (primorial n) ↔ ∀ p ∈ filter Nat.Prime (range (n + 1)), ¬ p ∣ r := by unfold primorial rw [Nat.coprime_prod_right_iff] exact forall₂_congr fun p hp => by rw [Nat.coprime_comm, Nat.Prime.coprime_iff_not_dvd (mem_filter.mp hp).2] /-- **The count in the brief's residue form.** For `d : ℕ`, the number of `r < p_n#` with `r ≢ 0, -2` and `r + d ≢ 0, -2` modulo every prime `p ≤ n` is `∏_{p ≤ n prime} ρ_p(d)`. -/ theorem pair_count_residues (n d : ℕ) : ((range (primorial n)).filter (fun r => ∀ p ∈ filter Nat.Prime (range (n + 1)), ¬ p ∣ r ∧ ¬ p ∣ r + 2 ∧ ¬ p ∣ r + d ∧ ¬ p ∣ r + d + 2)).card = ∏ p ∈ filter Nat.Prime (range (n + 1)), rho p d := by rw [← pairCount_primorial, pairCount_eq_card_residues (primorial_pos n)] congr 1 refine filter_congr fun r _ => ?_ simp only [coprime_primorial_iff] constructor · intro h exact ⟨fun p hp => (h p hp).1, fun p hp => (h p hp).2.1, fun p hp => (h p hp).2.2.1, fun p hp => (h p hp).2.2.2⟩ · rintro ⟨h1, h2, h3, h4⟩ p hp exact ⟨h1 p hp, h2 p hp, h3 p hp, h4 p hp⟩ end PairCorrelation /-! Axiom check, and evaluation-only spot checks (not used by any proof): brute force over `r < P` against `∏ ρ_p(d)` at `P = 5# = 30` (`d = 0, ..., 7`) and `P = 7# = 210` (`d = 0, ..., 30`). -/ #print axioms PairCorrelation.pairCount_primorial #print axioms PairCorrelation.density_primorial #print axioms PairCorrelation.pair_count_residues #eval (List.range 8).map fun d => (((Finset.range (primorial 5)).filter (fun r => ∀ p ∈ Finset.filter Nat.Prime (Finset.range 6), ¬ p ∣ r ∧ ¬ p ∣ r + 2 ∧ ¬ p ∣ r + d ∧ ¬ p ∣ r + d + 2)).card, ∏ p ∈ Finset.filter Nat.Prime (Finset.range 6), PairCorrelation.rho p d) #eval (List.range 31).map fun d => (((Finset.range (primorial 7)).filter (fun r => ∀ p ∈ Finset.filter Nat.Prime (Finset.range 8), ¬ p ∣ r ∧ ¬ p ∣ r + 2 ∧ ¬ p ∣ r + d ∧ ¬ p ∣ r + d + 2)).card, ∏ p ∈ Finset.filter Nat.Prime (Finset.range 8), PairCorrelation.rho p d)