/-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 Formalizations.Combinatorics.Choose.MiddleBinomialDivisorspublic import Mathlib.Algebra.BigOperators.Group.Finset.Basicpublic import Mathlib.Algebra.Order.BigOperators.Group.Finsetpublic import Mathlib.Analysis.SpecificLimits.Basicpublic import Mathlib.Data.Nat.Choose.Centralpublic import Mathlib.Data.Nat.Countpublic import Mathlib.Data.Nat.Factorization.Basicpublic import Mathlib.Tactic.Ringpublic import Mathlib.Tactic.Positivitypublic import Mathlib.NumberTheory.Padics.PadicVal.Basicpublic import Mathlib.Order.Filter.AtTopBot.Basicpublic import Mathlib.Topology.Instances.Real.Lemmas /-!# `n + k` usually divides `C(2n, n)` (Pomerance, Theorem 2) C. Pomerance, *Divisors of the middle binomial coefficient*, Amer. Math. Monthly 122 (2015) 636β644,Theorem 2: for every positive integer `k`, the set of `n` with `n + k β£ C(2n, n)` has asymptoticdensity `1`. For primes `p β₯ 2k` the power of `p` in `n + k` always divides `C(2n, n)` (inequality (4),`MiddleBinomial.padicValNat_add_le`); for each of the finitely many primes `p < 2k`, the `n` where itfails are few, because `C(2n, n)` has many factors `p` for most `n` (Lemma 2 of the paper: few `n β€ x`have few carries in base `p`) and few `n β€ x` have `p^j β£ n + k` with `j` large. ## Main results * `MiddleBinomial.tendsto_density_add_dvd_centralBinom`: for k >= 1 the proportion of n < x with n + k dividing C(2n, n) tends to 1 (Theorem 2) ## References * C. Pomerance, Divisors of the middle binomial coefficient, Amer. Math. Monthly 122 (2015) 636β644, doi:10.4169/amer.math.monthly.122.7.636.-/ @[expose] public section namespace MiddleBinomial open Nat Filter Topology /-- Kummer for the central binomial coefficient (copy of the private lemma of`MiddleBinomialDivisors`). -/private lemma central_val' {p : β} [Fact p.Prime] (n b : β) (hb : Nat.log p (2 * n) < b) : padicValNat p (centralBinom n) = (Finset.filter (fun i => p ^ i β€ 2 * (n % p ^ i)) (Finset.Ico 1 b)).card := by rw [Nat.centralBinom_eq_two_mul_choose, padicValNat_choose (by omega) hb] have h : 2 * n - n = n := by omega rw [h] congr 1 apply Finset.filter_congr intro i _ omega /-! ### Counting residues in blocks -/ private lemma count_block (q : β) (P : β β Prop) [DecidablePred P] (M : β) : Nat.count (fun n => P (n % q)) (q * M) = M * Nat.count P q := by induction M with | zero => simp | succ M ih => rw [Nat.mul_succ, Nat.count_add, ih, Nat.succ_mul] congr 1 rw [Nat.count_eq_card_filter_range, Nat.count_eq_card_filter_range] congr 1 apply Finset.filter_congr intro k hk rw [Finset.mem_range] at hk rw [Nat.mul_add_mod, Nat.mod_eq_of_lt hk] private lemma block_le {q : β} (hq : 0 < q) (P : β β Prop) [DecidablePred P] (x : β) : ((Finset.range x).filter (fun n => P (n % q))).card β€ (x / q + 1) * ((Finset.range q).filter P).card := by have hcb := count_block q P (x / q + 1) simp only [Nat.count_eq_card_filter_range] at hcb rw [β hcb] apply Finset.card_le_card intro n hn simp only [Finset.mem_filter, Finset.mem_range] at hn β’ refine β¨?_, hn.2β© have h1 := Nat.div_add_mod x q have h2 := Nat.mod_lt x hq nlinarith [hn.1] /-! ### Large digits -/ /-- Number of "large" base-`p` digits (those `d` with `p β€ 2 d`) among the first `D` digits of `r`. -/private def cntL (p D r : β) : β := β i β Finset.range D, if p β€ 2 * (r / p ^ i % p) then 1 else 0 private lemma cntL_succ (D r : β) {p : β} : cntL p (D + 1) r = (if p β€ 2 * (r % p) then 1 else 0) + cntL p D (r / p) := by unfold cntL rw [Finset.sum_range_succ', add_comm] congr 1 Β· simp Β· apply Finset.sum_congr rfl intro i _ rw [pow_succ', Nat.div_div_eq_div_mul] /-- Number of `r < p ^ D` with fewer than `J` large digits. -/private def fS (p D J : β) : β := ((Finset.range (p ^ D)).filter (fun r => cntL p D r < J)).card private lemma fS_zero (p D : β) : fS p D 0 = 0 := by simp [fS] private lemma fS_le (p D J : β) : fS p D J β€ p ^ D := by unfold fS exact (Finset.card_filter_le _ _).trans (by simp) private lemma fS_succ {p : β} (hp : 0 < p) (D J : β) : fS p (D + 1) (J + 1) β€ fS p D (J + 1) * ((p + 1) / 2) + fS p D J * p := by set A := (Finset.range (p ^ D)).filter (fun r => cntL p D r < J + 1) with hA set B := (Finset.range (p ^ D)).filter (fun r => cntL p D r < J) with hB have sub : (Finset.range (p ^ (D + 1))).filter (fun r => cntL p (D + 1) r < J + 1) β Finset.image (fun q : β Γ β => q.2 + p * q.1) (A ΓΛ’ Finset.range ((p + 1) / 2) βͺ B ΓΛ’ Finset.range p) := by intro r hr simp only [Finset.mem_filter, Finset.mem_range] at hr obtain β¨hr1, hr2β© := hr rw [Finset.mem_image] have hm : r / p < p ^ D := by rw [Nat.div_lt_iff_lt_mul hp] rw [pow_succ] at hr1 exact hr1 refine β¨(r / p, r % p), ?_, ?_β© Β· rw [cntL_succ] at hr2 rw [Finset.mem_union, Finset.mem_product, Finset.mem_product] by_cases hl : p β€ 2 * (r % p) Β· right simp only [hl, βreduceIte] at hr2 refine β¨?_, ?_β© Β· rw [hB] simp only [Finset.mem_filter, Finset.mem_range] exact β¨hm, by omegaβ© Β· simpa using Nat.mod_lt r hp Β· left simp only [hl, βreduceIte] at hr2 refine β¨?_, ?_β© Β· rw [hA] simp only [Finset.mem_filter, Finset.mem_range] exact β¨hm, by omegaβ© Β· simp only [Finset.mem_range] omega Β· exact Nat.mod_add_div r p calc fS p (D + 1) (J + 1) β€ (Finset.image (fun q : β Γ β => q.2 + p * q.1) (A ΓΛ’ Finset.range ((p + 1) / 2) βͺ B ΓΛ’ Finset.range p)).card := Finset.card_le_card sub _ β€ (A ΓΛ’ Finset.range ((p + 1) / 2) βͺ B ΓΛ’ Finset.range p).card := Finset.card_image_le _ β€ (A ΓΛ’ Finset.range ((p + 1) / 2)).card + (B ΓΛ’ Finset.range p).card := Finset.card_union_le _ _ _ = fS p D (J + 1) * ((p + 1) / 2) + fS p D J * p := by rw [Finset.card_product, Finset.card_product, Finset.card_range, Finset.card_range] rfl /-! ### Analysis -/ private lemma tendsto_zero_of_rec (a b : β β β) (ha : β n, 0 β€ a n) (hb : Tendsto b atTop (π 0)) (h : β n, a (n + 1) β€ 2 / 3 * a n + b n) : Tendsto a atTop (π 0) := by rw [Metric.tendsto_atTop] intro Ξ΅ hΞ΅ obtain β¨N, hNβ© := Metric.tendsto_atTop.1 hb (Ξ΅ / 6) (by positivity) set m : β β β := fun n => max (a n - Ξ΅ / 2) 0 with hm have hstep : β n, N β€ n β m (n + 1) β€ 2 / 3 * m n := by intro n hn have := hN n hn rw [Real.dist_eq, sub_zero] at this have hb' := (abs_lt.1 this).2 have h1 := h n simp only [hm] apply max_le Β· have : 0 β€ max (a n - Ξ΅ / 2) 0 := le_max_right _ _ have h3 : a n - Ξ΅ / 2 β€ max (a n - Ξ΅ / 2) 0 := le_max_left _ _ linarith Β· have : 0 β€ max (a n - Ξ΅ / 2) 0 := le_max_right _ _ linarith have hpow : β j, m (N + j) β€ (2 / 3) ^ j * m N := by intro j induction j with | zero => simp | succ j ih => have := hstep (N + j) (by omega) calc m (N + j + 1) β€ 2 / 3 * m (N + j) := this _ β€ 2 / 3 * ((2 / 3) ^ j * m N) := by linarith _ = (2 / 3) ^ (j + 1) * m N := by ring have ht : Tendsto (fun j : β => (2 / 3 : β) ^ j * m N) atTop (π 0) := by have := (tendsto_pow_atTop_nhds_zero_of_lt_one (r := (2 / 3 : β)) (by norm_num) (by norm_num)).mul_const (m N) simpa using this obtain β¨J0, hJ0β© := Metric.tendsto_atTop.1 ht (Ξ΅ / 2) (by positivity) refine β¨N + J0, fun n hn => ?_β© obtain β¨j, rflβ© := Nat.exists_eq_add_of_le (by omega : N β€ n) have h1 := hJ0 j (by omega) rw [Real.dist_eq, sub_zero] at h1 β’ have h2 := hpow j have h3 : a (N + j) - Ξ΅ / 2 β€ m (N + j) := le_max_left _ _ have h4 := ha (N + j) have h5 := (abs_lt.1 h1).2 rw [abs_of_nonneg h4] linarith private lemma tendsto_fS {p : β} (hp : 2 β€ p) (J : β) : Tendsto (fun D : β => (fS p D J : β) / (p : β) ^ D) atTop (π 0) := by induction J with | zero => simp [fS_zero] | succ J ih => apply tendsto_zero_of_rec _ _ (fun D => by positivity) ih intro D have hp0 : (0 : β) < p := by positivity have hpD : (0 : β) < (p : β) ^ D := by positivity have h1 := fS_succ (by omega : 0 < p) D J have h2 : 3 * ((p + 1) / 2) β€ 2 * p := by omega have h1' : (fS p (D + 1) (J + 1) : β) β€ (fS p D (J + 1) : β) * (((p + 1) / 2 : β) : β) + (fS p D J : β) * p := by exact_mod_cast h1 have h2' : 3 * (((p + 1) / 2 : β) : β) β€ 2 * p := by exact_mod_cast h2 have hF : (0 : β) β€ fS p D (J + 1) := by positivity rw [div_le_iffβ (by positivity)] have e : (2 / 3 * ((fS p D (J + 1) : β) / (p : β) ^ D) + (fS p D J : β) / (p : β) ^ D) * (p : β) ^ (D + 1) = (2 / 3 * (fS p D (J + 1) : β) + fS p D J) * p := by field_simp ring rw [e] nlinarith /-! ### Carries -/ private lemma cntL_le_padic {p : β} (hp : p.Prime) (D n : β) : cntL p D (n % p ^ D) β€ padicValNat p (centralBinom n) := by have : Fact p.Prime := β¨hpβ© have hb : Nat.log p (2 * n) < max (Nat.log p (2 * n) + 1) (D + 1) := lt_of_lt_of_le (Nat.lt_succ_self _) (le_max_left _ _) rw [central_val' n _ hb] unfold cntL rw [β Finset.card_filter] apply Finset.card_le_card_of_injOn (fun i => i + 1) Β· intro i hi rw [Finset.mem_coe, Finset.mem_filter, Finset.mem_range] at hi rw [Finset.mem_coe, Finset.mem_filter, Finset.mem_Ico] beta_reduce refine β¨β¨by omega, lt_of_lt_of_le (by omega) (le_max_right _ _)β©, ?_β© have e1 : n % p ^ (i + 1) = (n % p ^ D) % p ^ (i + 1) := (Nat.mod_mod_of_dvd n (pow_dvd_pow p (by omega))).symm rw [e1, Nat.mod_pow_succ] have hpos : 0 < p ^ i := pow_pos hp.pos i have h3 := Nat.mul_le_mul_left (p ^ i) hi.2 rw [pow_succ] nlinarith [Nat.zero_le (n % p ^ D % p ^ i)] Β· intro a _ b _ h simpa using h /-! ### Per-prime bound -/ private lemma card_dvd_le_one (Q k : β) : ((Finset.range Q).filter (fun r => (r + k) % Q = 0)).card β€ 1 := by apply Finset.card_le_one.2 intro a ha b hb simp only [Finset.mem_filter, Finset.mem_range] at ha hb have h : a + k β‘ b + k [MOD Q] := by unfold Nat.ModEq rw [ha.2, hb.2] have h2 := Nat.ModEq.add_right_cancel' k h unfold Nat.ModEq at h2 rwa [Nat.mod_eq_of_lt ha.1, Nat.mod_eq_of_lt hb.1] at h2 private lemma tendsto_bad {p : β} (hp : p.Prime) (k : β) : Tendsto (fun x : β => (((Finset.range x).filter (fun n => padicValNat p (centralBinom n) < padicValNat p (n + k))).card : β) / x) atTop (π 0) := by have hp2 : 2 β€ p := hp.two_le have hpR : (1 : β) < p := by exact_mod_cast hp.one_lt rw [Metric.tendsto_atTop] intro Ξ΅ hΞ΅ obtain β¨J, hJβ© := pow_unbounded_of_one_lt (4 / Ξ΅) hpR obtain β¨D, hD0β© := Metric.tendsto_atTop.1 (tendsto_fS hp2 J) (Ξ΅ / 4) (by positivity) have hD := hD0 D le_rfl rw [Real.dist_eq, sub_zero, abs_of_nonneg (by positivity)] at hD obtain β¨N, hNβ© := exists_nat_gt (4 * (fS p D J : β) / Ξ΅ + 4 / Ξ΅) have hQpos : (0 : β) < (p : β) ^ J := by positivity have hqpos : (0 : β) < (p : β) ^ D := by positivity have hQ' : 4 < (p : β) ^ J * Ξ΅ := (div_lt_iffβ hΞ΅).1 hJ refine β¨N, fun x hx => ?_β© have hxN : (N : β) β€ x := by exact_mod_cast hx have h4 : (0 : β) < 4 / Ξ΅ := by positivity have h4F : (0 : β) β€ 4 * (fS p D J : β) / Ξ΅ := by positivity have hxpos : (0 : β) < x := by linarith have hF : 4 * (fS p D J : β) < x * Ξ΅ := (div_lt_iffβ hΞ΅).1 (by linarith) have hx1 : 4 < (x : β) * Ξ΅ := (div_lt_iffβ hΞ΅).1 (by linarith) set bad := (Finset.range x).filter (fun n => padicValNat p (centralBinom n) < padicValNat p (n + k)) with hbad set A := (Finset.range x).filter (fun n => cntL p D (n % p ^ D) < J) with hA set C := (Finset.range x).filter (fun n => (n % p ^ J + k) % p ^ J = 0) with hC have hsub : bad β A βͺ C := by intro n hn rw [hbad, Finset.mem_filter] at hn rw [Finset.mem_union] by_cases h : padicValNat p (centralBinom n) < J Β· left rw [hA, Finset.mem_filter] exact β¨hn.1, lt_of_le_of_lt (cntL_le_padic hp D n) hβ© Β· right rw [hC, Finset.mem_filter] refine β¨hn.1, ?_β© have h1 : p ^ J β£ n + k := (pow_dvd_pow p (by omega : J β€ padicValNat p (n + k))).trans pow_padicValNat_dvd rw [Nat.mod_add_mod] exact Nat.mod_eq_zero_of_dvd h1 have hAc : A.card β€ (x / p ^ D + 1) * fS p D J := block_le (pow_pos hp.pos D) (fun r => cntL p D r < J) x have hCc : C.card β€ (x / p ^ J + 1) * 1 := (block_le (pow_pos hp.pos J) (fun r => (r + k) % p ^ J = 0) x).trans (Nat.mul_le_mul_left _ (card_dvd_le_one _ _)) have hcard : bad.card β€ (x / p ^ D + 1) * fS p D J + (x / p ^ J + 1) * 1 := (Finset.card_le_card hsub).trans ((Finset.card_union_le _ _).trans (_root_.add_le_add hAc hCc)) have hcardR : (bad.card : β) β€ (((x / p ^ D : β) : β) + 1) * fS p D J + (((x / p ^ J : β) : β) + 1) * 1 := by exact_mod_cast hcard have e1 : ((x / p ^ D : β) : β) β€ (x : β) / (p : β) ^ D := by have := Nat.cast_div_le (Ξ± := β) (m := x) (n := p ^ D) simpa using this have e2 : ((x / p ^ J : β) : β) β€ (x : β) / (p : β) ^ J := by have := Nat.cast_div_le (Ξ± := β) (m := x) (n := p ^ J) simpa using this have hF0 : (0 : β) β€ fS p D J := by positivity have e3 : (x : β) / (p : β) ^ D * fS p D J = x * ((fS p D J : β) / (p : β) ^ D) := by ring have e4 : (x : β) / (p : β) ^ J < x * Ξ΅ / 4 := by rw [div_lt_iffβ hQpos] nlinarith have e5 : (x : β) * ((fS p D J : β) / (p : β) ^ D) β€ x * (Ξ΅ / 4) := mul_le_mul_of_nonneg_left hD.le hxpos.le have e6 : ((x / p ^ D : β) : β) * fS p D J β€ x * (Ξ΅ / 4) := by calc ((x / p ^ D : β) : β) * fS p D J β€ (x : β) / (p : β) ^ D * fS p D J := mul_le_mul_of_nonneg_right e1 hF0 _ = _ := e3 _ β€ _ := e5 rw [Real.dist_eq, sub_zero, abs_of_nonneg (by positivity), div_lt_iffβ hxpos] nlinarith /-- A prime power in `n + k` not dividing `C(2n, n)` happens at a small prime. -/private lemma exists_small_prime {k : β} (hk : 1 β€ k) (n : β) (h : Β¬ (n + k β£ centralBinom n)) : β p, p.Prime β§ p < 2 * k β§ padicValNat p (centralBinom n) < padicValNat p (n + k) := by by_contra hcon push Not at hcon apply h rw [β Nat.factorization_prime_le_iff_dvd (by omega) (centralBinom_pos n).ne'] intro p hp rw [Nat.factorization_def _ hp, Nat.factorization_def _ hp] by_cases hlt : p < 2 * k Β· exact hcon p hp hlt Β· have : Fact p.Prime := β¨hpβ© exact padicValNat_add_le hk (by omega) n /-- **Pomerance, Theorem 2.** For every `k β₯ 1`, the proportion of `n < x` with `n + k β£ C(2n, n)`tends to `1`. -/theorem tendsto_density_add_dvd_centralBinom (k : β) (hk : 1 β€ k) : Tendsto (fun x : β => (((Finset.range x).filter (fun n => n + k β£ centralBinom n)).card : β) / x) atTop (π 1) := by set S := (Finset.range (2 * k)).filter Nat.Prime with hS set badp : β β β β Finset β := fun p x => (Finset.range x).filter (fun n => padicValNat p (centralBinom n) < padicValNat p (n + k)) with hbp have hsum : Tendsto (fun x : β => β p β S, ((badp p x).card : β) / x) atTop (π 0) := by have := tendsto_finsetSum S (fun p hp => tendsto_bad (Finset.mem_filter.1 hp).2 k) simpa using this have hlow : Tendsto (fun x : β => 1 - β p β S, ((badp p x).card : β) / x) atTop (π 1) := by simpa using (tendsto_const_nhds (x := (1 : β))).sub hsum refine tendsto_of_tendsto_of_tendsto_of_le_of_le' hlow tendsto_const_nhds ?_ ?_ Β· filter_upwards [eventually_ge_atTop 1] with x hx have hxpos : (0 : β) < x := by exact_mod_cast hx set good := (Finset.range x).filter (fun n => n + k β£ centralBinom n) with hgood have h1 : good.card + ((Finset.range x).filter (fun n => Β¬ n + k β£ centralBinom n)).card = x := by rw [hgood, Finset.card_filter_add_card_filter_not, Finset.card_range] have hsub : (Finset.range x).filter (fun n => Β¬ n + k β£ centralBinom n) β S.biUnion (fun p => badp p x) := by intro n hn rw [Finset.mem_filter] at hn obtain β¨p, hp, hpk, hvβ© := exists_small_prime hk n hn.2 rw [Finset.mem_biUnion] refine β¨p, Finset.mem_filter.2 β¨Finset.mem_range.2 hpk, hpβ©, ?_β© rw [hbp] exact Finset.mem_filter.2 β¨hn.1, hvβ© have h2 : ((Finset.range x).filter (fun n => Β¬ n + k β£ centralBinom n)).card β€ β p β S, (badp p x).card := (Finset.card_le_card hsub).trans Finset.card_biUnion_le have h1R : (good.card : β) + (((Finset.range x).filter (fun n => Β¬ n + k β£ centralBinom n)).card : β) = x := by exact_mod_cast h1 have h2R : (((Finset.range x).filter (fun n => Β¬ n + k β£ centralBinom n)).card : β) β€ β p β S, ((badp p x).card : β) := by exact_mod_cast h2 have hs : β p β S, ((badp p x).card : β) / x = (β p β S, ((badp p x).card : β)) / x := by simp only [div_eq_mul_inv] rw [Finset.sum_mul] rw [hs, le_div_iffβ hxpos, sub_mul, one_mul, div_mul_cancelβ _ hxpos.ne'] linarith Β· filter_upwards [eventually_ge_atTop 1] with x hx have hxpos : (0 : β) < x := by exact_mod_cast hx rw [div_le_one hxpos] have : ((Finset.range x).filter (fun n => n + k β£ centralBinom n)).card β€ x := by simpa using Finset.card_filter_le (Finset.range x) (fun n => n + k β£ centralBinom n) exact_mod_cast this end MiddleBinomial