/-Copyright (c) 2026 Carles MarΓn. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles MarΓn, Claude (AI assistant)-/module public import Mathlib.Analysis.SpecialFunctions.Pow.Realpublic import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basicpublic import Mathlib.NumberTheory.PrimeCountingpublic import Mathlib.NumberTheory.Wilsonpublic import Mathlib.NumberTheory.Bertrand /-!# Willans' formula for the n-th prime The `n`-th prime is `1 + Ξ£_{i=1}^{2^n} β(n / Ξ£_{j=1}^{i} βcosΒ² (Ο ((j - 1)! + 1) / j)β)^{1/n}β`(Willans, 1964). By Wilson's theorem (Mathlib's `Nat.prime_iff_fac_equiv_neg_one`) the inner flooris `1` exactly when `j = 1` or `j` is prime, so the inner sum is `Ο(i) + 1`; the outer floor is `1`when `Ο(i) < n` and `0` otherwise (bounding `y^n < 2^n` instead of `n^{1/n}`), and Bertrand'spostulate gives `p_n β€ 2^n`, so the outer sum counts `1 β€ i < p_n`. ## Main results * `Willans.floor_cos_sq_eq`: floor(cos^2(pi((j-1)!+1)/j)) is 1 iff j = 1 or j is prime (Wilson)* `Willans.nth_prime_eq`: Willans' formula for the n-th prime ## References * C. P. Willans, *On formulae for the nth prime number*, Math. Gazette 48 (1964), 413β415.-/ @[expose] public section open Finset namespace Willans /-- **Wilson floor.** For `j β₯ 1`, `βcosΒ²(Ο((j-1)!+1)/j)β` is `1` if `j = 1` or `j` is prime,and `0` otherwise. -/theorem floor_cos_sq_eq (j : β) (hj : 1 β€ j) : βReal.cos (Real.pi * (((j - 1).factorial : β) + 1) / j) ^ 2β = if j = 1 β¨ j.Prime then 1 else 0 := by have hj0 : (j : β) β 0 := by positivity have key : j β£ (j - 1).factorial + 1 β (j = 1 β¨ j.Prime) := by by_cases h1 : j = 1 Β· subst h1; simp Β· rw [Nat.prime_iff_fac_equiv_neg_one h1, β ZMod.natCast_eq_zero_iff] push_cast simp [h1, eq_neg_iff_add_eq_zero] have hsin : Real.sin (Real.pi * (((j - 1).factorial : β) + 1) / j) = 0 β j β£ (j - 1).factorial + 1 := by rw [Real.sin_eq_zero_iff] constructor Β· rintro β¨n, hnβ© have h2 : (n : β) * j = (((j - 1).factorial : β) + 1) := by have := Real.pi_pos field_simp at hn nlinarith [hn] have h3 : n * (j : β€) = (((j - 1).factorial + 1 : β) : β€) := by exact_mod_cast h2 have : (j : β€) β£ (((j - 1).factorial + 1 : β) : β€) := β¨n, by rw [β h3]; ringβ© exact_mod_cast this Β· rintro β¨k, hkβ© refine β¨k, ?_β© have : (((j - 1).factorial : β) + 1) = j * k := by exact_mod_cast hk rw [this]; push_cast; field_simp have hc : Real.cos (Real.pi * (((j - 1).factorial : β) + 1) / j) ^ 2 = 1 - Real.sin (Real.pi * (((j - 1).factorial : β) + 1) / j) ^ 2 := by rw [Real.cos_sq'] split_ifs with h Β· rw [hc, (hsin.mpr (key.mpr h)), Int.floor_eq_iff]; norm_num Β· have hne : Real.sin (Real.pi * (((j - 1).factorial : β) + 1) / j) β 0 := fun h0 => h (key.mp (hsin.mp h0)) rw [Int.floor_eq_iff] refine β¨by simp; positivity, ?_β© rw [hc] have := pow_pos (abs_pos.mpr hne) 2 rw [sq_abs] at this push_cast; linarith open scoped Nat.Prime /-- Step 2: the inner sum is `Ο(i) + 1`. -/theorem sum_floor_eq (i : β) (hi : 1 β€ i) : β j β Icc (1 : β) i, (βReal.cos (Real.pi * (((j - 1).factorial : β) + 1) / j) ^ 2β : β) = ((Ο i + 1 : β) : β) := by induction i, hi using Nat.le_induction with | base => rw [Finset.Icc_self, Finset.sum_singleton, floor_cos_sq_eq 1 le_rfl] simp [Nat.primeCounting_one] | succ i hi ih => rw [Finset.sum_Icc_succ_top (by omega), ih, floor_cos_sq_eq (i + 1) (by omega)] have h1 : Ο (i + 1) = Ο i + if (i + 1).Prime then 1 else 0 := by show Nat.count Nat.Prime (i + 1 + 1) = Nat.count Nat.Prime (i + 1) + _ rw [Nat.count_succ] rw [h1] have h3 : i β 0 := by omega by_cases hp : (i + 1).Prime <;> simp [hp, h3] /-- Step 3: the `n`-th root floor is an indicator. -/theorem floor_root_eq (n k : β) (hn : 1 β€ n) : β((n : β) / ((k + 1 : β) : β)) ^ ((1 : β) / n)β = if k < n then 1 else 0 := by have hn0 : (0 : β) < n := by exact_mod_cast hn have hk0 : (0 : β) < ((k + 1 : β) : β) := by positivity have hx0 : 0 β€ (n : β) / ((k + 1 : β) : β) := by positivity split_ifs with h Β· have hx1 : 1 β€ (n : β) / ((k + 1 : β) : β) := by rw [le_div_iffβ hk0]; simp only [one_mul]; exact_mod_cast h have hxn : (n : β) / ((k + 1 : β) : β) β€ n := by rw [div_le_iffβ hk0]; push_cast; nlinarith [hn0, (by positivity : (0 : β) β€ k)] rw [Int.floor_eq_iff] refine β¨by simpa using Real.one_le_rpow hx1 (by positivity), ?_β© set y := ((n : β) / ((k + 1 : β) : β)) ^ ((1 : β) / n) with hy have hyn : y ^ n = (n : β) / ((k + 1 : β) : β) := by rw [hy, β Real.rpow_natCast, β Real.rpow_mul hx0, one_div_mul_cancel hn0.ne', Real.rpow_one] by_contra hcon push Not at hcon have h2 : (2 : β) ^ n β€ y ^ n := pow_le_pow_leftβ (by norm_num) (by push_cast at hcon; linarith) n have h3 : (n : β) < 2 ^ n := by exact_mod_cast Nat.lt_two_pow_self linarith Β· push Not at h have hx1 : (n : β) / ((k + 1 : β) : β) < 1 := by rw [div_lt_one hk0]; exact_mod_cast Nat.lt_succ_of_le h rw [Int.floor_eq_iff] refine β¨by simpa using Real.rpow_nonneg hx0 _, ?_β© have := Real.rpow_lt_one hx0 hx1 (by positivity : (0 : β) < 1 / n) simpa using this /-- `p_k β€ 2^(k+1)` for the `k`-th prime indexed from 0. -/theorem nth_prime_le (k : β) : Nat.nth Nat.Prime k β€ 2 ^ (k + 1) := by induction k with | zero => simp [Nat.nth_prime_zero_eq_two] | succ k ih => have hp := Nat.prime_nth_prime k obtain β¨q, hq, hlt, hleβ© := Nat.exists_prime_lt_and_le_two_mul (Nat.nth Nat.Prime k) hp.ne_zero have : Nat.nth Nat.Prime (k + 1) β€ q := (Nat.nth_add_one_le_iff Nat.infinite_setOfPred_prime hq).mpr hlt calc Nat.nth Nat.Prime (k + 1) β€ q := this _ β€ 2 * Nat.nth Nat.Prime k := hle _ β€ 2 * 2 ^ (k + 1) := by omega _ = 2 ^ (k + 1 + 1) := by ring /-- **Willans' formula** for the `n`-th prime. -/theorem nth_prime_eq (n : β) (hn : 1 β€ n) : (Nat.nth Nat.Prime (n - 1) : β) = 1 + β i β Finset.Icc (1 : β) (2 ^ n), (β((n : β) / β j β Finset.Icc (1 : β) i, (βReal.cos (Real.pi * (((j - 1).factorial : β) + 1) / j) ^ 2β : β)) ^ ((1 : β) / n)β : β) := by set p := Nat.nth Nat.Prime (n - 1) with hpdef have hp2 : 2 β€ p := (Nat.prime_nth_prime _).two_le have hpb : p β€ 2 ^ n := by have := nth_prime_le (n - 1) rwa [Nat.sub_add_cancel hn] at this have hterm : β i β Icc (1 : β) (2 ^ n), (β((n : β) / β j β Finset.Icc (1 : β) i, (βReal.cos (Real.pi * (((j - 1).factorial : β) + 1) / j) ^ 2β : β)) ^ ((1 : β) / n)β : β) = if i < p then 1 else 0 := by intro i hi rw [mem_Icc] at hi rw [sum_floor_eq i hi.1, floor_root_eq n (Ο i) hn] have hiff : Ο i < n β i < p := by have := Nat.lt_nth_iff_count_lt Nat.infinite_setOfPred_prime (a := n - 1) (b := i + 1) change n - 1 < Ο i β p < i + 1 at this omega by_cases h : i < p Β· simp [h, hiff.mpr h] Β· simp [h, (not_congr hiff).mpr h] rw [Finset.sum_congr rfl hterm, β Finset.sum_filter] simp only [Finset.sum_const, nsmul_eq_mul, mul_one] have : (Icc (1 : β) (2 ^ n)).filter (Β· < p) = Icc 1 (p - 1) := by ext i; simp only [mem_filter, mem_Icc]; omega rw [this, Nat.card_Icc] have : (p - 1 + 1 - 1 : β) = p - 1 := by omega rw [this] have : ((p - 1 : β) : β) = (p : β) - 1 := by rw [Nat.cast_sub (by omega)]; simp rw [this]; ring end Willans