/-
Copyright 2025 The Formal Conjectures Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
module
public import Mathlib.Analysis.SpecialFunctions.Log.Basic
public import FormalConjecturesForMathlib.Algebra.Order.Group.Pointwise.Interval
public import FormalConjecturesForMathlib.Data.Set.Interval
public import FormalConjecturesForMathlib.Order.Interval.Finset.Basic
public import FormalConjecturesForMathlib.Order.Interval.Finset.Nat
public import Batteries.Util.ProofWanted
public import Mathlib.Tactic@[expose] public sectionopen Filteropen scoped Topologynamespace Set
Given a set S and an element b in an order β, where all intervals bounded above are finite,
we define the partial density of S (relative to a set A) to be the proportion of elements in
{x ∈ A | x < b} that lie in S ∩ A.
This definition was inspired from https://github.com/b-mehta/unit-fractions
@[inline]
noncomputable abbrev partialDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) (b : β) : ℝ :=
((S ∩ A) ∩ Iio b).ncard / (A ∩ Iio b).ncardtheorem partialDensity_le_one {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) (b : β) : S.partialDensity A b ≤ 1 := β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set βb:β⊢ S.partialDensity A b ≤ 1
β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set βb:β⊢ ↑(S ∩ A ∩ Iio b).ncard ≤ ↑(A ∩ Iio b).ncard
All goals completed! 🐙
Given a set S in an order β, where all intervals bounded above are finite, we define the upper
density of S (relative to a set A) to be the limsup of the partial densities of S
(relative to A) for b → ∞.
noncomputable def upperDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : ℝ :=
atTop.limsup fun (b : β) ↦ S.partialDensity A b
Given a set S in an order β, where all intervals bounded above are finite, we define the lower
density of S (relative to a set A) to be the liminf of the partial densities of S
(relative to A) for b → ∞.
noncomputable def lowerDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : ℝ :=
atTop.liminf fun (b : β) ↦ S.partialDensity A btheorem lowerDensity_le_one {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : S.lowerDensity A ≤ 1 := β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set β⊢ S.lowerDensity A ≤ 1
β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set βh:atTop = ⊥⊢ S.lowerDensity A ≤ 1β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set βh:¬atTop = ⊥⊢ S.lowerDensity A ≤ 1
β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set βh:atTop = ⊥⊢ S.lowerDensity A ≤ 1 All goals completed! 🐙
β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set βh:¬atTop = ⊥⊢ S.lowerDensity A ≤ 1 β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set βh:¬atTop = ⊥this:atTop.NeBot⊢ S.lowerDensity A ≤ 1
β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set βh:¬atTop = ⊥this:atTop.NeBotx:ℝhx:x ∈ {a | ∀ᶠ (n : ℝ) in map (fun b ↦ S.partialDensity A b) atTop, a ≤ n}⊢ x ≤ 1
All goals completed! 🐙β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set β⊢ 0 ≤ sSup {a | ∀ᶠ (n : β) in atTop, a ≤ S.partialDensity A n}
exact (em _).elim (le_csSup · <| .of_forall fun _ ↦ by β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βS:Set βA:Set β⊢ 0 ≤ S.lowerDensity A positivity All goals completed! 🐙)
(Real.sSup_of_not_bddAbove · |>.ge)
A set S in an order β where all intervals bounded above are finite is said to have
density α : ℝ (relative to a set A) if the proportion of x ∈ S such that x < n
in A tends to α as n → ∞.
When β = ℕ this by default defines the natural density of a set
(i.e., relative to all of ℕ).
def HasDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (α : ℝ) (A : Set β := Set.univ) : Prop :=
Tendsto (fun (b : β) => S.partialDensity A b) atTop (𝓝 α)
A set S in an order β where all intervals bounded above are finite is said to have
positive density (relative to a set A) if there exists a positive α : ℝ such that
S has density α (relative to a set A).
def HasPosDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : Prop :=
∃ α > 0, S.HasDensity α A
The two-sided partial natural density of a set of integers, counted inside the interval
[-N, N].
noncomputable def intPartialDensity (S : Set ℤ) (N : ℕ) : ℝ :=
open scoped Classical in
(((Finset.Icc (-(N : ℤ)) (N : ℤ)).filter (fun m => m ∈ S)).card : ℝ) /
(2 * (N : ℝ) + 1)
A set of integers has two-sided natural density α.
def HasIntDensity (S : Set ℤ) (α : ℝ) : Prop :=
Tendsto (fun N : ℕ => S.intPartialDensity N) atTop (𝓝 α)@[simp]
theorem intPartialDensity_empty (N : ℕ) : intPartialDensity (∅ : Set ℤ) N = 0 := by N:ℕ⊢ ∅.intPartialDensity N = 0
simp [intPartialDensity] All goals completed! 🐙namespace HasIntDensity@[simp]
theorem empty : HasIntDensity (∅ : Set ℤ) 0 := by ⊢ ∅.HasIntDensity 0
simp [HasIntDensity] All goals completed! 🐙end HasIntDensitynamespace HasDensityIn a non-trivial partial order with a least element, the set of all elements has density one.
@[simp]
theorem univ {β : Type*} [PartialOrder β] [LocallyFiniteOrder β] [OrderBot β] [Nontrivial β] :
(@Set.univ β).HasDensity 1 := by β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βinst✝:Nontrivial β⊢ Set.univ.HasDensity 1
by_cases h : atTop (α := β) = ⊥ pos β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βinst✝:Nontrivial βh:atTop = ⊥⊢ Set.univ.HasDensity 1neg β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βinst✝:Nontrivial βh:¬atTop = ⊥⊢ Set.univ.HasDensity 1
· pos β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βinst✝:Nontrivial βh:atTop = ⊥⊢ Set.univ.HasDensity 1 simp [h, HasDensity] All goals completed! 🐙
· neg β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βinst✝:Nontrivial βh:¬atTop = ⊥⊢ Set.univ.HasDensity 1 simp [HasDensity, partialDensity] neg β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βinst✝:Nontrivial βh:¬atTop = ⊥⊢ Tendsto (fun b ↦ ↑(Iio b).ncard / ↑(Iio b).ncard) atTop (𝓝 1)
let ⟨b, hb⟩ := Set.Iio_eventually_ncard_ne_zero β neg β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βinst✝:Nontrivial βh:¬atTop = ⊥b:βhb:∀ n ≥ b, (Iio n).ncard ≠ 0⊢ Tendsto (fun b ↦ ↑(Iio b).ncard / ↑(Iio b).ncard) atTop (𝓝 1)
refine tendsto_const_nhds.congr' ?_ neg β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βinst✝:Nontrivial βh:¬atTop = ⊥b:βhb:∀ n ≥ b, (Iio n).ncard ≠ 0⊢ (fun x ↦ 1) =ᶠ[atTop] fun b ↦ ↑(Iio b).ncard / ↑(Iio b).ncard
exact (eventually_ge_atTop b).mono fun n hn ↦ (div_self <| mod_cast hb n hn).symm All goals completed! 🐙theorem univ_nat_hasDensity_one : (@Set.univ ℕ).HasDensity 1 := univ@[simp]
theorem empty {β : Type*} [Preorder β] [LocallyFiniteOrderBot β] (A : Set β := Set.univ) :
Set.HasDensity (∅ : Set β) 0 A := by β:Type u_1inst✝¹:Preorder βinst✝:LocallyFiniteOrderBot βA:Set β⊢ ∅.HasDensity 0 A
simpa [HasDensity, partialDensity] using tendsto_const_nhds All goals completed! 🐙
theorem mono {β : Type*} [PartialOrder β] [LocallyFiniteOrder β] [OrderBot β]
{S T : Set β} {αS αT : ℝ} [(atTop (α := β)).NeBot] (h : S ⊆ T) (hS : S.HasDensity αS)
(hT : T.HasDensity αT) : αS ≤ αT := by β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:S.HasDensity αShT:T.HasDensity αT⊢ αS ≤ αT
rw [HasDensity β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)⊢ αS ≤ αT β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)⊢ αS ≤ αT] at hS hT β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)⊢ αS ≤ αT
apply le_of_tendsto_of_tendsto hS hT β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)⊢ (fun b ↦ S.partialDensity Set.univ b) ≤ᶠ[atTop] fun b ↦ T.partialDensity Set.univ b
filter_upwards [eventually_ge_atTop ⊥] with b hb β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)b:βhb:⊥ ≤ b⊢ S.partialDensity Set.univ b ≤ T.partialDensity Set.univ b
apply div_le_div_of_nonneg_right hab β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)b:βhb:⊥ ≤ b⊢ ↑(S ∩ Set.univ ∩ Iio b).ncard ≤ ↑(T ∩ Set.univ ∩ Iio b).ncardhc β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)b:βhb:⊥ ≤ b⊢ 0 ≤ ↑(Set.univ ∩ Iio b).ncard
grw [Set.ncard_le_ncard (inter_subset_inter_left _ (inter_subset_inter_left _ h)) hab β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)b:βhb:⊥ ≤ b⊢ ↑(T ∩ Set.univ ∩ Iio b).ncard ≤ ↑(T ∩ Set.univ ∩ Iio b).ncardhc β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)b:βhb:⊥ ≤ b⊢ 0 ≤ ↑(Set.univ ∩ Iio b).ncard] hc β:Type u_1inst✝³:PartialOrder βinst✝²:LocallyFiniteOrder βinst✝¹:OrderBot βS:Set βT:Set βαS:ℝαT:ℝinst✝:atTop.NeBoth:S ⊆ ThS:Tendsto (fun b ↦ S.partialDensity Set.univ b) atTop (𝓝 αS)hT:Tendsto (fun b ↦ T.partialDensity Set.univ b) atTop (𝓝 αT)b:βhb:⊥ ≤ b⊢ 0 ≤ ↑(Set.univ ∩ Iio b).ncard
exact Nat.cast_nonneg _ All goals completed! 🐙theorem nonneg {β : Type*} [Preorder β] [LocallyFiniteOrderBot β] [(atTop : Filter β).NeBot]
{S : Set β} {α : ℝ} (h : S.HasDensity α) : 0 ≤ α :=
le_of_tendsto_of_tendsto' empty h fun b => by β:Type u_1inst✝²:Preorder βinst✝¹:LocallyFiniteOrderBot βinst✝:atTop.NeBotS:Set βα:ℝh:S.HasDensity αb:β⊢ ∅.partialDensity Set.univ b ≤ S.partialDensity Set.univ b simp [div_nonneg, partialDensity] All goals completed! 🐙end Set.HasDensitynamespace Natopen Set
The natural density of the set of even numbers is 1 / 2.
theorem hasDensity_even : {n : ℕ | Even n}.HasDensity (1 / 2) := by ⊢ {n | Even n}.HasDensity (1 / 2)
simp [HasDensity, partialDensity] ⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
have h {n : ℕ} (hn : 1 ≤ n) : (({n : ℕ | Even n} ∩ Iio n).ncard : ℝ) / n =
if Even n then 2⁻¹ else (n + 1 : ℝ) / n * 2⁻¹ := by ⊢ {n | Even n}.HasDensity (1 / 2) h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
split_ifs with h pos n:ℕhn:1 ≤ nh:Even n⊢ ↑({n | Even n} ∩ Iio n).ncard / ↑n = 2⁻¹neg n:ℕhn:1 ≤ nh:¬Even n⊢ ↑({n | Even n} ∩ Iio n).ncard / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
· pos n:ℕhn:1 ≤ nh:Even n⊢ ↑({n | Even n} ∩ Iio n).ncard / ↑n = 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) rw [← image_mul_two_Iio_even h, pos n:ℕhn:1 ≤ nh:Even n⊢ ↑((fun x ↦ 2 * x) '' Iio (n / 2)).ncard / ↑n = 2⁻¹ All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) ncard_image_of_injective _
(mul_right_injective₀ (by n:ℕhn:1 ≤ nh:Even n⊢ 2 ≠ 0 All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) simp All goals completed! 🐙 All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹))), ncard_Iio, pos n:ℕhn:1 ≤ nh:Even n⊢ ↑(n / 2) / ↑n = 2⁻¹ All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
cast_div_charZero (even_iff_two_dvd.mp h), pos n:ℕhn:1 ≤ nh:Even n⊢ ↑n / ↑2 / ↑n = 2⁻¹ All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) cast_ofNat, pos n:ℕhn:1 ≤ nh:Even n⊢ ↑n / 2 / ↑n = 2⁻¹ All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
div_div_cancel_left' <| cast_ne_zero.2 (by n:ℕhn:1 ≤ nh:Even n⊢ n ≠ 0 All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) linarith All goals completed! 🐙 All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹))] All goals completed! 🐙 h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
· neg n:ℕhn:1 ≤ nh:¬Even n⊢ ↑({n | Even n} ∩ Iio n).ncard / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) replace h : Even (n + 1) := by ⊢ {n | Even n}.HasDensity (1 / 2) neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ ↑({n | Even n} ∩ Iio n).ncard / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) simpa [Nat.even_add', ← Nat.not_even_iff_odd]neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ ↑({n | Even n} ∩ Iio n).ncard / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ ↑({n | Even n} ∩ Iio n).ncard / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
rw [← image_mul_two_Iio n, neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ ↑((fun x ↦ 2 * x) '' Iio ((n + 1) / 2)).ncard / ↑n = (↑n + 1) / ↑n * 2⁻¹ neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) ncard_image_of_injective _
(mul_right_injective₀ (by n:ℕhn:1 ≤ nh:Even (n + 1)⊢ 2 ≠ 0neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) simp All goals completed! 🐙neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹))), ncard_Iio, neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ ↑((n + 1) / 2) / ↑n = (↑n + 1) / ↑n * 2⁻¹neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
cast_div (even_iff_two_dvd.mp h) (by n:ℕhn:1 ≤ nh:Even (n + 1)⊢ ↑2 ≠ 0neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) norm_num All goals completed! 🐙neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)), cast_add neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)]neg n:ℕhn:1 ≤ nh:Even (n + 1)⊢ (↑n + ↑1) / ↑2 / ↑n = (↑n + 1) / ↑n * 2⁻¹ h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹); ring h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹) h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun b ↦ ↑({n | Even n} ∩ Iio b).ncard / ↑b) atTop (𝓝 2⁻¹)
refine Tendsto.congr' (eventually_atTop.2 ⟨1, fun n hn => (h hn).symm⟩)
(Tendsto.if' tendsto_const_nhds ?_) h:∀ {n : ℕ}, 1 ≤ n → ↑({n | Even n} ∩ Iio n).ncard / ↑n = if Even n then 2⁻¹ else (↑n + 1) / ↑n * 2⁻¹⊢ Tendsto (fun n ↦ (↑n + 1) / ↑n * 2⁻¹) atTop (𝓝 2⁻¹)
replace h : Tendsto (fun (k : ℕ) => 1 + 1 / (k : ℝ)) atTop (𝓝 1) := by ⊢ {n | Even n}.HasDensity (1 / 2) h:Tendsto (fun k ↦ 1 + 1 / ↑k) atTop (𝓝 1)⊢ Tendsto (fun n ↦ (↑n + 1) / ↑n * 2⁻¹) atTop (𝓝 2⁻¹)
simpa using Tendsto.const_add (M := ℝ) _ tendsto_one_div_atTop_nhds_zero_nat h:Tendsto (fun k ↦ 1 + 1 / ↑k) atTop (𝓝 1)⊢ Tendsto (fun n ↦ (↑n + 1) / ↑n * 2⁻¹) atTop (𝓝 2⁻¹) h:Tendsto (fun k ↦ 1 + 1 / ↑k) atTop (𝓝 1)⊢ Tendsto (fun n ↦ (↑n + 1) / ↑n * 2⁻¹) atTop (𝓝 2⁻¹)
simpa using Tendsto.mul_const _ <|
Tendsto.congr' (eventually_atTop.2 ⟨1, fun k hk => by h:Tendsto (fun k ↦ 1 + 1 / ↑k) atTop (𝓝 1)k:ℕhk:1 ≤ k⊢ 1 + 1 / ↑k = (↑k + 1) / ↑k field_simp All goals completed! 🐙⟩) hA finite set has natural density zero.
theorem hasDensity_zero_of_finite {S : Set ℕ} (h : S.Finite) : S.HasDensity 0 := by S:Set ℕh:S.Finite⊢ S.HasDensity 0
simp [HasDensity, partialDensity] S:Set ℕh:S.Finite⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0)
have (n : ℕ) : ((S ∩ Set.Iio n).ncard : ℝ) / n ≤ S.ncard / n := by S:Set ℕh:S.Finite⊢ S.HasDensity 0 S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0)
by_cases h₀ : n = 0 pos S:Set ℕh:S.Finiten:ℕh₀:n = 0⊢ ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑nneg S:Set ℕh:S.Finiten:ℕh₀:¬n = 0⊢ ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0); simp [h₀] neg S:Set ℕh:S.Finiten:ℕh₀:¬n = 0⊢ ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0)
exact div_le_div₀ (by S:Set ℕh:S.Finiten:ℕh₀:¬n = 0⊢ 0 ≤ ↑S.ncard S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0) simp All goals completed! 🐙 S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0)) (by S:Set ℕh:S.Finiten:ℕh₀:¬n = 0⊢ ↑(S ∩ Iio n).ncard ≤ ↑S.ncard S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0) simpa using Set.ncard_inter_le_ncard_left _ _ h All goals completed! 🐙 S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0))
(by S:Set ℕh:S.Finiten:ℕh₀:¬n = 0⊢ 0 < ↑n S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0) simpa using n.pos_of_ne_zero h₀ All goals completed! 🐙 S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0)) le_rfl S:Set ℕh:S.Finitethis:∀ (n : ℕ), ↑(S ∩ Iio n).ncard / ↑n ≤ ↑S.ncard / ↑n⊢ Tendsto (fun b ↦ ↑(S ∩ Iio b).ncard / ↑b) atTop (𝓝 0)
exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds
(tendsto_const_div_atTop_nhds_zero_nat _)
(fun _ => div_nonneg (cast_nonneg _) (cast_nonneg _)) this All goals completed! 🐙A set of positive natural density is infinite.
theorem infinite_of_hasDensity_pos {S : Set ℕ} {α : ℝ} (h : S.HasDensity α) (hα : α ≠ 0) :
S.Infinite :=
mt hasDensity_zero_of_finite fun h' => hα (tendsto_nhds_unique h h')end NatLogarithmic Density
section LogarithmicDensityopen Finset Real
A set A of natural numbers has logarithmic density d if the sequence
$(1 / \log n) \cdot \sum_{k \in A, k \le n} (1/k)$ converges to d.
Logarithmic density is a weaker notion than natural density: if a set has natural density d,
then it also has logarithmic density d (see Set.HasDensity.hasLogDensity), but the converse
is false.
def Set.HasLogDensity (A : Set ℕ) (d : ℝ) : Prop :=
open scoped Classical in
Tendsto (fun n : ℕ => (∑ k ≤ n with k ∈ A, (k : ℝ)⁻¹ / .log n : ℝ)) atTop (𝓝 d)
If a set has natural density d, then it also has logarithmic density d.
proof_wanted Set.HasDensity.hasLogDensity {A : Set ℕ} {d : ℝ} (h : A.HasDensity d) : A.HasLogDensity dend LogarithmicDensity