doc-next-gen

Mathlib.Data.Nat.Log

Module docstring

{"# Natural number logarithms

This file defines two -valued analogs of the logarithm of n with base b: * log b n: Lower logarithm, or floor log. Greatest k such that b^k ≤ n. * clog b n: Upper logarithm, or ceil log. Least k such that n ≤ b^k.

These are interesting because, for 1 < b, Nat.log b and Nat.clog b are respectively right and left adjoints of Nat.pow b. See pow_le_iff_le_log and le_pow_iff_clog_le. ","### Floor logarithm ","### Ceil logarithm ","### Computating the logarithm efficiently "}

Nat.log definition
(b : ℕ) : ℕ → ℕ
Full source
/-- `log b n`, is the logarithm of natural number `n` in base `b`. It returns the largest `k : ℕ`
such that `b^k ≤ n`, so if `b^k = n`, it returns exactly `k`. -/
@[pp_nodot]
def log (b : ) : 
  | n => if h : b ≤ n ∧ 1 < b then log b (n / b) + 1 else 0
decreasing_by
  -- putting this in the def triggers the `unusedHavesSuffices` linter:
  -- https://github.com/leanprover-community/batteries/issues/428
  have : n / b < n := div_lt_self ((Nat.zero_lt_one.trans h.2).trans_le h.1) h.2
  decreasing_trivial
Floor logarithm of natural numbers
Informal description
For natural numbers \( b \) and \( n \), the function \( \log_b n \) computes the largest natural number \( k \) such that \( b^k \leq n \). If \( b^k = n \), it returns exactly \( k \). The function is defined recursively: if \( b \leq n \) and \( 1 < b \), then \( \log_b n = \log_b (n / b) + 1 \); otherwise, \( \log_b n = 0 \).
Nat.log_eq_zero_iff theorem
{b n : ℕ} : log b n = 0 ↔ n < b ∨ b ≤ 1
Full source
@[simp]
theorem log_eq_zero_iff {b n : } : loglog b n = 0 ↔ n < b ∨ b ≤ 1 := by
  rw [log, dite_eq_right_iff]
  simp only [Nat.add_eq_zero_iff, Nat.one_ne_zero, and_false, imp_false, not_and_or, not_le, not_lt]
Characterization of Zero Floor Logarithm: $\log_b n = 0 \leftrightarrow (n < b \lor b \leq 1)$
Informal description
For natural numbers $b$ and $n$, the floor logarithm $\log_b n$ equals zero if and only if either $n < b$ or $b \leq 1$.
Nat.log_of_lt theorem
{b n : ℕ} (hb : n < b) : log b n = 0
Full source
theorem log_of_lt {b n : } (hb : n < b) : log b n = 0 :=
  log_eq_zero_iff.2 (Or.inl hb)
Floor Logarithm Vanishes for Small Argument: $\log_b n = 0$ when $n < b$
Informal description
For any natural numbers $b$ and $n$, if $n < b$, then the floor logarithm $\log_b n$ equals zero.
Nat.log_of_left_le_one theorem
{b : ℕ} (hb : b ≤ 1) (n) : log b n = 0
Full source
theorem log_of_left_le_one {b : } (hb : b ≤ 1) (n) : log b n = 0 :=
  log_eq_zero_iff.2 (Or.inr hb)
Floor Logarithm Vanishes for Base $\leq 1$: $\log_b n = 0$ when $b \leq 1$
Informal description
For any natural numbers $b$ and $n$, if $b \leq 1$, then the floor logarithm $\log_b n$ equals zero.
Nat.log_pos theorem
{b n : ℕ} (hb : 1 < b) (hbn : b ≤ n) : 0 < log b n
Full source
@[bound]
theorem log_pos {b n : } (hb : 1 < b) (hbn : b ≤ n) : 0 < log b n :=
  log_pos_iff.2 ⟨hbn, hb⟩
Positivity of Floor Logarithm: $\log_b n > 0$ when $1 < b \leq n$
Informal description
For natural numbers $b$ and $n$, if $1 < b$ and $b \leq n$, then the floor logarithm $\log_b n$ is positive.
Nat.log_of_one_lt_of_le theorem
{b n : ℕ} (h : 1 < b) (hn : b ≤ n) : log b n = log b (n / b) + 1
Full source
theorem log_of_one_lt_of_le {b n : } (h : 1 < b) (hn : b ≤ n) : log b n = log b (n / b) + 1 := by
  rw [log]
  exact if_pos ⟨hn, h⟩
Recursive Formula for Floor Logarithm: $\log_b n = \log_b (n/b) + 1$ when $1 < b \leq n$
Informal description
For natural numbers $b$ and $n$ with $1 < b$ and $b \leq n$, the floor logarithm satisfies $\log_b n = \log_b (n / b) + 1$.
Nat.log_zero_left theorem
: ∀ n, log 0 n = 0
Full source
@[simp] lemma log_zero_left : ∀ n, log 0 n = 0 := log_of_left_le_one <| Nat.zero_le _
Floor Logarithm with Base Zero: $\log_0 n = 0$
Informal description
For any natural number $n$, the floor logarithm with base $0$ satisfies $\log_0 n = 0$.
Nat.log_zero_right theorem
(b : ℕ) : log b 0 = 0
Full source
@[simp]
theorem log_zero_right (b : ) : log b 0 = 0 :=
  log_eq_zero_iff.2 (le_total 1 b)
Floor Logarithm of Zero: $\log_b 0 = 0$
Informal description
For any natural number $b$, the floor logarithm of $0$ with base $b$ is $0$, i.e., $\log_b 0 = 0$.
Nat.log_one_left theorem
: ∀ n, log 1 n = 0
Full source
@[simp]
theorem log_one_left : ∀ n, log 1 n = 0 :=
  log_of_left_le_one le_rfl
Floor Logarithm with Base One: $\log_1 n = 0$
Informal description
For any natural number $n$, the floor logarithm with base $1$ satisfies $\log_1 n = 0$.
Nat.log_one_right theorem
(b : ℕ) : log b 1 = 0
Full source
@[simp]
theorem log_one_right (b : ) : log b 1 = 0 :=
  log_eq_zero_iff.2 (lt_or_le _ _)
Floor Logarithm of One: $\log_b 1 = 0$
Informal description
For any natural number $b$, the floor logarithm of $1$ with base $b$ is $0$, i.e., $\log_b 1 = 0$.
Nat.pow_le_iff_le_log theorem
{b : ℕ} (hb : 1 < b) {x y : ℕ} (hy : y ≠ 0) : b ^ x ≤ y ↔ x ≤ log b y
Full source
/-- `pow b` and `log b` (almost) form a Galois connection. See also `Nat.pow_le_of_le_log` and
`Nat.le_log_of_pow_le` for individual implications under weaker assumptions. -/
theorem pow_le_iff_le_log {b : } (hb : 1 < b) {x y : } (hy : y ≠ 0) :
    b ^ x ≤ y ↔ x ≤ log b y := by
  induction y using Nat.strong_induction_on generalizing x with | h y ih => ?_
  cases x with
  | zero => dsimp; omega
  | succ x =>
    rw [log]; split_ifs with h
    · have b_pos : 0 < b := lt_of_succ_lt hb
      rw [Nat.add_le_add_iff_right, ← ih (y / b) (div_lt_self
        (Nat.pos_iff_ne_zero.2 hy) hb) (Nat.div_pos h.1 b_pos).ne', le_div_iff_mul_le b_pos,
        pow_succ', Nat.mul_comm]
    · exact iff_of_false (fun hby => h ⟨(le_self_pow x.succ_ne_zero _).trans hby, hb⟩)
        (not_succ_le_zero _)
Galois Connection Between Exponentiation and Floor Logarithm: $b^x \leq y \leftrightarrow x \leq \log_b y$
Informal description
For natural numbers $b > 1$, $x$, and $y \neq 0$, the inequality $b^x \leq y$ holds if and only if $x \leq \log_b y$, where $\log_b y$ is the greatest natural number $k$ such that $b^k \leq y$.
Nat.lt_pow_iff_log_lt theorem
{b : ℕ} (hb : 1 < b) {x y : ℕ} (hy : y ≠ 0) : y < b ^ x ↔ log b y < x
Full source
theorem lt_pow_iff_log_lt {b : } (hb : 1 < b) {x y : } (hy : y ≠ 0) : y < b ^ x ↔ log b y < x :=
  lt_iff_lt_of_le_iff_le (pow_le_iff_le_log hb hy)
Strict Inequality Version of Galois Connection: $y < b^x \leftrightarrow \log_b y < x$
Informal description
For natural numbers $b > 1$, $x$, and $y \neq 0$, the inequality $y < b^x$ holds if and only if $\log_b y < x$, where $\log_b y$ is the greatest natural number $k$ such that $b^k \leq y$.
Nat.pow_le_of_le_log theorem
{b x y : ℕ} (hy : y ≠ 0) (h : x ≤ log b y) : b ^ x ≤ y
Full source
theorem pow_le_of_le_log {b x y : } (hy : y ≠ 0) (h : x ≤ log b y) : b ^ x ≤ y := by
  refine (le_or_lt b 1).elim (fun hb => ?_) fun hb => (pow_le_iff_le_log hb hy).2 h
  rw [log_of_left_le_one hb, Nat.le_zero] at h
  rwa [h, Nat.pow_zero, one_le_iff_ne_zero]
Exponentiation Bound via Floor Logarithm: $x \leq \log_b y \Rightarrow b^x \leq y$
Informal description
For any natural numbers $b$, $x$, and $y \neq 0$, if $x \leq \log_b y$, then $b^x \leq y$, where $\log_b y$ is the greatest natural number $k$ such that $b^k \leq y$.
Nat.le_log_of_pow_le theorem
{b x y : ℕ} (hb : 1 < b) (h : b ^ x ≤ y) : x ≤ log b y
Full source
theorem le_log_of_pow_le {b x y : } (hb : 1 < b) (h : b ^ x ≤ y) : x ≤ log b y := by
  rcases ne_or_eq y 0 with (hy | rfl)
  exacts [(pow_le_iff_le_log hb hy).1 h, (h.not_lt (Nat.pow_pos (Nat.zero_lt_one.trans hb))).elim]
Floor Logarithm Lower Bound from Power Inequality: $b^x \leq y \Rightarrow x \leq \log_b y$
Informal description
For natural numbers $b > 1$, $x$, and $y$, if $b^x \leq y$, then $x \leq \log_b y$, where $\log_b y$ is the greatest natural number $k$ such that $b^k \leq y$.
Nat.pow_log_le_self theorem
(b : ℕ) {x : ℕ} (hx : x ≠ 0) : b ^ log b x ≤ x
Full source
theorem pow_log_le_self (b : ) {x : } (hx : x ≠ 0) : b ^ log b x ≤ x :=
  pow_le_of_le_log hx le_rfl
Power of Floor Logarithm Bound: $b^{\log_b x} \leq x$ for $x \neq 0$
Informal description
For any natural numbers $b$ and $x \neq 0$, the inequality $b^{\log_b x} \leq x$ holds, where $\log_b x$ is the greatest natural number $k$ such that $b^k \leq x$.
Nat.log_lt_of_lt_pow theorem
{b x y : ℕ} (hy : y ≠ 0) : y < b ^ x → log b y < x
Full source
theorem log_lt_of_lt_pow {b x y : } (hy : y ≠ 0) : y < b ^ x → log b y < x :=
  lt_imp_lt_of_le_imp_le (pow_le_of_le_log hy)
Floor Logarithm Upper Bound from Power Inequality: $y < b^x \Rightarrow \log_b y < x$
Informal description
For any natural numbers $b$, $x$, and $y \neq 0$, if $y < b^x$, then $\log_b y < x$, where $\log_b y$ is the greatest natural number $k$ such that $b^k \leq y$.
Nat.lt_pow_of_log_lt theorem
{b x y : ℕ} (hb : 1 < b) : log b y < x → y < b ^ x
Full source
theorem lt_pow_of_log_lt {b x y : } (hb : 1 < b) : log b y < x → y < b ^ x :=
  lt_imp_lt_of_le_imp_le (le_log_of_pow_le hb)
Power Upper Bound from Floor Logarithm Inequality: $\log_b y < x \Rightarrow y < b^x$
Informal description
For natural numbers $b > 1$, $x$, and $y$, if $\log_b y < x$, then $y < b^x$, where $\log_b y$ is the greatest natural number $k$ such that $b^k \leq y$.
Nat.log_lt_self theorem
(b : ℕ) {x : ℕ} (hx : x ≠ 0) : log b x < x
Full source
lemma log_lt_self (b : ) {x : } (hx : x ≠ 0) : log b x < x :=
  match le_or_lt b 1 with
  | .inl h => log_of_left_le_one h x ▸ Nat.pos_iff_ne_zero.2 hx
  | .inr h => log_lt_of_lt_pow hx <| Nat.lt_pow_self h
Floor Logarithm is Strictly Less Than Input: $\log_b x < x$ for $x \neq 0$
Informal description
For any natural numbers $b$ and $x \neq 0$, the floor logarithm $\log_b x$ is strictly less than $x$, i.e., $\log_b x < x$.
Nat.lt_pow_succ_log_self theorem
{b : ℕ} (hb : 1 < b) (x : ℕ) : x < b ^ (log b x).succ
Full source
theorem lt_pow_succ_log_self {b : } (hb : 1 < b) (x : ) : x < b ^ (log b x).succ :=
  lt_pow_of_log_lt hb (lt_succ_self _)
Upper bound via floor logarithm: $x < b^{\log_b x + 1}$
Informal description
For any natural numbers $b > 1$ and $x$, we have $x < b^{(\log_b x) + 1}$, where $\log_b x$ is the greatest natural number $k$ such that $b^k \leq x$.
Nat.log_eq_iff theorem
{b m n : ℕ} (h : m ≠ 0 ∨ 1 < b ∧ n ≠ 0) : log b n = m ↔ b ^ m ≤ n ∧ n < b ^ (m + 1)
Full source
theorem log_eq_iff {b m n : } (h : m ≠ 0m ≠ 0 ∨ 1 < b ∧ n ≠ 0) :
    loglog b n = m ↔ b ^ m ≤ n ∧ n < b ^ (m + 1) := by
  rcases em (1 < b ∧ n ≠ 0) with (⟨hb, hn⟩ | hbn)
  · rw [le_antisymm_iff, ← Nat.lt_succ_iff, ← pow_le_iff_le_log, ← lt_pow_iff_log_lt,
      and_comm] <;> assumption
  have hm : m ≠ 0 := h.resolve_right hbn
  rw [not_and_or, not_lt, Ne, not_not] at hbn
  rcases hbn with (hb | rfl)
  · obtain rfl | rfl := le_one_iff_eq_zero_or_eq_one.1 hb
    any_goals
      simp only [ne_eq, zero_eq, reduceSucc, lt_self_iff_false,  not_lt_zero, false_and, or_false]
        at h
      simp [h, eq_comm (a := 0), Nat.zero_pow (Nat.pos_iff_ne_zero.2 _)] <;> omega
  · simp [@eq_comm _ 0, hm]
Characterization of Floor Logarithm: $\log_b n = m \leftrightarrow b^m \leq n < b^{m+1}$
Informal description
For natural numbers $b$, $m$, and $n$, if either $m \neq 0$ or ($1 < b$ and $n \neq 0$), then the floor logarithm $\log_b n$ equals $m$ if and only if $b^m \leq n$ and $n < b^{m+1}$.
Nat.log_eq_of_pow_le_of_lt_pow theorem
{b m n : ℕ} (h₁ : b ^ m ≤ n) (h₂ : n < b ^ (m + 1)) : log b n = m
Full source
theorem log_eq_of_pow_le_of_lt_pow {b m n : } (h₁ : b ^ m ≤ n) (h₂ : n < b ^ (m + 1)) :
    log b n = m := by
  rcases eq_or_ne m 0 with (rfl | hm)
  · rw [Nat.pow_one] at h₂
    exact log_of_lt h₂
  · exact (log_eq_iff (Or.inl hm)).2 ⟨h₁, h₂⟩
Floor Logarithm Characterization via Power Bounds: $\log_b n = m$ when $b^m \leq n < b^{m+1}$
Informal description
For natural numbers $b$, $m$, and $n$, if $b^m \leq n$ and $n < b^{m+1}$, then the floor logarithm satisfies $\log_b n = m$.
Nat.log_pow theorem
{b : ℕ} (hb : 1 < b) (x : ℕ) : log b (b ^ x) = x
Full source
theorem log_pow {b : } (hb : 1 < b) (x : ) : log b (b ^ x) = x :=
  log_eq_of_pow_le_of_lt_pow le_rfl (Nat.pow_lt_pow_right hb x.lt_succ_self)
Logarithm of Power Identity: $\log_b(b^x) = x$ for $b > 1$
Informal description
For any natural numbers $b > 1$ and $x$, the floor logarithm satisfies $\log_b (b^x) = x$.
Nat.log_eq_one_iff theorem
{b n : ℕ} : log b n = 1 ↔ n < b * b ∧ 1 < b ∧ b ≤ n
Full source
theorem log_eq_one_iff {b n : } : loglog b n = 1 ↔ n < b * b ∧ 1 < b ∧ b ≤ n :=
  log_eq_one_iff'.trans
    ⟨fun h => ⟨h.2, lt_mul_self_iff.1 (h.1.trans_lt h.2), h.1⟩, fun h => ⟨h.2.2, h.1⟩⟩
Characterization of $\log_b n = 1$ via $b \leq n < b^2$ and $1 < b$
Informal description
For natural numbers $b$ and $n$, the floor logarithm satisfies $\log_b n = 1$ if and only if $n < b^2$, $1 < b$, and $b \leq n$.
Nat.log_mul_base theorem
{b n : ℕ} (hb : 1 < b) (hn : n ≠ 0) : log b (n * b) = log b n + 1
Full source
theorem log_mul_base {b n : } (hb : 1 < b) (hn : n ≠ 0) : log b (n * b) = log b n + 1 := by
  apply log_eq_of_pow_le_of_lt_pow <;> rw [pow_succ', Nat.mul_comm b]
  exacts [Nat.mul_le_mul_right _ (pow_log_le_self _ hn),
    (Nat.mul_lt_mul_right (Nat.zero_lt_one.trans hb)).2 (lt_pow_succ_log_self hb _)]
Logarithm of Product with Base: $\log_b(n \cdot b) = \log_b n + 1$ for $b > 1$ and $n \neq 0$
Informal description
For natural numbers $b > 1$ and $n \neq 0$, the floor logarithm satisfies $\log_b (n \cdot b) = \log_b n + 1$.
Nat.pow_log_le_add_one theorem
(b : ℕ) : ∀ x, b ^ log b x ≤ x + 1
Full source
theorem pow_log_le_add_one (b : ) : ∀ x, b ^ log b x ≤ x + 1
  | 0 => by rw [log_zero_right, Nat.pow_zero]
  | x + 1 => (pow_log_le_self b x.succ_ne_zero).trans (x + 1).le_succ
Power of Floor Logarithm Bound with Offset: $b^{\log_b x} \leq x + 1$
Informal description
For any natural numbers $b$ and $x$, the inequality $b^{\log_b x} \leq x + 1$ holds, where $\log_b x$ is the greatest natural number $k$ such that $b^k \leq x$.
Nat.log_monotone theorem
{b : ℕ} : Monotone (log b)
Full source
theorem log_monotone {b : } : Monotone (log b) := by
  refine monotone_nat_of_le_succ fun n => ?_
  rcases le_or_lt b 1 with hb | hb
  · rw [log_of_left_le_one hb]
    exact zero_le _
  · exact le_log_of_pow_le hb (pow_log_le_add_one _ _)
Monotonicity of the Floor Logarithm Function $\log_b$
Informal description
For any natural number $b$, the floor logarithm function $\log_b$ is monotone. That is, for any natural numbers $n$ and $m$, if $n \leq m$, then $\log_b n \leq \log_b m$.
Nat.log_mono_right theorem
{b n m : ℕ} (h : n ≤ m) : log b n ≤ log b m
Full source
@[mono]
theorem log_mono_right {b n m : } (h : n ≤ m) : log b n ≤ log b m :=
  log_monotone h
Monotonicity of Floor Logarithm with Respect to Argument: $\log_b n \leq \log_b m$ for $n \leq m$
Informal description
For any natural numbers $b$, $n$, and $m$, if $n \leq m$, then $\log_b n \leq \log_b m$.
Nat.log_anti_left theorem
{b c n : ℕ} (hc : 1 < c) (hb : c ≤ b) : log b n ≤ log c n
Full source
@[mono]
theorem log_anti_left {b c n : } (hc : 1 < c) (hb : c ≤ b) : log b n ≤ log c n := by
  rcases eq_or_ne n 0 with (rfl | hn); · rw [log_zero_right, log_zero_right]
  apply le_log_of_pow_le hc
  calc
    c ^ log b n ≤ b ^ log b n := Nat.pow_le_pow_left hb _
    _ ≤ n := pow_log_le_self _ hn
Floor Logarithm is Antitone in Base: $\log_b n \leq \log_c n$ for $1 < c \leq b$
Informal description
For natural numbers $b$, $c$, and $n$, if $1 < c$ and $c \leq b$, then the floor logarithm satisfies $\log_b n \leq \log_c n$.
Nat.log_antitone_left theorem
{n : ℕ} : AntitoneOn (fun b => log b n) (Set.Ioi 1)
Full source
theorem log_antitone_left {n : } : AntitoneOn (fun b => log b n) (Set.Ioi 1) := fun _ hc _ _ hb =>
  log_anti_left (Set.mem_Iio.1 hc) hb
Antitonicity of Floor Logarithm in Base: $\log_b n$ decreases as $b$ increases for $b > 1$
Informal description
For any natural number $n$, the function $b \mapsto \log_b n$ is antitone on the interval $(1, \infty)$. That is, for any $b_1, b_2 > 1$ with $b_1 \leq b_2$, we have $\log_{b_2} n \leq \log_{b_1} n$.
Nat.log_div_base theorem
(b n : ℕ) : log b (n / b) = log b n - 1
Full source
@[simp]
theorem log_div_base (b n : ) : log b (n / b) = log b n - 1 := by
  rcases le_or_lt b 1 with hb | hb
  · rw [log_of_left_le_one hb, log_of_left_le_one hb, Nat.zero_sub]
  rcases lt_or_le n b with h | h
  · rw [div_eq_of_lt h, log_of_lt h, log_zero_right]
  rw [log_of_one_lt_of_le hb h, Nat.add_sub_cancel_right]
Logarithm of Division by Base: $\log_b(n/b) = \log_b n - 1$
Informal description
For any natural numbers $b$ and $n$, the floor logarithm satisfies $\log_b (n / b) = \log_b n - 1$.
Nat.log_div_mul_self theorem
(b n : ℕ) : log b (n / b * b) = log b n
Full source
@[simp]
theorem log_div_mul_self (b n : ) : log b (n / b * b) = log b n := by
  rcases le_or_lt b 1 with hb | hb
  · rw [log_of_left_le_one hb, log_of_left_le_one hb]
  rcases lt_or_le n b with h | h
  · rw [div_eq_of_lt h, Nat.zero_mul, log_zero_right, log_of_lt h]
  rw [log_mul_base hb (Nat.div_pos h (by omega)).ne', log_div_base,
    Nat.sub_add_cancel (succ_le_iff.2 <| log_pos hb h)]
Logarithm Identity: $\log_b \left(\frac{n}{b} \cdot b\right) = \log_b n$
Informal description
For any natural numbers $b$ and $n$, the floor logarithm satisfies $\log_b \left(\frac{n}{b} \cdot b\right) = \log_b n$.
Nat.add_pred_div_lt theorem
{b n : ℕ} (hb : 1 < b) (hn : 2 ≤ n) : (n + b - 1) / b < n
Full source
theorem add_pred_div_lt {b n : } (hb : 1 < b) (hn : 2 ≤ n) : (n + b - 1) / b < n := by
  rw [div_lt_iff_lt_mul (by omega), ← succ_le_iff, ← pred_eq_sub_one,
    succ_pred_eq_of_pos (by omega)]
  exact Nat.add_le_mul hn hb
Division inequality for natural numbers: $\frac{n + b - 1}{b} < n$ when $b>1$ and $n\geq2$
Informal description
For natural numbers $b > 1$ and $n \geq 2$, we have $\frac{n + b - 1}{b} < n$.
Nat.log2_eq_log_two theorem
{n : ℕ} : Nat.log2 n = Nat.log 2 n
Full source
lemma log2_eq_log_two {n : } : Nat.log2 n = Nat.log 2 n := by
  rcases eq_or_ne n 0 with rfl | hn
  · rw [log2_zero, log_zero_right]
  apply eq_of_forall_le_iff
  intro m
  rw [Nat.le_log2 hn, ← Nat.pow_le_iff_le_log Nat.one_lt_two hn]
Base-2 Logarithm Equivalence: $\log_2 n = \log_2 n$
Informal description
For any natural number $n$, the base-2 logarithm $\log_2 n$ is equal to the floor logarithm $\log_2 n$ computed using the general `Nat.log` function with base 2.
Nat.clog definition
(b : ℕ) : ℕ → ℕ
Full source
/-- `clog b n`, is the upper logarithm of natural number `n` in base `b`. It returns the smallest
`k : ℕ` such that `n ≤ b^k`, so if `b^k = n`, it returns exactly `k`. -/
@[pp_nodot]
def clog (b : ) : 
  | n => if h : 1 < b ∧ 1 < n then clog b ((n + b - 1) / b) + 1 else 0
decreasing_by
  -- putting this in the def triggers the `unusedHavesSuffices` linter:
  -- https://github.com/leanprover-community/batteries/issues/428
  have : (n + b - 1) / b < n := add_pred_div_lt h.1 h.2
  decreasing_trivial
Ceiling logarithm of natural numbers
Informal description
For natural numbers \( b \) and \( n \), the function \(\mathrm{clog}_b(n)\) returns the smallest natural number \( k \) such that \( n \leq b^k \). If \( b \leq 1 \) or \( n \leq 1 \), it returns \( 0 \). Otherwise, it is computed recursively as \( \mathrm{clog}_b(\lceil n / b \rceil) + 1 \), where \(\lceil \cdot \rceil\) denotes the ceiling function.
Nat.clog_of_left_le_one theorem
{b : ℕ} (hb : b ≤ 1) (n : ℕ) : clog b n = 0
Full source
theorem clog_of_left_le_one {b : } (hb : b ≤ 1) (n : ) : clog b n = 0 := by
  rw [clog, dif_neg fun h : 1 < b ∧ 1 < n => h.1.not_le hb]
Ceiling Logarithm Vanishes for Base $\leq 1$
Informal description
For any natural numbers $b$ and $n$, if $b \leq 1$, then the ceiling logarithm $\mathrm{clog}_b(n) = 0$.
Nat.clog_of_right_le_one theorem
{n : ℕ} (hn : n ≤ 1) (b : ℕ) : clog b n = 0
Full source
theorem clog_of_right_le_one {n : } (hn : n ≤ 1) (b : ) : clog b n = 0 := by
  rw [clog, dif_neg fun h : 1 < b ∧ 1 < n => h.2.not_le hn]
Ceiling logarithm vanishes for small $n$
Informal description
For any natural number $n \leq 1$ and any natural number $b$, the ceiling logarithm satisfies $\mathrm{clog}_b(n) = 0$.
Nat.clog_zero_left theorem
(n : ℕ) : clog 0 n = 0
Full source
@[simp] lemma clog_zero_left (n : ) : clog 0 n = 0 := clog_of_left_le_one (Nat.zero_le _) _
Ceiling Logarithm with Base Zero Vanishes
Informal description
For any natural number $n$, the ceiling logarithm with base $0$ satisfies $\mathrm{clog}_0(n) = 0$.
Nat.clog_zero_right theorem
(b : ℕ) : clog b 0 = 0
Full source
@[simp] lemma clog_zero_right (b : ) : clog b 0 = 0 := clog_of_right_le_one (Nat.zero_le _) _
Ceiling logarithm of zero vanishes for any base
Informal description
For any natural number $b$, the ceiling logarithm satisfies $\mathrm{clog}_b(0) = 0$.
Nat.clog_one_left theorem
(n : ℕ) : clog 1 n = 0
Full source
@[simp]
theorem clog_one_left (n : ) : clog 1 n = 0 :=
  clog_of_left_le_one le_rfl _
Ceiling Logarithm with Base One Vanishes
Informal description
For any natural number $n$, the ceiling logarithm with base $1$ satisfies $\mathrm{clog}_1(n) = 0$.
Nat.clog_one_right theorem
(b : ℕ) : clog b 1 = 0
Full source
@[simp]
theorem clog_one_right (b : ) : clog b 1 = 0 :=
  clog_of_right_le_one le_rfl _
Ceiling logarithm of one vanishes for any base
Informal description
For any natural number $b$, the ceiling logarithm satisfies $\mathrm{clog}_b(1) = 0$.
Nat.clog_of_two_le theorem
{b n : ℕ} (hb : 1 < b) (hn : 2 ≤ n) : clog b n = clog b ((n + b - 1) / b) + 1
Full source
theorem clog_of_two_le {b n : } (hb : 1 < b) (hn : 2 ≤ n) :
    clog b n = clog b ((n + b - 1) / b) + 1 := by rw [clog, dif_pos (⟨hb, hn⟩ : 1 < b ∧ 1 < n)]
Recurrence relation for ceiling logarithm when \( b > 1 \) and \( n \geq 2 \)
Informal description
For natural numbers \( b > 1 \) and \( n \geq 2 \), the ceiling logarithm satisfies the recurrence relation: \[ \mathrm{clog}_b(n) = \mathrm{clog}_b\left(\left\lceil \frac{n}{b} \right\rceil\right) + 1, \] where \(\lceil \cdot \rceil\) denotes the ceiling function (implemented as \((n + b - 1)/b\)).
Nat.clog_pos theorem
{b n : ℕ} (hb : 1 < b) (hn : 2 ≤ n) : 0 < clog b n
Full source
theorem clog_pos {b n : } (hb : 1 < b) (hn : 2 ≤ n) : 0 < clog b n := by
  rw [clog_of_two_le hb hn]
  exact zero_lt_succ _
Positivity of Ceiling Logarithm for \(b > 1\) and \(n \geq 2\)
Informal description
For any natural numbers \( b > 1 \) and \( n \geq 2 \), the ceiling logarithm \(\mathrm{clog}_b(n)\) is positive, i.e., \(0 < \mathrm{clog}_b(n)\).
Nat.clog_eq_one theorem
{b n : ℕ} (hn : 2 ≤ n) (h : n ≤ b) : clog b n = 1
Full source
theorem clog_eq_one {b n : } (hn : 2 ≤ n) (h : n ≤ b) : clog b n = 1 := by
  rw [clog_of_two_le (hn.trans h) hn, clog_of_right_le_one]
  rw [← Nat.lt_succ_iff, Nat.div_lt_iff_lt_mul] <;> omega
Ceiling Logarithm Equals One for $2 \leq n \leq b$
Informal description
For any natural numbers $b$ and $n$ such that $2 \leq n \leq b$, the ceiling logarithm satisfies $\mathrm{clog}_b(n) = 1$.
Nat.le_pow_iff_clog_le theorem
{b : ℕ} (hb : 1 < b) {x y : ℕ} : x ≤ b ^ y ↔ clog b x ≤ y
Full source
/-- `clog b` and `pow b` form a Galois connection. -/
theorem le_pow_iff_clog_le {b : } (hb : 1 < b) {x y : } : x ≤ b ^ y ↔ clog b x ≤ y := by
  induction x using Nat.strong_induction_on generalizing y with | h x ih => ?_
  cases y
  · rw [Nat.pow_zero]
    refine ⟨fun h => (clog_of_right_le_one h b).le, ?_⟩
    simp_rw [← not_lt]
    contrapose!
    exact clog_pos hb
  have b_pos : 0 < b := zero_lt_of_lt hb
  rw [clog]; split_ifs with h
  · rw [Nat.add_le_add_iff_right, ← ih ((x + b - 1) / b) (add_pred_div_lt hb h.2),
      Nat.div_le_iff_le_mul_add_pred b_pos, Nat.mul_comm b, ← Nat.pow_succ,
      Nat.add_sub_assoc (Nat.succ_le_of_lt b_pos), Nat.add_le_add_iff_right]
  · exact iff_of_true ((not_lt.1 (not_and.1 h hb)).trans <| succ_le_of_lt <| Nat.pow_pos b_pos)
      (zero_le _)
Ceiling Logarithm Bound: $x \leq b^y \leftrightarrow \mathrm{clog}_b(x) \leq y$
Informal description
For any natural numbers $b > 1$, $x$, and $y$, we have $x \leq b^y$ if and only if the ceiling logarithm $\mathrm{clog}_b(x) \leq y$.
Nat.pow_lt_iff_lt_clog theorem
{b : ℕ} (hb : 1 < b) {x y : ℕ} : b ^ y < x ↔ y < clog b x
Full source
theorem pow_lt_iff_lt_clog {b : } (hb : 1 < b) {x y : } : b ^ y < x ↔ y < clog b x :=
  lt_iff_lt_of_le_iff_le (le_pow_iff_clog_le hb)
Strict Inequality Characterization via Ceiling Logarithm: $b^y < x \leftrightarrow y < \mathrm{clog}_b(x)$
Informal description
For any natural numbers $b > 1$, $x$, and $y$, the inequality $b^y < x$ holds if and only if $y$ is strictly less than the ceiling logarithm $\mathrm{clog}_b(x)$.
Nat.clog_pow theorem
(b x : ℕ) (hb : 1 < b) : clog b (b ^ x) = x
Full source
theorem clog_pow (b x : ) (hb : 1 < b) : clog b (b ^ x) = x :=
  eq_of_forall_ge_iff fun z ↦ by rw [← le_pow_iff_clog_le hb, Nat.pow_le_pow_iff_right hb]
Ceiling Logarithm of Power: $\mathrm{clog}_b(b^x) = x$
Informal description
For any natural numbers $b > 1$ and $x$, the ceiling logarithm of $b^x$ with base $b$ equals $x$, i.e., $\mathrm{clog}_b(b^x) = x$.
Nat.pow_pred_clog_lt_self theorem
{b : ℕ} (hb : 1 < b) {x : ℕ} (hx : 1 < x) : b ^ (clog b x).pred < x
Full source
theorem pow_pred_clog_lt_self {b : } (hb : 1 < b) {x : } (hx : 1 < x) :
    b ^ (clog b x).pred < x := by
  rw [← not_le, le_pow_iff_clog_le hb, not_le]
  exact pred_lt (clog_pos hb hx).ne'
Strict lower bound for $x$ via ceiling logarithm: $b^{\mathrm{clog}_b(x) - 1} < x$
Informal description
For any natural numbers $b > 1$ and $x > 1$, we have $b^{(\mathrm{clog}_b(x)) - 1} < x$, where $\mathrm{clog}_b(x)$ is the ceiling logarithm of $x$ with base $b$.
Nat.le_pow_clog theorem
{b : ℕ} (hb : 1 < b) (x : ℕ) : x ≤ b ^ clog b x
Full source
theorem le_pow_clog {b : } (hb : 1 < b) (x : ) : x ≤ b ^ clog b x :=
  (le_pow_iff_clog_le hb).2 le_rfl
Upper bound via ceiling logarithm: $x \leq b^{\mathrm{clog}_b(x)}$
Informal description
For any natural numbers $b > 1$ and $x$, we have $x \leq b^{\mathrm{clog}_b(x)}$, where $\mathrm{clog}_b(x)$ is the ceiling logarithm of $x$ with base $b$.
Nat.clog_mono_right theorem
(b : ℕ) {n m : ℕ} (h : n ≤ m) : clog b n ≤ clog b m
Full source
@[mono]
theorem clog_mono_right (b : ) {n m : } (h : n ≤ m) : clog b n ≤ clog b m := by
  rcases le_or_lt b 1 with hb | hb
  · rw [clog_of_left_le_one hb]
    exact zero_le _
  · rw [← le_pow_iff_clog_le hb]
    exact h.trans (le_pow_clog hb _)
Monotonicity of Ceiling Logarithm: $n \leq m \Rightarrow \mathrm{clog}_b(n) \leq \mathrm{clog}_b(m)$
Informal description
For any natural number $b$ and any natural numbers $n \leq m$, the ceiling logarithm satisfies $\mathrm{clog}_b(n) \leq \mathrm{clog}_b(m)$.
Nat.clog_anti_left theorem
{b c n : ℕ} (hc : 1 < c) (hb : c ≤ b) : clog b n ≤ clog c n
Full source
@[mono]
theorem clog_anti_left {b c n : } (hc : 1 < c) (hb : c ≤ b) : clog b n ≤ clog c n := by
  rw [← le_pow_iff_clog_le (lt_of_lt_of_le hc hb)]
  calc
    n ≤ c ^ clog c n := le_pow_clog hc _
    _ ≤ b ^ clog c n := Nat.pow_le_pow_left hb _
Monotonicity of Ceiling Logarithm with Respect to Base: $\mathrm{clog}_b(n) \leq \mathrm{clog}_c(n)$ for $c \leq b$ and $1 < c$
Informal description
For natural numbers $b$, $c$, and $n$, if $1 < c$ and $c \leq b$, then the ceiling logarithm satisfies $\mathrm{clog}_b(n) \leq \mathrm{clog}_c(n)$.
Nat.clog_monotone theorem
(b : ℕ) : Monotone (clog b)
Full source
theorem clog_monotone (b : ) : Monotone (clog b) := fun _ _ => clog_mono_right _
Monotonicity of the Ceiling Logarithm Function $\mathrm{clog}_b$
Informal description
For any natural number $b$, the ceiling logarithm function $\mathrm{clog}_b : \mathbb{N} \to \mathbb{N}$ is monotone. That is, for any natural numbers $n \leq m$, we have $\mathrm{clog}_b(n) \leq \mathrm{clog}_b(m)$.
Nat.clog_antitone_left theorem
{n : ℕ} : AntitoneOn (fun b : ℕ => clog b n) (Set.Ioi 1)
Full source
theorem clog_antitone_left {n : } : AntitoneOn (fun b :  => clog b n) (Set.Ioi 1) :=
  fun _ hc _ _ hb => clog_anti_left (Set.mem_Iio.1 hc) hb
Antitonicity of Ceiling Logarithm with Respect to Base: $\mathrm{clog}_b(n) \geq \mathrm{clog}_c(n)$ for $1 < b \leq c$
Informal description
For any natural number $n$, the function $b \mapsto \mathrm{clog}_b(n)$ is antitone (i.e., decreasing) on the interval $(1, \infty)$. In other words, for $b, c \in \mathbb{N}$ with $1 < b \leq c$, we have $\mathrm{clog}_c(n) \leq \mathrm{clog}_b(n)$.
Nat.log_le_clog theorem
(b n : ℕ) : log b n ≤ clog b n
Full source
theorem log_le_clog (b n : ) : log b n ≤ clog b n := by
  obtain hb | hb := le_or_lt b 1
  · rw [log_of_left_le_one hb]
    exact zero_le _
  cases n with
  | zero =>
    rw [log_zero_right]
    exact zero_le _
  | succ n =>
    exact (Nat.pow_le_pow_iff_right hb).1
      ((pow_log_le_self b n.succ_ne_zero).trans <| le_pow_clog hb _)
Comparison of Floor and Ceiling Logarithms: $\log_b n \leq \mathrm{clog}_b n$
Informal description
For any natural numbers $b$ and $n$, the floor logarithm $\log_b n$ is less than or equal to the ceiling logarithm $\mathrm{clog}_b n$.
Nat.logC definition
(b m : ℕ) : ℕ
Full source
/--
An alternate definition for `Nat.log` which computes more efficiently. For mathematical purposes,
use `Nat.log` instead, and see `Nat.log_eq_logC`.

Note a tail-recursive version of `Nat.log` is also possible:
```
def logTR (b n : ℕ) : ℕ :=
  let rec go : ℕ → ℕ → ℕ | n, acc => if h : b ≤ n ∧ 1 < b then go (n / b) (acc + 1) else acc
  decreasing_by
    have : n / b < n := Nat.div_lt_self (by omega) h.2
    decreasing_trivial
  go n 0
```
but performs worse for large numbers than `Nat.logC`:
```
#eval Nat.logTR 2 (2 ^ 1000000)
#eval Nat.logC 2 (2 ^ 1000000)
```

The definition `Nat.logC` is not tail-recursive, however, but the stack limit will only be reached
if the output size is around 2^10000, meaning the input will be around 2^(2^10000), which will
take far too long to compute in the first place.

Adapted from https://downloads.haskell.org/~ghc/9.0.1/docs/html/libraries/ghc-bignum-1.0/GHC-Num-BigNat.html#v:bigNatLogBase-35-
-/
@[pp_nodot] def logC (b m : ) :  :=
  if h : 1 < b then let (_, e) := step b h; e else 0 where
  /--
  An auxiliary definition for `Nat.logC`, where the base of the logarithm is _squared_ in each
  loop. This allows significantly faster computation of the logarithm: it takes logarithmic time
  in the size of the output, rather than linear time.
  -/
  step (pw : ) (hpw : 1 < pw) : ℕ × ℕ :=
    if h : m < pw then (m, 0)
    else
      let (q, e) := step (pw * pw) (Nat.mul_lt_mul_of_lt_of_lt hpw hpw)
      if q < pw then (q, 2 * e) else (q / pw, 2 * e + 1)
  termination_by m / pw
  decreasing_by
    have : m / (pw * pw) < m / pw := logC_aux hpw (le_of_not_lt h)
    decreasing_trivial
Optimized floor logarithm for natural numbers
Informal description
The function `Nat.logC` computes the floor logarithm of a natural number `m` with base `b`, i.e., the greatest natural number `k` such that `b^k ≤ m`. This is an optimized version of `Nat.log` that computes more efficiently by squaring the base in each iteration, resulting in logarithmic time complexity with respect to the output size.
Nat.log_eq_logC theorem
: log = logC
Full source
/--
The result of `Nat.log` agrees with the result of `Nat.logC`. The latter will be computed more
efficiently, but the former is easier to prove things about and has more lemmas.
This lemma is tagged @[csimp] so that the code generated for `Nat.log` uses `Nat.logC` instead.
-/
@[csimp] theorem log_eq_logC : log = logC := by
  ext b m
  rcases le_or_lt b 1 with hb | hb
  case inl => rw [logC_of_left_le_one hb, Nat.log_of_left_le_one hb]
  case inr =>
    rcases eq_or_ne m 0 with rfl | hm
    case inl => rw [Nat.log_zero_right, logC_zero]
    case inr =>
      rw [Nat.log_eq_iff (Or.inr ⟨hb, hm⟩)]
      exact logC_spec hb (zero_lt_of_ne_zero hm)
Equality of Floor Logarithm and Optimized Floor Logarithm: $\log = \logC$
Informal description
The floor logarithm function $\log_b n$ and the optimized floor logarithm function $\logC_b n$ are equal for all natural numbers $b$ and $n$, i.e., $\log = \logC$.