import Mathlib /-! # The twin-slot census of the tile (Copying Theorem, classical) Job #24 (formalize). For a modulus `m`, a *twin slot* is a residue `r : ZMod m` with `r` and `r + 2` both units, i.e. `gcd(r, m) = gcd(r + 2, m) = 1`. The classical count (Schemmel 1869; OEIS A059861; the repo's Copying Theorem `D_{n+1} = D_n (p - 2)`) is proved here: * `twinCount_mul`: the count is multiplicative over coprime moduli (Chinese remainder theorem as the ring isomorphism `ZMod.chineseRemainder`). * `twinCount_prime`: at a prime `p` the count is `1` if `p = 2` and `p - 2` otherwise. * `twinCount_prod`: for any finite set of distinct primes, the count at their product is the product of the local factors. * `twinCount_primorial`, `twinCount_primorial_odd`: for the primorial `x#` (Mathlib's `primorial x`), the count is `∏_{p ≤ x prime} (if p = 2 then 1 else p - 2) = ∏_{2 < p ≤ x prime} (p - 2)`. * `twinCount_eq_card_coprime`: the count equals `#{r < m : gcd(r, m) = gcd(r + 2, m) = 1}` for `0 < m`. -/ namespace TwinCensus /-- The number of twin slots mod `m`. -/ noncomputable def twinCount (m : ℕ) : ℕ := Nat.card {r : ZMod m // IsUnit r ∧ IsUnit (r + 2)} theorem twinCount_mul {m n : ℕ} (h : m.Coprime n) : twinCount (m * n) = twinCount m * twinCount n := by let e := ZMod.chineseRemainder h have key : ∀ r : ZMod (m * n), (IsUnit r ∧ IsUnit (r + 2)) ↔ ((IsUnit (e.toEquiv r).1 ∧ IsUnit ((e.toEquiv r).1 + 2)) ∧ (IsUnit (e.toEquiv r).2 ∧ IsUnit ((e.toEquiv r).2 + 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] change _ ↔ ((IsUnit (e r).1 ∧ IsUnit ((e r).1 + 2)) ∧ (IsUnit (e r).2 ∧ IsUnit ((e r).2 + 2))) rw [h1, h2, Prod.isUnit_iff, Prod.isUnit_iff, Prod.fst_add, Prod.snd_add, Prod.fst_ofNat, Prod.snd_ofNat] tauto unfold twinCount 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') theorem twinCount_prime {p : ℕ} (hp : p.Prime) : twinCount p = if p = 2 then 1 else p - 2 := by have := Fact.mk hp have hiff : ∀ r : ZMod p, (IsUnit r ∧ IsUnit (r + 2)) ↔ r ∈ (Finset.univ \ {0, -2} : Finset (ZMod p)) := by intro r have hneg : r + 2 = 0 ↔ r = -2 := by constructor <;> intro h <;> linear_combination h simp only [isUnit_iff_ne_zero, ne_eq, hneg, Finset.mem_sdiff, Finset.mem_univ, true_and, Finset.mem_insert, Finset.mem_singleton, not_or] unfold twinCount rw [Nat.card_congr (Equiv.subtypeEquivRight hiff), Nat.card_eq_fintype_card, Fintype.card_coe, Finset.card_sdiff_of_subset (Finset.subset_univ _), Finset.card_univ, ZMod.card] split_ifs with h2 · subst h2 have h0 : (-2 : ZMod 2) = 0 := by have h := ZMod.natCast_self 2 push_cast at h rw [h, neg_zero] rw [h0] simp · have hne : (0 : ZMod p) ≠ -2 := by intro h exact two_ne_zero_of_prime_ne_two hp h2 (by linear_combination h) rw [Finset.card_pair_eq_two_iff.mpr hne] theorem twinCount_one : twinCount 1 = 1 := by unfold twinCount have h : ∀ r : ZMod 1, IsUnit r ∧ IsUnit (r + 2) := fun r => ⟨isUnit_of_subsingleton r, isUnit_of_subsingleton _⟩ rw [Nat.card_congr (Equiv.subtypeUnivEquiv h), Nat.card_eq_fintype_card, ZMod.card] theorem twinCount_prod (S : Finset ℕ) (hS : ∀ p ∈ S, p.Prime) : twinCount (∏ p ∈ S, p) = ∏ p ∈ S, (if p = 2 then 1 else p - 2) := by induction S using Finset.induction_on with | empty => simpa using twinCount_one | @insert a s ha ih => have hpa : a.Prime := hS a (Finset.mem_insert_self a s) have hs : ∀ p ∈ s, p.Prime := fun p hp => hS p (Finset.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 [Finset.prod_insert ha, Finset.prod_insert ha, twinCount_mul hcop, twinCount_prime hpa, ih hs] theorem twinCount_primorial (x : ℕ) : twinCount (primorial x) = ∏ p ∈ Finset.filter Nat.Prime (Finset.range (x + 1)), (if p = 2 then 1 else p - 2) := twinCount_prod _ (fun _ hp => (Finset.mem_filter.mp hp).2) theorem twinCount_primorial_odd (x : ℕ) : twinCount (primorial x) = ∏ p ∈ Finset.filter (fun p => p.Prime ∧ 2 < p) (Finset.range (x + 1)), (p - 2) := by rw [twinCount_primorial] conv_rhs => rw [← Finset.filter_filter, Finset.prod_filter] apply Finset.prod_congr rfl intro p hp have hpp : p.Prime := (Finset.mem_filter.mp hp).2 by_cases h2 : p = 2 · simp [h2] · have h2lt : 2 < p := lt_of_le_of_ne hpp.two_le (Ne.symm h2) simp [h2, h2lt] /-- The census in the repo's gcd form, over the naturals below `m`. -/ theorem twinCount_eq_card_coprime {m : ℕ} (hm : 0 < m) : twinCount m = ((Finset.range m).filter (fun r => r.Coprime m ∧ (r + 2).Coprime m)).card := by have : NeZero m := ⟨hm.ne'⟩ unfold twinCount rw [← Nat.card_eq_finsetCard] apply Nat.card_congr refine { toFun := fun r => ⟨r.1.val, by rw [Finset.mem_filter, Finset.mem_range] refine ⟨ZMod.val_lt r.1, ?_, ?_⟩ · rw [← ZMod.isUnit_iff_coprime, ZMod.natCast_zmod_val]; exact r.2.1 · rw [← ZMod.isUnit_iff_coprime]; push_cast [ZMod.natCast_zmod_val]; exact r.2.2⟩ invFun := fun s => ⟨(s.1 : ZMod m), by have hs := s.2 rw [Finset.mem_filter, Finset.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 push_cast at this; exact this⟩ left_inv := fun r => by ext; simp right_inv := fun s => by ext have hs := s.2 rw [Finset.mem_filter, Finset.mem_range] at hs simp [ZMod.val_natCast, Nat.mod_eq_of_lt hs.1] } end TwinCensus