Lean / Reader
Lean reader

Minimum61.lean

115 lines18 declarationsno sorryimports Mathlib.Tactic Mathlib.Algebra.BigOperators.Group.Finset.Basic
  1. import Mathlib.Tactic
  2. import Mathlib.Algebra.BigOperators.Group.Finset.Basic
  3. namespace ReciprocalMinimum
  4. def common : ℕ := 591133442051411133755680800
  5. def gap : ℕ := 8976696224760
  6. def denominators : List ℕ := (List.range 61).map (· + 1)
  7. def moduli : List ℕ := [61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11]
  8. def weight (d : ℕ) := common / d
  9. def total (pick : ℕ → Bool) : List ℕ → ℕ
  10. | [] => 0
  11. | d :: ds => (if pick d then weight d else 0) + total pick ds
  12. def options (p : ℕ) : List ℕ → List ℕ
  13. | [] => [0]
  14. | d :: ds =>
  15. let tail := options p ds
  16. let r := weight d % p
  17. if r = 0 then tail else tail ++ tail.map (fun x => (r + x) % p)
  18. theorem residue_possible (p : ℕ) (pick : ℕ → Bool) (ds : List ℕ) :
  19. total pick ds % p ∈ options p ds := by
  20. induction ds with
  21. | nil => simp [total, options]
  22. | cons d ds ih =>
  23. by_cases hz : weight d % p = 0
  24. · simp only [options, hz, if_true, total]
  25. cases pick d <;> simp [Nat.add_mod (weight d), hz, ih]
  26. · simp only [options, hz, if_false, total]
  27. cases pick d
  28. · simp [ih]
  29. · simp only [if_true, List.mem_append, List.mem_map]
  30. exact Or.inr ⟨_, ih, (Nat.add_mod _ _ _).symm⟩
  31. def expand (bound offset stride p : ℕ) (old allowed : List ℕ) : List ℕ :=
  32. old.flatMap fun r =>
  33. (((List.range (min p (bound / stride + 1))).map fun k => r + stride * k).filter
  34. fun u => u ≤ bound && allowed.contains ((u + offset) % p))
  35. theorem expand_complete {t bound offset stride p : ℕ} (ht : t ≤ bound)
  36. (hs : 0 < stride) (hp : 0 < p) (old allowed : List ℕ)
  37. (ho : t % stride ∈ old) (ha : (t + offset) % p ∈ allowed) :
  38. t % (stride * p) ∈ expand bound offset stride p old allowed := by
  39. let u := t % (stride * p)
  40. have hu : u < stride * p := Nat.mod_lt _ (Nat.mul_pos hs hp)
  41. have hb : u ≤ bound := le_trans (Nat.mod_le _ _) ht
  42. have hr : u % stride = t % stride := Nat.mod_mul_right_mod _ _ _
  43. have hq : u / stride < p := (Nat.div_lt_iff_lt_mul hs).mpr (by simpa [Nat.mul_comm] using hu)
  44. have hk : u / stride < bound / stride + 1 := Nat.lt_succ_of_le (Nat.div_le_div_right hb)
  45. have hpmod : u % p = t % p := Nat.mod_mod_of_dvd _ (dvd_mul_left _ _)
  46. have hv : (u + offset) % p = (t + offset) % p := by
  47. rw [Nat.add_mod u, Nat.add_mod t, hpmod]
  48. change u ∈ _
  49. simp only [expand, List.mem_flatMap, List.mem_filter, List.mem_map, List.mem_range,
  50. Bool.and_eq_true, decide_eq_true_eq, List.contains_iff_mem]
  51. refine ⟨t % stride, ho, ⟨⟨u / stride, lt_min hq hk, ?_⟩, hb, hv ▸ ha⟩⟩
  52. rw [← hr, Nat.mod_add_div]
  53. def sieve (bound offset : ℕ) (ds : List ℕ) :
  54. List ℕ → ℕ → List ℕ → List ℕ
  55. | [], _, old => old
  56. | p :: ps, stride, old =>
  57. sieve bound offset ds ps (stride * p) (expand bound offset stride p old (options p ds))
  58. theorem sieve_complete {t bound offset : ℕ} (ht : t ≤ bound) (ds ps : List ℕ)
  59. (stride : ℕ) (old : List ℕ) (hs : 0 < stride) (ho : t % stride ∈ old)
  60. (hp : ∀ p ∈ ps, 0 < p ∧ (t + offset) % p ∈ options p ds)
  61. (hbig : t < stride * ps.prod) : t ∈ sieve bound offset ds ps stride old := by
  62. induction ps generalizing stride old with
  63. | nil => simpa [sieve, Nat.mod_eq_of_lt (by simpa using hbig : t < stride)] using ho
  64. | cons p ps ih =>
  65. obtain ⟨hpos, hres⟩ := hp p (by simp)
  66. exact ih _ _ (Nat.mul_pos hs hpos)
  67. (expand_complete ht hs hpos old _ ho hres)
  68. (fun q hq => hp q (List.mem_cons_of_mem _ hq))
  69. (by simpa [List.prod_cons, Nat.mul_assoc] using hbig)
  70. def computed := sieve (2 * gap) (common - gap) denominators moduli 1 [0]
  71. set_option maxRecDepth 20000 in
  72. set_option maxHeartbeats 8000000 in
  73. theorem finite_sieve : computed = [gap, 2 * gap] ∨ computed = [2 * gap, gap] := by
  74. decide +kernel
  75. theorem near_one (pick : ℕ → Bool)
  76. (lo : common - gap ≤ total pick denominators)
  77. (hi : total pick denominators ≤ common + gap) :
  78. total pick denominators = common ∨ total pick denominators = common + gap := by
  79. let t := total pick denominators - (common - gap)
  80. have hgc : gap ≤ common := by decide
  81. have he : t + (common - gap) = total pick denominators := Nat.sub_add_cancel lo
  82. have ht : t ≤ 2 * gap := by omega
  83. have hm : t ∈ computed := by
  84. apply sieve_complete ht denominators moduli 1 [0] (by decide) (by simp [Nat.mod_one])
  85. · intro p hp
  86. refine ⟨?_, ?_⟩
  87. · have hall : ∀ k ∈ moduli, 0 < k := by decide
  88. exact hall _ hp
  89. · rw [he]; exact residue_possible _ _ _
  90. · have hprod : 2 * gap < 1 * moduli.prod := by decide
  91. omega
  92. rcases finite_sieve with h | h <;> rw [h] at hm <;> simp only [List.mem_cons,
  93. List.not_mem_nil, or_false] at hm <;> omega
  94. def witness : List ℕ := [4, 5, 6, 14, 17, 21, 29, 34, 39, 46, 47, 49, 53, 58, 61]
  95. theorem attains : total witness.contains denominators = common + gap := by decide +kernel
  96. theorem reduced_fraction : gap * 65852004707580 = common := by decide
  97. end ReciprocalMinimum