import Mathlib.Tacticimport Mathlib.Algebra.BigOperators.Group.Finset.Basicnamespace ReciprocalMinimumdef common : ℕ := 591133442051411133755680800def gap : ℕ := 8976696224760def denominators : List ℕ := (List.range 61).map (· + 1)def moduli : List ℕ := [61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11]def weight (d : ℕ) := common / ddef total (pick : ℕ → Bool) : List ℕ → ℕ| [] => 0| d :: ds => (if pick d then weight d else 0) + total pick dsdef options (p : ℕ) : List ℕ → List ℕ| [] => [0]| d :: ds =>let tail := options p dslet r := weight d % pif r = 0 then tail else tail ++ tail.map (fun x => (r + x) % p)theorem residue_possible (p : ℕ) (pick : ℕ → Bool) (ds : List ℕ) :total pick ds % p ∈ options p ds := byinduction ds with| nil => simp [total, options]| cons d ds ih =>by_cases hz : weight d % p = 0· simp only [options, hz, if_true, total]cases pick d <;> simp [Nat.add_mod (weight d), hz, ih]· simp only [options, hz, if_false, total]cases pick d· simp [ih]· simp only [if_true, List.mem_append, List.mem_map]exact Or.inr ⟨_, ih, (Nat.add_mod _ _ _).symm⟩def expand (bound offset stride p : ℕ) (old allowed : List ℕ) : List ℕ :=old.flatMap fun r =>(((List.range (min p (bound / stride + 1))).map fun k => r + stride * k).filterfun u => u ≤ bound && allowed.contains ((u + offset) % p))theorem expand_complete {t bound offset stride p : ℕ} (ht : t ≤ bound)(hs : 0 < stride) (hp : 0 < p) (old allowed : List ℕ)(ho : t % stride ∈ old) (ha : (t + offset) % p ∈ allowed) :t % (stride * p) ∈ expand bound offset stride p old allowed := bylet u := t % (stride * p)have hu : u < stride * p := Nat.mod_lt _ (Nat.mul_pos hs hp)have hb : u ≤ bound := le_trans (Nat.mod_le _ _) hthave hr : u % stride = t % stride := Nat.mod_mul_right_mod _ _ _have hq : u / stride < p := (Nat.div_lt_iff_lt_mul hs).mpr (by simpa [Nat.mul_comm] using hu)have hk : u / stride < bound / stride + 1 := Nat.lt_succ_of_le (Nat.div_le_div_right hb)have hpmod : u % p = t % p := Nat.mod_mod_of_dvd _ (dvd_mul_left _ _)have hv : (u + offset) % p = (t + offset) % p := byrw [Nat.add_mod u, Nat.add_mod t, hpmod]change u ∈ _simp only [expand, List.mem_flatMap, List.mem_filter, List.mem_map, List.mem_range,Bool.and_eq_true, decide_eq_true_eq, List.contains_iff_mem]refine ⟨t % stride, ho, ⟨⟨u / stride, lt_min hq hk, ?_⟩, hb, hv ▸ ha⟩⟩rw [← hr, Nat.mod_add_div]def sieve (bound offset : ℕ) (ds : List ℕ) :List ℕ → ℕ → List ℕ → List ℕ| [], _, old => old| p :: ps, stride, old =>sieve bound offset ds ps (stride * p) (expand bound offset stride p old (options p ds))theorem sieve_complete {t bound offset : ℕ} (ht : t ≤ bound) (ds ps : List ℕ)(stride : ℕ) (old : List ℕ) (hs : 0 < stride) (ho : t % stride ∈ old)(hp : ∀ p ∈ ps, 0 < p ∧ (t + offset) % p ∈ options p ds)(hbig : t < stride * ps.prod) : t ∈ sieve bound offset ds ps stride old := byinduction ps generalizing stride old with| nil => simpa [sieve, Nat.mod_eq_of_lt (by simpa using hbig : t < stride)] using ho| cons p ps ih =>obtain ⟨hpos, hres⟩ := hp p (by simp)exact ih _ _ (Nat.mul_pos hs hpos)(expand_complete ht hs hpos old _ ho hres)(fun q hq => hp q (List.mem_cons_of_mem _ hq))(by simpa [List.prod_cons, Nat.mul_assoc] using hbig)def computed := sieve (2 * gap) (common - gap) denominators moduli 1 [0]set_option maxRecDepth 20000 inset_option maxHeartbeats 8000000 intheorem finite_sieve : computed = [gap, 2 * gap] ∨ computed = [2 * gap, gap] := bydecide +kerneltheorem near_one (pick : ℕ → Bool)(lo : common - gap ≤ total pick denominators)(hi : total pick denominators ≤ common + gap) :total pick denominators = common ∨ total pick denominators = common + gap := bylet t := total pick denominators - (common - gap)have hgc : gap ≤ common := by decidehave he : t + (common - gap) = total pick denominators := Nat.sub_add_cancel lohave ht : t ≤ 2 * gap := by omegahave hm : t ∈ computed := byapply sieve_complete ht denominators moduli 1 [0] (by decide) (by simp [Nat.mod_one])· intro p hprefine ⟨?_, ?_⟩· have hall : ∀ k ∈ moduli, 0 < k := by decideexact hall _ hp· rw [he]; exact residue_possible _ _ _· have hprod : 2 * gap < 1 * moduli.prod := by decideomegarcases finite_sieve with h | h <;> rw [h] at hm <;> simp only [List.mem_cons,List.not_mem_nil, or_false] at hm <;> omegadef witness : List ℕ := [4, 5, 6, 14, 17, 21, 29, 34, 39, 46, 47, 49, 53, 58, 61]theorem attains : total witness.contains denominators = common + gap := by decide +kerneltheorem reduced_fraction : gap * 65852004707580 = common := by decideend ReciprocalMinimum