Lean / Reader
Lean reader

Partial470.lean

379 lines15 declarationsno sorryimports Mathlib.NumberTheory.FactorisationProperties Mathlib.NumberTheory.ArithmeticFunction.Misc
  1. import Mathlib.NumberTheory.FactorisationProperties
  2. import Mathlib.NumberTheory.ArithmeticFunction.Misc
  3. /-!
  4. # Partial Machine-Checked Results for Erdős Problem #470
  5. Formalizes verified partial theorems supporting our investigation of Erdős #470
  6. (existence of odd weird numbers):
  7. 1. `weird_of_abundance_lt_min_prime` & `weird_of_odd_abundance_two`:
  8. If `n` is abundant with `2 ≤ δ(n) < p_min(n)` (in particular, `n` odd and `δ(n) = 2`),
  9. then `n` is weird.
  10. 2. `not_abundant_kravitz_base` & `not_weird_kravitz_base`:
  11. Kravitz-type `m * Q * R` obstruction: if `2 * m - σ(m) ≥ 2` and `R > Q > σ(m)` are
  12. primes coprime to `m`, then `m * Q * R` is deficient (hence not weird).
  13. 3. `not_abundant_odd_sqfree_omega_le_four` & `not_weird_odd_sqfree_omega_le_four`:
  14. Every odd squarefree `n` with `ω(n) ≤ 4` is not abundant (hence not weird).
  15. 4. `abundant_945`, `pseudoperfect_945`, `not_weird_945`:
  16. The smallest odd abundant number `945` is pseudoperfect, hence not weird.
  17. -/
  18. open Finset
  19. namespace Erdos470Partial
  20. /-! ## 1. Sufficient condition for weirdness via small abundance -/
  21. lemma sum_properDivisors_sdiff {n : ℕ} {s : Finset ℕ} (hs : s ⊆ n.properDivisors)
  22. (hsum : ∑ i ∈ s, i = n) :
  23. ∑ i ∈ (n.properDivisors \ s), i = (∑ i ∈ n.properDivisors, i) - n := by
  24. have h := Finset.sum_sdiff (f := fun i : ℕ => i) hs
  25. omega
  26. /-- If `n > 0` has abundance `δ = σ(n) - 2n` with `1 < δ` and every prime factor `p ∣ n`
  27. satisfies `δ < p`, then `n` is weird. -/
  28. theorem weird_of_abundance_lt_min_prime {n δ : ℕ} (hn : 0 < n) (hδ : 1 < δ)
  29. (hmin : ∀ p, p.Prime → p ∣ n → δ < p)
  30. (hsum : ∑ i ∈ n.properDivisors, i = n + δ) : n.Weird := by
  31. refine ⟨?_, ?_⟩
  32. · unfold Nat.Abundant
  33. omega
  34. · rintro ⟨_, s, hs, hsn⟩
  35. set t := n.properDivisors \ s
  36. have htsum : ∑ i ∈ t, i = δ := by
  37. have h := sum_properDivisors_sdiff hs hsn
  38. change ∑ i ∈ (n.properDivisors \ s), i = δ
  39. omega
  40. have ht_sub : t ⊆ ({1} : Finset ℕ) := by
  41. intro d hd
  42. rw [Finset.mem_singleton]
  43. have hd_prop : d ∈ n.properDivisors := (Finset.mem_sdiff.mp hd).1
  44. have hd_dvd : d ∣ n := (Nat.mem_properDivisors.mp hd_prop).1
  45. have hd_pos : 0 < d := Nat.pos_of_ne_zero fun h => by
  46. subst h
  47. rw [zero_dvd_iff] at hd_dvd
  48. exact hn.ne' hd_dvd
  49. have hd_le : d ≤ δ := by
  50. calc d ≤ ∑ i ∈ t, i :=
  51. Finset.single_le_sum (f := fun i : ℕ => i) (fun _ _ => Nat.zero_le _) hd
  52. _ = δ := htsum
  53. by_contra hd_ne_one
  54. have hp : d.minFac.Prime := Nat.minFac_prime hd_ne_one
  55. have hp_dvd : d.minFac ∣ n := (Nat.minFac_dvd d).trans hd_dvd
  56. have hδ_lt : δ < d.minFac := hmin d.minFac hp hp_dvd
  57. have hmf_le : d.minFac ≤ d := Nat.minFac_le hd_pos
  58. omega
  59. have hle1 : ∑ i ∈ t, i ≤ 1 := by
  60. calc ∑ i ∈ t, i ≤ ∑ i ∈ ({1} : Finset ℕ), i :=
  61. Finset.sum_le_sum_of_subset_of_nonneg ht_sub (fun _ _ _ => Nat.zero_le _)
  62. _ = 1 := by simp
  63. omega
  64. /-- Any odd `n` with `σ(n) = 2n + 2` (i.e., abundance `2`) is weird. -/
  65. theorem weird_of_odd_abundance_two {n : ℕ} (hodd : Odd n)
  66. (hsum : ∑ i ∈ n.properDivisors, i = n + 2) : n.Weird := by
  67. have hn : 0 < n := Nat.pos_of_ne_zero (by rintro rfl; simp at hodd)
  68. refine weird_of_abundance_lt_min_prime hn (by decide) ?_ hsum
  69. intro p hp hpn
  70. have hp2 : p ≠ 2 := by
  71. rintro rfl
  72. exact (Nat.not_odd_iff_even.mpr (even_iff_two_dvd.mpr hpn)) hodd
  73. have hp_ge2 : 2 ≤ p := hp.two_le
  74. omega
  75. /-! ## 2. Theorem 2: Obstruction to Kravitz-type `m * Q * R` constructions -/
  76. /-- If `m ≥ 1` is deficient with `2 * m - σ(m) ≥ 2`, and `R > Q > σ(m)` are primes
  77. coprime to `m`, then `m * Q * R` is not abundant (in fact deficient). -/
  78. theorem not_abundant_kravitz_base {m Q R : ℕ} (hm : 0 < m)
  79. (hdef : ∑ d ∈ m.divisors, d + 2 ≤ 2 * m)
  80. (hQ : Q.Prime) (hR : R.Prime)
  81. (hmQ : m.Coprime Q) (hmR : m.Coprime R)
  82. (hσQ : ∑ d ∈ m.divisors, d < Q) (hQR : Q < R) :
  83. ¬ (m * Q * R).Abundant := by
  84. have hQR_ne : Q ≠ R := hQR.ne
  85. have hQR_cop : Q.Coprime R := (Nat.coprime_primes hQ hR).mpr hQR_ne
  86. have hmQ_R : (m * Q).Coprime R := hmR.mul_left hQR_cop
  87. have hσ_mul : ∑ d ∈ (m * Q * R).divisors, d =
  88. (∑ d ∈ m.divisors, d) * (Q + 1) * (R + 1) := by
  89. rw [hmQ_R.sum_divisors_mul, hmQ.sum_divisors_mul, hR.sum_divisors, hQ.sum_divisors]
  90. have hN_pos : 0 < m * Q * R := Nat.mul_pos (Nat.mul_pos hm hQ.pos) hR.pos
  91. unfold Nat.Abundant
  92. rw [← Nat.add_lt_add_iff_right (k := m * Q * R), ← Nat.sum_divisors_eq_sum_properDivisors_add_self]
  93. rw [hσ_mul]
  94. set S := ∑ d ∈ m.divisors, d
  95. have hm_ge1 : 1 ≤ S := by
  96. have h1 : 1 ∈ m.divisors := Nat.one_mem_divisors.mpr hm.ne'
  97. exact Finset.single_le_sum (f := fun i : ℕ => i) (fun _ _ => Nat.zero_le _) h1
  98. have hm_ge2 : 2 ≤ m := by omega
  99. have hm_le_S : m + 1 ≤ S := by
  100. have hsub : ({1, m} : Finset ℕ) ⊆ m.divisors := by
  101. intro x hx
  102. simp only [Finset.mem_insert, Finset.mem_singleton] at hx
  103. rcases hx with rfl | hxm
  104. · exact Nat.one_mem_divisors.mpr hm.ne'
  105. · rw [hxm]; exact Nat.mem_divisors.mpr ⟨dvd_refl m, hm.ne'⟩
  106. have hne : (1 : ℕ) ≠ m := by omega
  107. calc m + 1 = ∑ x ∈ ({1, m} : Finset ℕ), x := by rw [Finset.sum_pair hne]; omega
  108. _ ≤ S := Finset.sum_le_sum_of_subset_of_nonneg hsub (fun _ _ _ => Nat.zero_le _)
  109. have hQ_ne2 : Q ≠ 2 := by omega
  110. have hR_ne2 : R ≠ 2 := by omega
  111. have hQ_odd : Q % 2 = 1 := (Nat.Prime.eq_two_or_odd hQ).resolve_left hQ_ne2
  112. have hR_odd : R % 2 = 1 := (Nat.Prime.eq_two_or_odd hR).resolve_left hR_ne2
  113. have hR_ge : Q + 2 ≤ R := by omega
  114. have h1 : S * (Q + 1) + Q < 2 * m * Q := by nlinarith
  115. have h2 : S * (Q + 1) < Q * R := by nlinarith
  116. nlinarith
  117. /-- Under the hypotheses of `not_abundant_kravitz_base`, `m * Q * R` is not weird. -/
  118. theorem not_weird_kravitz_base {m Q R : ℕ} (hm : 0 < m)
  119. (hdef : ∑ d ∈ m.divisors, d + 2 ≤ 2 * m)
  120. (hQ : Q.Prime) (hR : R.Prime)
  121. (hmQ : m.Coprime Q) (hmR : m.Coprime R)
  122. (hσQ : ∑ d ∈ m.divisors, d < Q) (hQR : Q < R) :
  123. ¬ (m * Q * R).Weird :=
  124. fun hw => not_abundant_kravitz_base hm hdef hQ hR hmQ hmR hσQ hQR hw.1
  125. /-! ## 3. Theorem 1 (r ≤ 4): No odd squarefree number with `ω(n) ≤ 4` is abundant -/
  126. private lemma sqfree_sum_divisors_eq_prod {n : ℕ} (hn : n ≠ 0) (hsq : Squarefree n) :
  127. ∑ d ∈ n.divisors, d = ∏ p ∈ n.primeFactors, (p + 1) := by
  128. rw [Nat.sum_divisors hn]
  129. refine Finset.prod_congr rfl fun p hp => ?_
  130. have hfac : n.factorization p = 1 := by
  131. have hle := hsq.natFactorization_le_one p
  132. have hpos : 0 < n.factorization p :=
  133. Nat.Prime.factorization_pos_of_dvd (Nat.prime_of_mem_primeFactors hp) hn
  134. (Nat.dvd_of_mem_primeFactors hp)
  135. omega
  136. rw [hfac]
  137. simp [Finset.sum_range_succ, add_comm]
  138. private lemma odd_prime_ge_13_of_not_mem {p : ℕ} (hp : p.Prime) (hodd : Odd p)
  139. (hnot : p ∉ ({3, 5, 7, 11} : Finset ℕ)) : 13 ≤ p := by
  140. simp only [Finset.mem_insert, Finset.mem_singleton, not_or] at hnot
  141. have hp2 : p ≠ 2 := by
  142. rintro rfl
  143. revert hodd
  144. decide
  145. have hp_ge2 : 2 ≤ p := hp.two_le
  146. have hne4 : p ≠ 4 := fun h => by subst h; revert hp; decide
  147. have hne6 : p ≠ 6 := fun h => by subst h; revert hp; decide
  148. have hne8 : p ≠ 8 := fun h => by subst h; revert hp; decide
  149. have hne9 : p ≠ 9 := fun h => by subst h; revert hp; decide
  150. have hne10 : p ≠ 10 := fun h => by subst h; revert hp; decide
  151. have hne12 : p ≠ 12 := fun h => by subst h; revert hp; decide
  152. omega
  153. private lemma prod_abundancy_odd_le_four {S : Finset ℕ}
  154. (hS_prime : ∀ p ∈ S, p.Prime) (hS_odd : ∀ p ∈ S, Odd p) (hS_card : S.card ≤ 4) :
  155. ∏ p ∈ S, ((p + 1 : ℚ) / p) < 2 := by
  156. set P₀ : Finset ℕ := {3, 5, 7, 11}
  157. have hP₀_card : P₀.card = 4 := by decide
  158. have hcard_split : (S ∩ P₀).card + (S \ P₀).card = S.card :=
  159. Finset.card_inter_add_card_sdiff S P₀
  160. have hcard_split2 : (P₀ ∩ S).card + (P₀ \ S).card = P₀.card :=
  161. Finset.card_inter_add_card_sdiff P₀ S
  162. have hcard_le : (S \ P₀).card ≤ (P₀ \ S).card := by
  163. rw [Finset.inter_comm] at hcard_split2
  164. omega
  165. have h1 : ∏ p ∈ S \ P₀, ((p + 1 : ℚ) / p) ≤ (12 / 11 : ℚ) ^ (S \ P₀).card := by
  166. calc ∏ p ∈ S \ P₀, ((p + 1 : ℚ) / p)
  167. ≤ ∏ _p ∈ S \ P₀, (12 / 11 : ℚ) := by
  168. refine Finset.prod_le_prod (fun p hp => by positivity) (fun p hp => ?_)
  169. have hp_mem : p ∈ S := (Finset.mem_sdiff.mp hp).1
  170. have hp_not : p ∉ P₀ := (Finset.mem_sdiff.mp hp).2
  171. have hp13 : 13 ≤ p :=
  172. odd_prime_ge_13_of_not_mem (hS_prime p hp_mem) (hS_odd p hp_mem) hp_not
  173. have hpq : (13 : ℚ) ≤ (p : ℚ) := by exact_mod_cast hp13
  174. rw [div_le_div_iff₀ (by positivity) (by positivity)]
  175. linarith
  176. _ = (12 / 11 : ℚ) ^ (S \ P₀).card := by rw [Finset.prod_const]
  177. have h2 : (12 / 11 : ℚ) ^ (S \ P₀).card ≤ (12 / 11 : ℚ) ^ (P₀ \ S).card :=
  178. pow_le_pow_right₀ (by norm_num) hcard_le
  179. have h3 : (12 / 11 : ℚ) ^ (P₀ \ S).card ≤ ∏ p ∈ P₀ \ S, ((p + 1 : ℚ) / p) := by
  180. calc (12 / 11 : ℚ) ^ (P₀ \ S).card
  181. = ∏ _p ∈ P₀ \ S, (12 / 11 : ℚ) := by rw [Finset.prod_const]
  182. _ ≤ ∏ p ∈ P₀ \ S, ((p + 1 : ℚ) / p) := by
  183. refine Finset.prod_le_prod (fun _ _ => by norm_num) (fun p hp => ?_)
  184. have hp_mem : p ∈ P₀ := (Finset.mem_sdiff.mp hp).1
  185. fin_cases hp_mem <;> norm_num
  186. have h_main : ∏ p ∈ S, ((p + 1 : ℚ) / p) ≤ ∏ p ∈ P₀, ((p + 1 : ℚ) / p) := by
  187. rw [← Finset.prod_inter_mul_prod_sdiff S P₀, ← Finset.prod_inter_mul_prod_sdiff P₀ S,
  188. Finset.inter_comm P₀ S]
  189. refine mul_le_mul_of_nonneg_left (h1.trans (h2.trans h3)) ?_
  190. exact Finset.prod_nonneg (fun p _ => by positivity)
  191. have hP₀_val : ∏ p ∈ P₀, ((p + 1 : ℚ) / p) < 2 := by
  192. norm_num [P₀]
  193. exact h_main.trans_lt hP₀_val
  194. /-- Every odd squarefree `n` with `ω(n) ≤ 4` is not abundant. -/
  195. theorem not_abundant_odd_sqfree_omega_le_four {n : ℕ} (hodd : Odd n) (hsq : Squarefree n)
  196. (hcard : n.primeFactors.card ≤ 4) : ¬ n.Abundant := by
  197. have hn0 : n ≠ 0 := by rintro rfl; simp at hodd
  198. have hσ : ∑ d ∈ n.divisors, d = ∏ p ∈ n.primeFactors, (p + 1) :=
  199. sqfree_sum_divisors_eq_prod hn0 hsq
  200. have hn_prod : n = ∏ p ∈ n.primeFactors, p :=
  201. (Nat.prod_primeFactors_of_squarefree hsq).symm
  202. have hS_prime : ∀ p ∈ n.primeFactors, p.Prime := fun _ hp => Nat.prime_of_mem_primeFactors hp
  203. have hS_odd : ∀ p ∈ n.primeFactors, Odd p := fun p hp =>
  204. hodd.of_dvd_nat (Nat.dvd_of_mem_primeFactors hp)
  205. have hlt : ∏ p ∈ n.primeFactors, ((p + 1 : ℚ) / p) < 2 :=
  206. prod_abundancy_odd_le_four hS_prime hS_odd hcard
  207. have hprod_pos : (0 : ℚ) < ∏ p ∈ n.primeFactors, (p : ℚ) :=
  208. Finset.prod_pos fun p hp => by exact_mod_cast (hS_prime p hp).pos
  209. have hσ_rat : (∑ d ∈ n.divisors, (d : ℚ)) = ∏ p ∈ n.primeFactors, ((p : ℚ) + 1) := by
  210. have h : ((∑ d ∈ n.divisors, d : ℕ) : ℚ) = ((∏ p ∈ n.primeFactors, (p + 1) : ℕ) : ℚ) := by
  211. exact_mod_cast hσ
  212. simpa only [Nat.cast_sum, Nat.cast_prod, Nat.cast_add, Nat.cast_one] using h
  213. have hn_rat : (n : ℚ) = ∏ p ∈ n.primeFactors, (p : ℚ) := by
  214. have h : (n : ℚ) = ((∏ p ∈ n.primeFactors, p : ℕ) : ℚ) := by exact_mod_cast hn_prod
  215. simpa only [Nat.cast_prod] using h
  216. have hlt_mul : (∑ d ∈ n.divisors, (d : ℚ)) < 2 * (n : ℚ) := by
  217. calc (∑ d ∈ n.divisors, (d : ℚ))
  218. = ∏ p ∈ n.primeFactors, ((p : ℚ) + 1) := hσ_rat
  219. _ = (∏ p ∈ n.primeFactors, ((p + 1 : ℚ) / p)) * (∏ p ∈ n.primeFactors, (p : ℚ)) := by
  220. rw [← Finset.prod_mul_distrib]
  221. refine Finset.prod_congr rfl fun p hp => ?_
  222. have hp0 : (p : ℚ) ≠ 0 := by exact_mod_cast (hS_prime p hp).ne_zero
  223. rw [div_mul_cancel₀ _ hp0]
  224. _ < 2 * (∏ p ∈ n.primeFactors, (p : ℚ)) := mul_lt_mul_of_pos_right hlt hprod_pos
  225. _ = 2 * (n : ℚ) := by rw [← hn_rat]
  226. have hlt_nat : ∑ d ∈ n.divisors, d < 2 * n := by
  227. have h : ((∑ d ∈ n.divisors, d : ℕ) : ℚ) < ((2 * n : ℕ) : ℚ) := by
  228. simpa only [Nat.cast_sum, Nat.cast_mul, Nat.cast_ofNat] using hlt_mul
  229. exact_mod_cast h
  230. unfold Nat.Abundant
  231. rw [← Nat.add_lt_add_iff_right (k := n), ← Nat.sum_divisors_eq_sum_properDivisors_add_self]
  232. omega
  233. /-- Every odd squarefree `n` with `ω(n) ≤ 4` is not weird. -/
  234. theorem not_weird_odd_sqfree_omega_le_four {n : ℕ} (hodd : Odd n) (hsq : Squarefree n)
  235. (hcard : n.primeFactors.card ≤ 4) : ¬ n.Weird :=
  236. fun hw => not_abundant_odd_sqfree_omega_le_four hodd hsq hcard hw.1
  237. /-! ## 4. Smallest odd abundant number 945 is pseudoperfect, hence not weird -/
  238. theorem abundant_945 : Nat.Abundant 945 := by decide +kernel
  239. theorem pseudoperfect_945 : Nat.Pseudoperfect 945 := by
  240. refine ⟨by decide, ({1, 5, 7, 9, 15, 21, 35, 45, 63, 105, 135, 189, 315} : Finset ℕ),
  241. by decide, by decide⟩
  242. theorem not_weird_945 : ¬ Nat.Weird 945 :=
  243. fun hw => hw.2 pseudoperfect_945
  244. /-! ## 5. Lemma 3: Odd Divisor Gap-Free Coverage -/
  245. /-- Inductive gap-free coverage: if `d 0 = 1`, `d 1 = 3`, `d 2 = 5`, `d` is strictly
  246. increasing on `range m` (with `3 ≤ m`), and for every `3 ≤ k < m`,
  247. `d k ≤ S k - 2` and `d k ≠ S k - 4` (where `S k = ∑ i ∈ range k, d i`), then
  248. every `x ≤ S m` with `x ≠ 2` and `x ≠ S m - 2` is a subset sum of `(range m).image d`. -/
  249. theorem odd_divisor_gap_free_coverage {d : ℕ → ℕ} {m : ℕ} (hm : 3 ≤ m)
  250. (hd0 : d 0 = 1) (hd1 : d 1 = 3) (hd2 : d 2 = 5)
  251. (hmono : ∀ i j, i < j → j < m → d i < d j)
  252. (hstep : ∀ k, 3 ≤ k → k < m →
  253. d k ≤ (∑ i ∈ Finset.range k, d i) - 2 ∧ d k ≠ (∑ i ∈ Finset.range k, d i) - 4) :
  254. ∀ x ≤ ∑ i ∈ Finset.range m, d i, x ≠ 2 → x ≠ (∑ i ∈ Finset.range m, d i) - 2 →
  255. ∃ s ⊆ (Finset.range m).image d, ∑ i ∈ s, i = x := by
  256. suffices h : ∀ k, 3 ≤ k → k ≤ m →
  257. 9 ≤ ∑ i ∈ Finset.range k, d i ∧
  258. ∀ x ≤ ∑ i ∈ Finset.range k, d i, x ≠ 2 → x ≠ (∑ i ∈ Finset.range k, d i) - 2 →
  259. ∃ s ⊆ (Finset.range k).image d, ∑ i ∈ s, i = x from
  260. (h m hm le_rfl).2
  261. intro k hk3 hkm
  262. induction k, hk3 using Nat.le_induction with
  263. | base =>
  264. have hS3 : ∑ i ∈ Finset.range 3, d i = 9 := by
  265. simp [Finset.sum_range_succ, hd0, hd1, hd2]
  266. rw [hS3]
  267. refine ⟨le_rfl, fun x hx hne2 hne7 => ?_⟩
  268. have hD3 : ({1, 3, 5} : Finset ℕ) ⊆ (Finset.range 3).image d := by
  269. intro y hy
  270. simp only [Finset.mem_insert, Finset.mem_singleton] at hy
  271. rcases hy with rfl | rfl | rfl
  272. · exact Finset.mem_image.mpr ⟨0, by decide, hd0⟩
  273. · exact Finset.mem_image.mpr ⟨1, by decide, hd1⟩
  274. · exact Finset.mem_image.mpr ⟨2, by decide, hd2⟩
  275. rcases (by omega : x = 0 ∨ x = 1 ∨ x = 3 ∨ x = 4 ∨ x = 5 ∨ x = 6 ∨ x = 8 ∨ x = 9) with
  276. rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl
  277. · exact ⟨∅, Finset.empty_subset _, by decide⟩
  278. · exact ⟨{1}, Finset.Subset.trans (by decide) hD3, by decide⟩
  279. · exact ⟨{3}, Finset.Subset.trans (by decide) hD3, by decide⟩
  280. · exact ⟨{1, 3}, Finset.Subset.trans (by decide) hD3, by decide⟩
  281. · exact ⟨{5}, Finset.Subset.trans (by decide) hD3, by decide⟩
  282. · exact ⟨{1, 5}, Finset.Subset.trans (by decide) hD3, by decide⟩
  283. · exact ⟨{3, 5}, Finset.Subset.trans (by decide) hD3, by decide⟩
  284. · exact ⟨{1, 3, 5}, hD3, by decide⟩
  285. | succ k hk3 ih =>
  286. obtain ⟨hSk9, hIH⟩ := ih (Nat.le_of_succ_le hkm)
  287. have hkm_lt : k < m := hkm
  288. obtain ⟨hle, hne4⟩ := hstep k hk3 hkm_lt
  289. have hdk_ge5 : 5 ≤ d k := by
  290. have h2k : d 2 < d k := hmono 2 k (by omega) hkm_lt
  291. omega
  292. have hSk1 : ∑ i ∈ Finset.range (k + 1), d i = (∑ i ∈ Finset.range k, d i) + d k :=
  293. Finset.sum_range_succ d k
  294. have hdk_not_mem : d k ∉ (Finset.range k).image d := by
  295. intro hmem
  296. rw [Finset.mem_image] at hmem
  297. obtain ⟨j, hj, hjd⟩ := hmem
  298. rw [Finset.mem_range] at hj
  299. have := hmono j k hj hkm_lt
  300. omega
  301. refine ⟨by omega, fun x hx hne2 hne_top => ?_⟩
  302. rw [hSk1] at hx hne_top
  303. have hD_sub : (Finset.range k).image d ⊆ (Finset.range (k + 1)).image d :=
  304. Finset.image_subset_image (Finset.range_mono (Nat.le_succ k))
  305. have hdk_mem : d k ∈ (Finset.range (k + 1)).image d :=
  306. Finset.mem_image.mpr ⟨k, Finset.mem_range.mpr (Nat.lt_succ_self k), rfl⟩
  307. by_cases hx_le : x ≤ ∑ i ∈ Finset.range k, d i
  308. · by_cases hx_sub2 : x = (∑ i ∈ Finset.range k, d i) - 2
  309. · set y := (∑ i ∈ Finset.range k, d i) - 2 - d k
  310. have hy_le : y ≤ ∑ i ∈ Finset.range k, d i := by omega
  311. have hy_ne2 : y ≠ 2 := by omega
  312. have hy_ne_top : y ≠ (∑ i ∈ Finset.range k, d i) - 2 := by omega
  313. obtain ⟨s, hs, hsum⟩ := hIH y hy_le hy_ne2 hy_ne_top
  314. have hdk_notin_s : d k ∉ s := fun h => hdk_not_mem (hs h)
  315. refine ⟨insert (d k) s, Finset.insert_subset_iff.mpr ⟨hdk_mem, hs.trans hD_sub⟩, ?_⟩
  316. rw [Finset.sum_insert hdk_notin_s, hsum]
  317. omega
  318. · obtain ⟨s, hs, hsum⟩ := hIH x hx_le hne2 hx_sub2
  319. exact ⟨s, hs.trans hD_sub, hsum⟩
  320. · set y := x - d k
  321. have hy_le : y ≤ ∑ i ∈ Finset.range k, d i := by omega
  322. have hy_ne2 : y ≠ 2 := by omega
  323. have hy_ne_top : y ≠ (∑ i ∈ Finset.range k, d i) - 2 := by omega
  324. obtain ⟨s, hs, hsum⟩ := hIH y hy_le hy_ne2 hy_ne_top
  325. have hdk_notin_s : d k ∉ s := fun h => hdk_not_mem (hs h)
  326. refine ⟨insert (d k) s, Finset.insert_subset_iff.mpr ⟨hdk_mem, hs.trans hD_sub⟩, ?_⟩
  327. rw [Finset.sum_insert hdk_notin_s, hsum]
  328. omega
  329. /-- Consequently, if `n` is abundant with `A(n) = (∑ i ∈ n.properDivisors, i) - n`
  330. covered by `(range m).image d ⊆ n.properDivisors` (with `A(n) ≤ S_m`, `A(n) ∉ {2, S_m - 2}`),
  331. then `n` is pseudoperfect, hence not weird. -/
  332. theorem not_weird_of_gap_free_coverage {n : ℕ} {d : ℕ → ℕ} {m : ℕ} (hm : 3 ≤ m)
  333. (hsub : (Finset.range m).image d ⊆ n.properDivisors)
  334. (hd0 : d 0 = 1) (hd1 : d 1 = 3) (hd2 : d 2 = 5)
  335. (hmono : ∀ i j, i < j → j < m → d i < d j)
  336. (hstep : ∀ k, 3 ≤ k → k < m →
  337. d k ≤ (∑ i ∈ Finset.range k, d i) - 2 ∧ d k ≠ (∑ i ∈ Finset.range k, d i) - 4)
  338. (hab : n.Abundant)
  339. (hle : (∑ i ∈ n.properDivisors, i) - n ≤ ∑ i ∈ Finset.range m, d i)
  340. (hne2 : (∑ i ∈ n.properDivisors, i) - n ≠ 2)
  341. (hnetop : (∑ i ∈ n.properDivisors, i) - n ≠ (∑ i ∈ Finset.range m, d i) - 2) :
  342. ¬ n.Weird := by
  343. obtain ⟨s, hs, hsum⟩ := odd_divisor_gap_free_coverage hm hd0 hd1 hd2 hmono hstep
  344. ((∑ i ∈ n.properDivisors, i) - n) hle hne2 hnetop
  345. have hs_prop : s ⊆ n.properDivisors := hs.trans hsub
  346. have hcomp_sum : ∑ i ∈ (n.properDivisors \ s), i = n := by
  347. have h := Finset.sum_sdiff (f := fun i : ℕ => i) hs_prop
  348. unfold Nat.Abundant at hab
  349. omega
  350. have hpseudo : n.Pseudoperfect :=
  351. ⟨hab.pos, n.properDivisors \ s, Finset.sdiff_subset, hcomp_sum⟩
  352. exact fun hw => hw.2 hpseudo
  353. end Erdos470Partial