The n
th Number Satisfying a Predicate #
This file defines a function for "what is the n
th number that satisfies a given predicate p
",
and provides lemmas that deal with this function and its connection to Nat.count
.
Main definitions #
Nat.nth p n
: Then
-th naturalk
(zero-indexed) such thatp k
. If there is no such natural (that is,p
is true for at mostn
naturals), thenNat.nth p n = 0
.
Main results #
Nat.nth_eq_orderEmbOfFin
: For a finitely-often truep
, gives the cardinality of the set of numbers satisfyingp
above particular values ofnth p
Nat.gc_count_nth
: Establishes a Galois connection betweenNat.nth p
andNat.count p
.Nat.nth_eq_orderIsoOfNat
: For an infinitely-often true predicate,nth
agrees with the order-isomorphism of the subtype to the natural numbers.
There has been some discussion on the subject of whether both of nth
and
Nat.Subtype.orderIsoOfNat
should exist. See discussion
here.
Future work should address how lemmas that use these should be written.
Find the n
-th natural number satisfying p
(indexed from 0
, so nth p 0
is the first
natural number satisfying p
), or 0
if there is no such number. See also
Subtype.orderIsoOfNat
for the order isomorphism with ℕ when p
is infinitely often true.
Equations
- Nat.nth p n = if h : (setOf p).Finite then (Finset.sort (fun (x1 x2 : ℕ) => x1 ≤ x2) h.toFinset).getD n 0 else ↑((Nat.Subtype.orderIsoOfNat (setOf p)) n)
Instances For
When s
is an infinite set, nth
agrees with Nat.Subtype.orderIsoOfNat
.
When s
is an infinite set, nth
agrees with Nat.Subtype.orderIsoOfNat
.
Lemmas that work for finite and infinite sets #
An alternative recursive definition of Nat.nth
: Nat.nth s n
is the infimum of x ∈ s
such
that Nat.nth s k < x
for all k < n
, if this set is nonempty. We do not assume that the set is
nonempty because we use the same "garbage value" 0
both for sInf
on ℕ
and for Nat.nth s n
for n ≥ #s
.
If a predicate p : ℕ → Prop
is true for infinitely many numbers, then Nat.count p
and
Nat.nth p
form a Galois insertion.
Equations
- Nat.giCountNth hp = GaloisInsertion.monotoneIntro ⋯ ⋯ ⋯ ⋯