import Mathlib.Analysis.SpecialFunctions.Expimport Mathlib.Analysis.Complex.ExponentialBoundsimport Mathlib.Analysis.SpecialFunctions.Log.Basicimport Mathlib.Analysis.SpecialFunctions.Pow.Realimport Mathlib.Data.Nat.Prime.Basicimport Mathlib.FieldTheory.Finite.Basicimport Mathlib.Tactic.Linarithimport Mathlib.Tactic.NormNumimport 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 estimatesas 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 sectionnamespace 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 netcontribution 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 := byletI : Fact p.Prime := ⟨hprime⟩let k := 2 * (p / 3) + 1have hexp : 3 * k = (p - 1) * 2 + 1 := by dsimp [k]; omegahave hinv (z : ZMod p) : (z ^ 3) ^ k = z := byrw [← 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) ^ khave hr : r ^ 3 = 2 := bysimpa [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 := byconstructor· intro hehave hcast := congrArg (fun n : ℕ => (n : ZMod p)) hesimpa using hcast· intro hehave hcast : ((a ^ 3 : ℕ) : ZMod p) = (2 : ℕ) := by simpa using hesimpa only [ZMod.val_natCast] using congrArg ZMod.val hcastunfold rootCountapply Finset.card_eq_one.mprrefine ⟨r.val, ?_⟩ext asimp 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 rflexact ⟨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 thetwo 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 := byhave hlog : 36 ≤ Real.log v := byhave h := Real.log_le_log (by positivity : (0 : ℝ) < 2^53)((by norm_num : (2 : ℝ)^53 ≤ 10^16).trans hv)rw [Real.log_pow] at hnorm_num at hlinarith [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 0def term (X : ℝ) (n : ℕ) : ℝ :=coefficient n * Real.log n * Real.exp (-(n : ℝ) / X)def race (X : ℝ) : ℝ := ∑' n : ℕ, term X nprivate theorem coefficient_bound (n : ℕ) : |coefficient n| ≤ (n : ℝ) + 1 := byhave h : rootCount n ≤ n := bysimpa [rootCount] using ((Finset.range n).card_filter_le (fun a => a ^ 3 % n = 2 % n))have hc : (rootCount n : ℝ) ≤ n := by exact_mod_cast hunfold coefficientsplit_ifs· apply abs_le.mprconstructor <;> 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| := byhave 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 : ℝ) := bycases 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 ringrw [term, abs_mul, abs_mul, abs_of_nonneg hlog, abs_of_pos (Real.exp_pos _), he]calc_ ≤ (((n : ℝ) + 1) * n) * Real.exp (-X⁻¹ * n) := bygcongr· 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 := byhave hs : Real.sqrt X ≤ (10 : ℝ)^55 := byapply (Real.sqrt_le_iff).2exact ⟨by positivity, hmax.trans_eq (by norm_num)⟩have hp : (10 : ℝ)^6 ≤ X ^ (1/8 : ℝ) := bycalc_ = (((10 : ℝ)^6)^8) ^ (1/8 : ℝ) := byrw [← 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)⁻¹ := byrw [Real.rpow_neg ((by norm_num : (0:ℝ) ≤ 10^50).trans hmin)]exact inv_anti₀ (by positivity) hphave hdiv : 100 / X ≤ (100 : ℝ) / 10^50 :=div_le_div_of_nonneg_left (by norm_num) (by positivity) hminunfold lowerEnvelopelinarith/-- The public bound follows if the explicit-formula envelope is supplied forthe 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 := byhave hX : 0 < X := (by norm_num : (0 : ℝ) < 10^50).trans_le hminapply (le_div_iff₀ (Real.sqrt_pos.2 hX)).mplinarith [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