Counting on ℕ #
This file defines the count
function, which gives, for any predicate on the natural numbers,
"how many numbers under k
satisfy this predicate?".
We then prove several expected lemmas about count
, relating it to the cardinality of other
objects, and helping to evaluate it for specific k
.
Count the number of naturals k < n
satisfying p k
.
Equations
- Nat.count p n = List.countP (fun (b : ℕ) => decide (p b)) (List.range n)
Instances For
A fintype instance for the set relevant to Nat.count
. Locally an instance in locale count
Equations
- Nat.CountSet.fintype p n = Fintype.ofFinset (Finset.filter p (Finset.range n)) ⋯
Instances For
theorem
Nat.count_eq_card_filter_range
(p : ℕ → Prop)
[DecidablePred p]
(n : ℕ)
:
Nat.count p n = (Finset.filter p (Finset.range n)).card
count p n
can be expressed as the cardinality of {k // k < n ∧ p k}
.
Alias of the reverse direction of Nat.count_succ_eq_succ_count_iff
.
Alias of the reverse direction of Nat.count_succ_eq_count_iff
.
theorem
Nat.count_le_cardinal
{p : ℕ → Prop}
[DecidablePred p]
(n : ℕ)
:
↑(Nat.count p n) ≤ Cardinal.mk ↑{k : ℕ | p k}
theorem
Nat.count_le_card
{p : ℕ → Prop}
[DecidablePred p]
(hp : Set.Finite (setOf p))
(n : ℕ)
:
Nat.count p n ≤ (Set.Finite.toFinset hp).card
theorem
Nat.count_lt_card
{p : ℕ → Prop}
[DecidablePred p]
{n : ℕ}
(hp : Set.Finite (setOf p))
(hpn : p n)
:
Nat.count p n < (Set.Finite.toFinset hp).card
theorem
Nat.count_mono_left
{p : ℕ → Prop}
[DecidablePred p]
{q : ℕ → Prop}
[DecidablePred q]
{n : ℕ}
(hpq : ∀ (k : ℕ), p k → q k)
: