# Documentation

Mathlib.Data.Nat.Nth

# The nth Number Satisfying a Predicate #

This file defines a function for "what is the nth number that satisifies a given predicate p", and provides lemmas that deal with this function and its connection to Nat.count.

## Main definitions #

• Nat.nth p n: The n-th natural k (zero-indexed) such that p k. If there is no such natural (that is, p is true for at most n naturals), then Nat.nth p n = 0.

## Main results #

• Nat.nth_eq_orderEmbOfFin: For a fintely-often true p, gives the cardinality of the set of numbers satisfying p above particular values of nth p
• Nat.gc_count_nth: Establishes a Galois connection between Nat.nth p and Nat.count p.
• Nat.nth_eq_orderIsoOfNat: For an infinitely-ofter 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.

noncomputable def Nat.nth (p : ) (n : ) :

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.

Instances For

### Lemmas about Nat.nth on a finite set #

theorem Nat.nth_of_card_le {p : } (hf : Set.Finite ()) {n : } (hn : ) :
Nat.nth p n = 0
theorem Nat.nth_eq_getD_sort {p : } (h : Set.Finite ()) (n : ) :
Nat.nth p n = List.getD (Finset.sort (fun x x_1 => x x_1) ()) n 0
theorem Nat.nth_eq_orderEmbOfFin {p : } (hf : Set.Finite ()) {n : } (hn : ) :
Nat.nth p n = ↑(Finset.orderEmbOfFin () (_ : )) { val := n, isLt := hn }
theorem Nat.nth_strictMonoOn {p : } (hf : Set.Finite ()) :
theorem Nat.nth_lt_nth_of_lt_card {p : } (hf : Set.Finite ()) {m : } {n : } (h : m < n) (hn : ) :
Nat.nth p m < Nat.nth p n
theorem Nat.nth_le_nth_of_lt_card {p : } (hf : Set.Finite ()) {m : } {n : } (h : m n) (hn : ) :
theorem Nat.lt_of_nth_lt_nth_of_lt_card {p : } (hf : Set.Finite ()) {m : } {n : } (h : Nat.nth p m < Nat.nth p n) (hm : ) :
m < n
theorem Nat.le_of_nth_le_nth_of_lt_card {p : } (hf : Set.Finite ()) {m : } {n : } (h : Nat.nth p m Nat.nth p n) (hm : ) :
m n
theorem Nat.nth_injOn {p : } (hf : Set.Finite ()) :
Set.InjOn () ()
theorem Nat.range_nth_of_finite {p : } (hf : Set.Finite ()) :
@[simp]
theorem Nat.image_nth_Iio_card {p : } (hf : Set.Finite ()) :
=
theorem Nat.nth_mem_of_lt_card {p : } {n : } (hf : Set.Finite ()) (hlt : ) :
p (Nat.nth p n)
theorem Nat.exists_lt_card_finite_nth_eq {p : } (hf : Set.Finite ()) {x : } (h : p x) :
n, Nat.nth p n = x

### Lemmas about Nat.nth on an infinite set #

theorem Nat.nth_apply_eq_orderIsoOfNat {p : } (hf : ) (n : ) :
Nat.nth p n = ↑(↑() n)

When s is an infinite set, nth agrees with Nat.Subtype.orderIsoOfNat.

theorem Nat.nth_eq_orderIsoOfNat {p : } (hf : ) :
= Subtype.val ↑()

When s is an infinite set, nth agrees with Nat.Subtype.orderIsoOfNat.

theorem Nat.nth_strictMono {p : } (hf : ) :
theorem Nat.nth_injective {p : } (hf : ) :
theorem Nat.nth_monotone {p : } (hf : ) :
theorem Nat.nth_lt_nth {p : } (hf : ) {k : } {n : } :
Nat.nth p k < Nat.nth p n k < n
theorem Nat.nth_le_nth {p : } (hf : ) {k : } {n : } :
Nat.nth p k Nat.nth p n k n
theorem Nat.range_nth_of_infinite {p : } (hf : ) :
theorem Nat.nth_mem_of_infinite {p : } (hf : ) (n : ) :
p (Nat.nth p n)

### Lemmas that work for finite and infinite sets #

theorem Nat.exists_lt_card_nth_eq {p : } {x : } (h : p x) :
n, (∀ (hf : Set.Finite ()), ) Nat.nth p n = x
theorem Nat.nth_mem {p : } (n : ) (h : ∀ (hf : Set.Finite ()), ) :
p (Nat.nth p n)
theorem Nat.nth_lt_nth' {p : } {m : } {n : } (hlt : m < n) (h : ∀ (hf : Set.Finite ()), ) :
Nat.nth p m < Nat.nth p n
theorem Nat.nth_le_nth' {p : } {m : } {n : } (hle : m n) (h : ∀ (hf : Set.Finite ()), ) :
theorem Nat.le_nth {p : } {n : } (h : ∀ (hf : Set.Finite ()), ) :
n Nat.nth p n
theorem Nat.isLeast_nth {p : } {n : } (h : ∀ (hf : Set.Finite ()), ) :
IsLeast {i | p i ∀ (k : ), k < nNat.nth p k < i} (Nat.nth p n)
theorem Nat.isLeast_nth_of_lt_card {p : } {n : } (hf : Set.Finite ()) (hn : ) :
IsLeast {i | p i ∀ (k : ), k < nNat.nth p k < i} (Nat.nth p n)
theorem Nat.isLeast_nth_of_infinite {p : } (hf : ) (n : ) :
IsLeast {i | p i ∀ (k : ), k < nNat.nth p k < i} (Nat.nth p n)
theorem Nat.nth_eq_sInf (p : ) (n : ) :
Nat.nth p n = sInf {x | p x ∀ (k : ), k < nNat.nth p k < x}

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 ≥ card s.

theorem Nat.nth_zero {p : } :
Nat.nth p 0 = sInf ()
@[simp]
theorem Nat.nth_zero_of_zero {p : } (h : p 0) :
Nat.nth p 0 = 0
theorem Nat.nth_zero_of_exists {p : } [] (h : n, p n) :
Nat.nth p 0 =
theorem Nat.nth_eq_zero {p : } {n : } :
Nat.nth p n = 0 p 0 n = 0 hf,
theorem Nat.nth_eq_zero_mono {p : } (h₀ : ¬p 0) {a : } {b : } (hab : a b) (ha : Nat.nth p a = 0) :
Nat.nth p b = 0
theorem Nat.le_nth_of_lt_nth_succ {p : } {k : } {a : } (h : a < Nat.nth p (k + 1)) (ha : p a) :
a Nat.nth p k
@[simp]
theorem Nat.count_nth_zero (p : ) [] :
Nat.count p (Nat.nth p 0) = 0
theorem Nat.filter_range_nth_eq_insert {p : } [] {k : } (hlt : ∀ (hf : Set.Finite ()), k + 1 < ) :
theorem Nat.filter_range_nth_eq_insert_of_finite {p : } [] (hf : Set.Finite ()) {k : } (hlt : k + 1 < ) :
theorem Nat.count_nth {p : } [] {n : } (hn : ∀ (hf : Set.Finite ()), ) :
Nat.count p (Nat.nth p n) = n
theorem Nat.count_nth_of_lt_card_finite {p : } [] {n : } (hp : Set.Finite ()) (hlt : ) :
Nat.count p (Nat.nth p n) = n
theorem Nat.count_nth_of_infinite {p : } [] (hp : ) (n : ) :
Nat.count p (Nat.nth p n) = n
theorem Nat.count_nth_succ {p : } [] {n : } (hn : ∀ (hf : Set.Finite ()), ) :
Nat.count p (Nat.nth p n + 1) = n + 1
@[simp]
theorem Nat.nth_count {p : } [] {n : } (hpn : p n) :
Nat.nth p () = n
theorem Nat.nth_lt_of_lt_count {p : } [] {n : } {k : } (h : k < ) :
Nat.nth p k < n
theorem Nat.le_nth_of_count_le {p : } [] {n : } {k : } (h : n Nat.nth p k) :
k
theorem Nat.nth_count_eq_sInf (p : ) [] (n : ) :
Nat.nth p () = sInf {i | p i n i}
theorem Nat.le_nth_count' {p : } [] {n : } (hpn : k, p k n k) :
n Nat.nth p ()
theorem Nat.le_nth_count {p : } [] (hp : ) (n : ) :
n Nat.nth p ()
noncomputable def Nat.giCountNth {p : } [] (hp : ) :

If a predicate p : ℕ → Prop is true for infinitely many numbers, then Nat.count p and Nat.nth p form a Galois insertion.

Instances For
theorem Nat.gc_count_nth {p : } [] (hp : ) :
theorem Nat.count_le_iff_le_nth {p : } [] (hp : ) {a : } {b : } :
b a Nat.nth p b
theorem Nat.lt_nth_iff_count_lt {p : } [] (hp : ) {a : } {b : } :
a < Nat.nth p a < b