import Mathlib.Analysis.SpecialFunctions.Exp import Mathlib.Analysis.Complex.ExponentialBounds import Mathlib.Analysis.SpecialFunctions.Log.Basic import Mathlib.Analysis.SpecialFunctions.Pow.Real import Mathlib.Data.Nat.Prime.Basic import Mathlib.FieldTheory.Finite.Basic import Mathlib.Tactic.Linarith import Mathlib.Tactic.NormNum import Mathlib.Tactic.Positivity /-! A partial Lean companion to https://isitsolved.com/journal/41af26083503. The finite-range conclusion takes the analytic lower bound as a hypothesis. The prime-square progression lemma takes the two prime distribution estimates as hypotheses. The root count and absolute convergence use mathlib alone. Rebuild in a mathlib checkout at f897ebcf72cd16f89ab4577d0c826cd14afaafc7 (v4.24.0, Lean 4.24.0). From that checkout with its mathlib cache installed: lake env lean -DwarningAsError=true /absolute/path/to/PrimeRace.lean -/ noncomputable section namespace CubicPrimeRace /-- Count representatives of the cube roots of two modulo n. Used only at primes > 3. -/ def rootCount (n : ℕ) : ℕ := ((Finset.range n).filter fun a => a ^ 3 % n = 2 % n).card /-- A prime that is 2 modulo 3 gives a single root and hence no net contribution to the race. The inverse to cubing is an explicit power. -/ theorem rootCount_eq_one {p : ℕ} (hprime : p.Prime) (hmod : p % 3 = 2) : rootCount p = 1 := by letI : Fact p.Prime := ⟨hprime⟩ let k := 2 * (p / 3) + 1 have hexp : 3 * k = (p - 1) * 2 + 1 := by dsimp [k]; omega have hinv (z : ZMod p) : (z ^ 3) ^ k = z := by rw [← pow_mul, hexp, pow_succ, pow_mul] by_cases hz : z = 0 · simp [hz] · simp [ZMod.pow_card_sub_one_eq_one hz] let r : ZMod p := (2 : ZMod p) ^ k have hr : r ^ 3 = 2 := by simpa [r, ← pow_mul, Nat.mul_comm k 3] using hinv (2 : ZMod p) have hroot (a : ℕ) : a ^ 3 % p = 2 % p ↔ (a : ZMod p) ^ 3 = 2 := by constructor · intro he have hcast := congrArg (fun n : ℕ => (n : ZMod p)) he simpa using hcast · intro he have hcast : ((a ^ 3 : ℕ) : ZMod p) = (2 : ℕ) := by simpa using he simpa only [ZMod.val_natCast] using congrArg ZMod.val hcast unfold rootCount apply Finset.card_eq_one.mpr refine ⟨r.val, ?_⟩ ext a simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_singleton, hroot] constructor · rintro ⟨ha, he⟩ have heq : (a : ZMod p) = r := (hinv _).symm.trans (congrArg (fun z : ZMod p => z ^ k) he) simpa [ZMod.val_natCast_of_lt ha] using congrArg ZMod.val heq · rintro rfl exact ⟨r.val_lt, by simpa using hr⟩ /-- Chebyshev's theta restricted to primes in a single class modulo three. -/ def thetaModThree (a : ℕ) (v : ℝ) : ℝ := ∑ n ∈ (Finset.range (⌊v⌋₊ + 1)).filter (fun n => n.Prime ∧ n % 3 = a), Real.log n /-- The large-endpoint estimate in the height-92 audit, starting from the two explicit progression estimates of Bennett–Martin–O'Bryant–Rechnitzer. The logarithm subtracted here removes the prime 2 from the prime-square bound. -/ theorem square_progressions_of_BMOR {v : ℝ} (hv : (10 : ℝ)^16 ≤ v) (h₁ : |thetaModThree 1 v - v / 2| ≤ v / (840 * Real.log v)) (h₂ : |thetaModThree 2 v - v / 2| ≤ v / (840 * Real.log v)) : 0.4999 * v < 2 * thetaModThree 2 v - thetaModThree 1 v - 2 * Real.log 2 := by have hlog : 36 ≤ Real.log v := by have h := Real.log_le_log (by positivity : (0 : ℝ) < 2^53) ((by norm_num : (2 : ℝ)^53 ≤ 10^16).trans hv) rw [Real.log_pow] at h norm_num at h linarith [Real.log_two_gt_d9] have he : v / (840 * Real.log v) ≤ v / 30240 := div_le_div_of_nonneg_left ((by norm_num : (0 : ℝ) ≤ 10^16).trans hv) (by norm_num) (by linarith) linarith [(abs_le.mp h₁).2, (abs_le.mp h₂).1, Real.log_two_lt_d9] /-- The no-root side has coefficient 1; the three-root side has coefficient -2. -/ def coefficient (n : ℕ) : ℝ := if n.Prime ∧ 3 < n then 1 - (rootCount n : ℝ) else 0 def term (X : ℝ) (n : ℕ) : ℝ := coefficient n * Real.log n * Real.exp (-(n : ℝ) / X) def race (X : ℝ) : ℝ := ∑' n : ℕ, term X n private theorem coefficient_bound (n : ℕ) : |coefficient n| ≤ (n : ℝ) + 1 := by have h : rootCount n ≤ n := by simpa [rootCount] using ((Finset.range n).card_filter_le (fun a => a ^ 3 % n = 2 % n)) have hc : (rootCount n : ℝ) ≤ n := by exact_mod_cast h unfold coefficient split_ifs · apply abs_le.mpr constructor <;> linarith [Nat.cast_nonneg n (α := ℝ), Nat.cast_nonneg (rootCount n) (α := ℝ)] · rw [abs_zero] positivity /-- Absolute convergence of the actual prime race at every positive scale. -/ theorem summable_abs_term {X : ℝ} (hX : 0 < X) : Summable fun n : ℕ => |term X n| := by have h := (Real.summable_pow_mul_exp_neg_nat_mul 2 (inv_pos.mpr hX)).add (Real.summable_pow_mul_exp_neg_nat_mul 1 (inv_pos.mpr hX)) refine h.of_nonneg_of_le (fun n => abs_nonneg _) (fun n => ?_) change |term X n| ≤ (n : ℝ)^2 * _ + (n : ℝ)^1 * _ have hlog : 0 ≤ Real.log (n : ℝ) := by cases n with | zero => simp | succ n => exact Real.log_nonneg (by exact_mod_cast Nat.succ_pos n) have he : -(n : ℝ) / X = -X⁻¹ * n := by ring rw [term, abs_mul, abs_mul, abs_of_nonneg hlog, abs_of_pos (Real.exp_pos _), he] calc _ ≤ (((n : ℝ) + 1) * n) * Real.exp (-X⁻¹ * n) := by gcongr · exact coefficient_bound n · exact Real.log_le_self (Nat.cast_nonneg n) _ = _ := by ring /-- The height-92 audit's normalized analytic bound, with the unconditional B=1. The term 0.4430 already absorbs the small-endpoint loss in the square sum. -/ def lowerEnvelope (X : ℝ) : ℝ := 0.4430 - 0.2754 - (4.77 / 10^58) * Real.sqrt X - 560 * X ^ (-(1/8 : ℝ)) - 100 / X /-- Bound the envelope on the entire real interval, including both endpoints. -/ theorem lowerEnvelope_gt {X : ℝ} (hmin : (10 : ℝ)^50 ≤ X) (hmax : X ≤ (10 : ℝ)^110) : 0.16 < lowerEnvelope X := by have hs : Real.sqrt X ≤ (10 : ℝ)^55 := by apply (Real.sqrt_le_iff).2 exact ⟨by positivity, hmax.trans_eq (by norm_num)⟩ have hp : (10 : ℝ)^6 ≤ X ^ (1/8 : ℝ) := by calc _ = (((10 : ℝ)^6)^8) ^ (1/8 : ℝ) := by rw [← Real.rpow_natCast_mul (by positivity)] norm_num _ ≤ X ^ (1/8 : ℝ) := Real.rpow_le_rpow (by positivity) ((by norm_num : ((10 : ℝ)^6)^8 ≤ (10 : ℝ)^50).trans hmin) (by norm_num) have invp : X ^ (-(1/8 : ℝ)) ≤ ((10 : ℝ)^6)⁻¹ := by rw [Real.rpow_neg ((by norm_num : (0:ℝ) ≤ 10^50).trans hmin)] exact inv_anti₀ (by positivity) hp have hdiv : 100 / X ≤ (100 : ℝ) / 10^50 := div_le_div_of_nonneg_left (by norm_num) (by positivity) hmin unfold lowerEnvelope linarith /-- The public bound follows if the explicit-formula envelope is supplied for the very same prime sum. This theorem makes that analytic dependency explicit. -/ theorem finite_range_of_envelope {X : ℝ} (hmin : (10 : ℝ)^50 ≤ X) (hmax : X ≤ (10 : ℝ)^110) (hanalytic : lowerEnvelope X ≤ race X / Real.sqrt X) : 0.09 * Real.sqrt X ≤ race X := by have hX : 0 < X := (by norm_num : (0 : ℝ) < 10^50).trans_le hmin apply (le_div_iff₀ (Real.sqrt_pos.2 hX)).mp linarith [lowerEnvelope_gt hmin hmax] end CubicPrimeRace #print axioms CubicPrimeRace.rootCount_eq_one #print axioms CubicPrimeRace.square_progressions_of_BMOR #print axioms CubicPrimeRace.summable_abs_term #print axioms CubicPrimeRace.lowerEnvelope_gt #print axioms CubicPrimeRace.finite_range_of_envelope