Lean / Reader
Lean reader

PrimeRace.lean

170 lines12 declarationsno sorryimports Mathlib.Analysis.SpecialFunctions.Exp Mathlib.Analysis.Complex.ExponentialBounds Mathlib.Analysis.SpecialFunctions.Log.Basic Mathlib.Analysis.SpecialFunctions.Pow.Real Mathlib.Data.Nat.Prime.Basic Mathlib.FieldTheory.Finite.Basic +3
  1. import Mathlib.Analysis.SpecialFunctions.Exp
  2. import Mathlib.Analysis.Complex.ExponentialBounds
  3. import Mathlib.Analysis.SpecialFunctions.Log.Basic
  4. import Mathlib.Analysis.SpecialFunctions.Pow.Real
  5. import Mathlib.Data.Nat.Prime.Basic
  6. import Mathlib.FieldTheory.Finite.Basic
  7. import Mathlib.Tactic.Linarith
  8. import Mathlib.Tactic.NormNum
  9. import Mathlib.Tactic.Positivity
  10. /-! A partial Lean companion to https://isitsolved.com/journal/41af26083503.
  11. The finite-range conclusion takes the analytic lower bound as a hypothesis.
  12. The prime-square progression lemma takes the two prime distribution estimates
  13. as hypotheses. The root count and absolute convergence use mathlib alone.
  14. Rebuild in a mathlib checkout at f897ebcf72cd16f89ab4577d0c826cd14afaafc7
  15. (v4.24.0, Lean 4.24.0). From that checkout with its mathlib cache installed:
  16. lake env lean -DwarningAsError=true /absolute/path/to/PrimeRace.lean
  17. -/
  18. noncomputable section
  19. namespace CubicPrimeRace
  20. /-- Count representatives of the cube roots of two modulo n. Used only at primes > 3. -/
  21. def rootCount (n : ℕ) : ℕ :=
  22. ((Finset.range n).filter fun a => a ^ 3 % n = 2 % n).card
  23. /-- A prime that is 2 modulo 3 gives a single root and hence no net
  24. contribution to the race. The inverse to cubing is an explicit power. -/
  25. theorem rootCount_eq_one {p : ℕ} (hprime : p.Prime) (hmod : p % 3 = 2) :
  26. rootCount p = 1 := by
  27. letI : Fact p.Prime := ⟨hprime⟩
  28. let k := 2 * (p / 3) + 1
  29. have hexp : 3 * k = (p - 1) * 2 + 1 := by dsimp [k]; omega
  30. have hinv (z : ZMod p) : (z ^ 3) ^ k = z := by
  31. rw [← pow_mul, hexp, pow_succ, pow_mul]
  32. by_cases hz : z = 0
  33. · simp [hz]
  34. · simp [ZMod.pow_card_sub_one_eq_one hz]
  35. let r : ZMod p := (2 : ZMod p) ^ k
  36. have hr : r ^ 3 = 2 := by
  37. simpa [r, ← pow_mul, Nat.mul_comm k 3] using hinv (2 : ZMod p)
  38. have hroot (a : ℕ) : a ^ 3 % p = 2 % p ↔ (a : ZMod p) ^ 3 = 2 := by
  39. constructor
  40. · intro he
  41. have hcast := congrArg (fun n : ℕ => (n : ZMod p)) he
  42. simpa using hcast
  43. · intro he
  44. have hcast : ((a ^ 3 : ℕ) : ZMod p) = (2 : ℕ) := by simpa using he
  45. simpa only [ZMod.val_natCast] using congrArg ZMod.val hcast
  46. unfold rootCount
  47. apply Finset.card_eq_one.mpr
  48. refine ⟨r.val, ?_⟩
  49. ext a
  50. simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_singleton, hroot]
  51. constructor
  52. · rintro ⟨ha, he⟩
  53. have heq : (a : ZMod p) = r := (hinv _).symm.trans (congrArg (fun z : ZMod p => z ^ k) he)
  54. simpa [ZMod.val_natCast_of_lt ha] using congrArg ZMod.val heq
  55. · rintro rfl
  56. exact ⟨r.val_lt, by simpa using hr⟩
  57. /-- Chebyshev's theta restricted to primes in a single class modulo three. -/
  58. def thetaModThree (a : ℕ) (v : ℝ) : ℝ :=
  59. ∑ n ∈ (Finset.range (⌊v⌋₊ + 1)).filter (fun n => n.Prime ∧ n % 3 = a), Real.log n
  60. /-- The large-endpoint estimate in the height-92 audit, starting from the
  61. two explicit progression estimates of Bennett–Martin–O'Bryant–Rechnitzer.
  62. The logarithm subtracted here removes the prime 2 from the prime-square bound. -/
  63. theorem square_progressions_of_BMOR {v : ℝ} (hv : (10 : ℝ)^16 ≤ v)
  64. (h₁ : |thetaModThree 1 v - v / 2| ≤ v / (840 * Real.log v))
  65. (h₂ : |thetaModThree 2 v - v / 2| ≤ v / (840 * Real.log v)) :
  66. 0.4999 * v < 2 * thetaModThree 2 v - thetaModThree 1 v - 2 * Real.log 2 := by
  67. have hlog : 36 ≤ Real.log v := by
  68. have h := Real.log_le_log (by positivity : (0 : ℝ) < 2^53)
  69. ((by norm_num : (2 : ℝ)^53 ≤ 10^16).trans hv)
  70. rw [Real.log_pow] at h
  71. norm_num at h
  72. linarith [Real.log_two_gt_d9]
  73. have he : v / (840 * Real.log v) ≤ v / 30240 :=
  74. div_le_div_of_nonneg_left ((by norm_num : (0 : ℝ) ≤ 10^16).trans hv)
  75. (by norm_num) (by linarith)
  76. linarith [(abs_le.mp h₁).2, (abs_le.mp h₂).1, Real.log_two_lt_d9]
  77. /-- The no-root side has coefficient 1; the three-root side has coefficient -2. -/
  78. def coefficient (n : ℕ) : ℝ :=
  79. if n.Prime ∧ 3 < n then 1 - (rootCount n : ℝ) else 0
  80. def term (X : ℝ) (n : ℕ) : ℝ :=
  81. coefficient n * Real.log n * Real.exp (-(n : ℝ) / X)
  82. def race (X : ℝ) : ℝ := ∑' n : ℕ, term X n
  83. private theorem coefficient_bound (n : ℕ) : |coefficient n| ≤ (n : ℝ) + 1 := by
  84. have h : rootCount n ≤ n := by
  85. simpa [rootCount] using ((Finset.range n).card_filter_le (fun a => a ^ 3 % n = 2 % n))
  86. have hc : (rootCount n : ℝ) ≤ n := by exact_mod_cast h
  87. unfold coefficient
  88. split_ifs
  89. · apply abs_le.mpr
  90. constructor <;> linarith [Nat.cast_nonneg n (α := ℝ), Nat.cast_nonneg (rootCount n) (α := ℝ)]
  91. · rw [abs_zero]
  92. positivity
  93. /-- Absolute convergence of the actual prime race at every positive scale. -/
  94. theorem summable_abs_term {X : ℝ} (hX : 0 < X) : Summable fun n : ℕ => |term X n| := by
  95. have h := (Real.summable_pow_mul_exp_neg_nat_mul 2 (inv_pos.mpr hX)).add
  96. (Real.summable_pow_mul_exp_neg_nat_mul 1 (inv_pos.mpr hX))
  97. refine h.of_nonneg_of_le (fun n => abs_nonneg _) (fun n => ?_)
  98. change |term X n| ≤ (n : ℝ)^2 * _ + (n : ℝ)^1 * _
  99. have hlog : 0 ≤ Real.log (n : ℝ) := by
  100. cases n with
  101. | zero => simp
  102. | succ n => exact Real.log_nonneg (by exact_mod_cast Nat.succ_pos n)
  103. have he : -(n : ℝ) / X = -X⁻¹ * n := by ring
  104. rw [term, abs_mul, abs_mul, abs_of_nonneg hlog, abs_of_pos (Real.exp_pos _), he]
  105. calc
  106. _ ≤ (((n : ℝ) + 1) * n) * Real.exp (-X⁻¹ * n) := by
  107. gcongr
  108. · exact coefficient_bound n
  109. · exact Real.log_le_self (Nat.cast_nonneg n)
  110. _ = _ := by ring
  111. /-- The height-92 audit's normalized analytic bound, with the unconditional B=1.
  112. The term 0.4430 already absorbs the small-endpoint loss in the square sum. -/
  113. def lowerEnvelope (X : ℝ) : ℝ :=
  114. 0.4430 - 0.2754 - (4.77 / 10^58) * Real.sqrt X
  115. - 560 * X ^ (-(1/8 : ℝ)) - 100 / X
  116. /-- Bound the envelope on the entire real interval, including both endpoints. -/
  117. theorem lowerEnvelope_gt {X : ℝ} (hmin : (10 : ℝ)^50 ≤ X) (hmax : X ≤ (10 : ℝ)^110) :
  118. 0.16 < lowerEnvelope X := by
  119. have hs : Real.sqrt X ≤ (10 : ℝ)^55 := by
  120. apply (Real.sqrt_le_iff).2
  121. exact ⟨by positivity, hmax.trans_eq (by norm_num)⟩
  122. have hp : (10 : ℝ)^6 ≤ X ^ (1/8 : ℝ) := by
  123. calc
  124. _ = (((10 : ℝ)^6)^8) ^ (1/8 : ℝ) := by
  125. rw [← Real.rpow_natCast_mul (by positivity)]
  126. norm_num
  127. _ ≤ X ^ (1/8 : ℝ) :=
  128. Real.rpow_le_rpow (by positivity) ((by norm_num : ((10 : ℝ)^6)^8 ≤ (10 : ℝ)^50).trans hmin)
  129. (by norm_num)
  130. have invp : X ^ (-(1/8 : ℝ)) ≤ ((10 : ℝ)^6)⁻¹ := by
  131. rw [Real.rpow_neg ((by norm_num : (0:ℝ) ≤ 10^50).trans hmin)]
  132. exact inv_anti₀ (by positivity) hp
  133. have hdiv : 100 / X ≤ (100 : ℝ) / 10^50 :=
  134. div_le_div_of_nonneg_left (by norm_num) (by positivity) hmin
  135. unfold lowerEnvelope
  136. linarith
  137. /-- The public bound follows if the explicit-formula envelope is supplied for
  138. the very same prime sum. This theorem makes that analytic dependency explicit. -/
  139. theorem finite_range_of_envelope {X : ℝ}
  140. (hmin : (10 : ℝ)^50 ≤ X) (hmax : X ≤ (10 : ℝ)^110)
  141. (hanalytic : lowerEnvelope X ≤ race X / Real.sqrt X) :
  142. 0.09 * Real.sqrt X ≤ race X := by
  143. have hX : 0 < X := (by norm_num : (0 : ℝ) < 10^50).trans_le hmin
  144. apply (le_div_iff₀ (Real.sqrt_pos.2 hX)).mp
  145. linarith [lowerEnvelope_gt hmin hmax]
  146. end CubicPrimeRace
  147. #print axioms CubicPrimeRace.rootCount_eq_one
  148. #print axioms CubicPrimeRace.square_progressions_of_BMOR
  149. #print axioms CubicPrimeRace.summable_abs_term
  150. #print axioms CubicPrimeRace.lowerEnvelope_gt
  151. #print axioms CubicPrimeRace.finite_range_of_envelope