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 (fun (x : ℕ) => p x) (Finset.range n)) ⋯
Instances For
theorem
Nat.count_eq_card_filter_range
(p : ℕ → Prop)
[DecidablePred p]
(n : ℕ)
:
Nat.count p n = (Finset.filter (fun (x : ℕ) => p x) (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.lt_of_count_lt_count
{p : ℕ → Prop}
[DecidablePred p]
{a b : ℕ}
(h : Nat.count p a < Nat.count p b)
:
a < b
theorem
Nat.count_injective
{p : ℕ → Prop}
[DecidablePred p]
{m n : ℕ}
(hm : p m)
(hn : p n)
(heq : Nat.count p m = Nat.count p n)
:
m = n
theorem
Nat.count_lt_card
{p : ℕ → Prop}
[DecidablePred p]
{n : ℕ}
(hp : (setOf p).Finite)
(hpn : p n)
:
Alias of the reverse direction of Nat.count_iff_forall
.
Alias of the reverse direction of Nat.count_iff_forall_not
.
theorem
Nat.count_mono_left
{p : ℕ → Prop}
[DecidablePred p]
{q : ℕ → Prop}
[DecidablePred q]
{n : ℕ}
(hpq : ∀ (k : ℕ), p k → q k)
: