Lean / Reader
Lean readerOptimalSwitches.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports Thresholds, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import Thresholdsimport Mathlib.Algebra.BigOperators.Associatednamespace FactorialSwitchesopen Finsetdef marked (H : Finset ℕ) (u : ℕ → ℕ) : Finset ℕ := H.image (fun p => p * u p)/-- The large marker is uniquely recoverable from a marked factor. -/theorem marked_inj (H : Finset ℕ) (B : ℕ) (u : ℕ → ℕ)(pr : ∀ p ∈ H, p.Prime) (large : ∀ p ∈ H, B < p)(size : ∀ p ∈ H, 0 < u p ∧ u p ≤ B) :Set.InjOn (fun p => p * u p) (↑H : Set ℕ) := byintro p hp l hl eqchange p * u p = l * u l at eqhave dv : p ∣ l*u l := by rw [← eq]; exact Nat.dvd_mul_right p (u p)rcases (pr p hp).dvd_mul.mp dv with h|h· exact ((Nat.dvd_prime (pr l hl)).mp h).resolve_left (pr p hp).ne_one· have := Nat.le_of_dvd (size l hl).1 hhave := (size l hl).2have := large p hpomegatheorem marked_prod (H : Finset ℕ) (B : ℕ) (u : ℕ → ℕ)(pr : ∀ p ∈ H, p.Prime) (large : ∀ p ∈ H, B < p)(size : ∀ p ∈ H, 0 < u p ∧ u p ≤ B) :(∏ x ∈ marked H u, x) = (∏ p ∈ H, p) * ∏ p ∈ H, u p := byunfold markedrw [prod_image (marked_inj H B u pr large size),prod_mul_distrib]theorem marked_disjoint (H K : Finset ℕ) (u : ℕ → ℕ)(free : ∀ a ∈ K, ∀ p ∈ H, ¬p ∣ a) : Disjoint K (marked H u) := byrefine Finset.disjoint_left.mpr ?_intro x hx markobtain ⟨p,hp,rfl⟩ := Finset.mem_image.mp markexact free _ hx p hp (Nat.dvd_mul_right _ _)/-- A reassignment preserving the product of the cofactors retains theentire omitted product, even with an arbitrary untouched background. -/theorem switch_product (H K : Finset ℕ) (B : ℕ) (u v : ℕ → ℕ)(pr : ∀ p ∈ H, p.Prime) (large : ∀ p ∈ H, B < p)(old : ∀ p ∈ H, 0 < u p ∧ u p ≤ B) (new : ∀ p ∈ H, 0 < v p ∧ v p ≤ B)(free : ∀ a ∈ K, ∀ p ∈ H, ¬p ∣ a)(cofactors : ∏ p ∈ H, u p = ∏ p ∈ H, v p) :(∏ x ∈ K ∪ marked H u, x) = ∏ x ∈ K ∪ marked H v, x := byrw [prod_union (marked_disjoint H K u free),prod_union (marked_disjoint H K v free),marked_prod H B u pr large old,marked_prod H B v pr large new,cofactors]/-- The selected factors after switching give the same target in the same interval. -/theorem switch_selected (A H K : Finset ℕ) (B target : ℕ) (u v : ℕ → ℕ)(pr : ∀ p ∈ H, p.Prime) (large : ∀ p ∈ H, B < p)(old : ∀ p ∈ H, 0 < u p ∧ u p ≤ B) (new : ∀ p ∈ H, 0 < v p ∧ v p ≤ B)(free : ∀ a ∈ K, ∀ p ∈ H, ¬p ∣ a)(cofactors : ∏ p ∈ H, u p = ∏ p ∈ H, v p)(within : K ∪ marked H v ⊆ A) (pos : ∀ a ∈ A, 0 < a)(target_eq : target * (∏ x ∈ K ∪ marked H u, x) = ∏ x ∈ A, x) :∏ x ∈ A \ (K ∪ marked H v), x = target := byhave same := switch_product H K B u v pr large old new free cofactorsrw [same] at target_eqhave mul := (Finset.prod_sdiff (f := id) within).trans target_eq.symmexact Nat.eq_of_mul_eq_mul_right (Finset.prod_pos (fun i hi => pos i (within hi))) mul/-- The reassigned labels themselves can be recovered uniquely from the new omitted set. -/theorem recover_label (H K : Finset ℕ) (B : ℕ) (u v : ℕ → ℕ)(pr : ∀ p ∈ H, p.Prime) (large : ∀ p ∈ H, B < p)(new : ∀ p ∈ H, 0 < v p ∧ v p ≤ B) (free : ∀ a ∈ K, ∀ p ∈ H, ¬p ∣ a)(sets : K ∪ marked H u = K ∪ marked H v) (p : ℕ) (hp : p ∈ H) : u p = v p := byhave mem : p*u p ∈ K ∪ marked H u := Finset.mem_union.mpr (Or.inr (Finset.mem_image_of_mem _ hp))rw [sets] at memrcases Finset.mem_union.mp mem with h|h· exact False.elim (free _ h p hp (Nat.dvd_mul_right _ _))obtain ⟨l,hl,e⟩ := Finset.mem_image.mp hhave dv : p ∣ l*v l := by rw [e]; exact Nat.dvd_mul_right _ _rcases (pr p hp).dvd_mul.mp dv with h|h· have eq := ((Nat.dvd_prime (pr l hl)).mp h).resolve_left (pr p hp).ne_onesubst lexact Nat.eq_of_mul_eq_mul_left (pr p hp).pos e.symm· have := Nat.le_of_dvd (new l hl).1 hhave := (new l hl).2have := large p hpomega#print axioms switch_selected#print axioms recover_label#print axioms marked_inj#print axioms switch_productend FactorialSwitches