The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.Nat.Log

80 declarations. Mathlib docs · Source

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 (b ^ ·). See le_log_iff_pow_le and clog_le_iff_le_pow.

Implementation notes

We define both functions using recursion on b. In order to compute, e.g., Nat.log b n, we compute e = Nat.log (b * b) n first, then figure out whether the answer is 2 * e or 2 * e + 1. The actual implementations use fuel recursion so that (by decide : Nat.log 2 20 = 4) works.

Adapted from https://downloads.haskell.org/~ghc/9.0.1/docs/html/libraries/ghc-bignum-1.0/GHC-Num-BigNat.html#v:bigNatLogBase-35-

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 lia) h.2
    decreasing_trivial
  go n 0

but performs worse for large numbers than Nat.log:

#eval Nat.logTR 2 (2 ^ 1000000)
#eval Nat.log 2 (2 ^ 1000000)

Floor logarithm

Ceil logarithm

theorem Nat.log.go.fun_cases_unfolding E1 #

∀ (n : ℕ) (motive : ℕ → ℕ → ℕ × ℕ → Prop),
  (∀ (x : ℕ), motive x 0 (n, 0)) →
    (∀ (b fuel : ℕ), n < b → motive b fuel.succ (n, 0)) →
      (∀ (b fuel : ℕ),
          ¬n < b → ∀ (q e : ℕ), Nat.log.go n (b * b) fuel = (q, e) → q < b → motive b fuel.succ (q, 2 * e)) →
        (∀ (b fuel : ℕ),
            ¬n < b → ∀ (q e : ℕ), Nat.log.go n (b * b) fuel = (q, e) → ¬q < b → motive b fuel.succ (q / b, 2 * e + 1)) →
          ∀ (a a_1 : ℕ), motive a a_1 (Nat.log.go n a a_1)

theorem Nat.log.go.match_1.congr_eq_1 E1 #

∀ (motive : ℕ × ℕ → Sort u_1) (x : ℕ × ℕ) (h_1 : (q e : ℕ) → motive (q, e)) (q e : ℕ),
  x = (q, e) →
    (match x with
      | (q, e) => h_1 q e) ≍
      h_1 q e

theorem Nat.log.go.match_3.congr_eq_2 E1 #

∀ (motive : ℕ → ℕ → Sort u_1) (x x_1 : ℕ) (h_1 : (x : ℕ) → motive x 0) (h_2 : (b fuel : ℕ) → motive b fuel.succ)
  (b fuel : ℕ),
  x = b →
    x_1 = fuel.succ →
      (match x, x_1 with
        | x, 0 => h_1 x
        | b, fuel.succ => h_2 b fuel) ≍
        h_2 b fuel

theorem Nat.log.fun_cases_unfolding E1 #

∀ (b n : ℕ) (motive : ℕ → Prop), (b ≤ 1 → motive 0) → (¬b ≤ 1 → motive (Nat.log.go n b n).2) → motive (Nat.log b n)

theorem Nat.log.go.match_3.congr_eq_1 E1 #

∀ (motive : ℕ → ℕ → Sort u_1) (x x_1 : ℕ) (h_1 : (x : ℕ) → motive x 0) (h_2 : (b fuel : ℕ) → motive b fuel.succ)
  (x_2 : ℕ),
  x = x_2 →
    x_1 = 0 →
      (match x, x_1 with
        | x, 0 => h_1 x
        | b, fuel.succ => h_2 b fuel) ≍
        h_1 x_2

theorem Nat.clog.fun_cases_unfolding E1 #

∀ (b n : ℕ) (motive : ℕ → Prop),
  (1 < b ∧ 1 < n → motive ((Nat.clog.go n b n).2 + 1)) → (¬(1 < b ∧ 1 < n) → motive 0) → motive (Nat.clog b n)

def Nat.log E1 #

ℕ → ℕ → ℕ

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.

def Nat.log.go E1 #

ℕ → ℕ → ℕ → ℕ × ℕ

An auxiliary definition for Nat.log.

For b > 1, n ≠ 0, n < b ^ fuel, Nat.log.go n b fuel = (n / b ^ b.log n, b.log n).

theorem Nat.log_of_left_le_one E1 #

∀ {b : ℕ}, b ≤ 1 → ∀ (n : ℕ), Nat.log b n = 0

theorem Nat.log_of_lt E1 #

∀ {b n : ℕ}, n < b → Nat.log b n = 0

theorem Nat.log.go_spec E1 #

∀ {b n fuel : ℕ},
  1 < b →
    n ≠ 0 →
      n < b ^ fuel →
        (Nat.log.go n b fuel).1 = n / b ^ (Nat.log.go n b fuel).2 ∧
          b ^ (Nat.log.go n b fuel).2 ≤ n ∧ n < b ^ ((Nat.log.go n b fuel).2 + 1)

theorem Nat.log_lt_iff_lt_pow E1 #

∀ {b : ℕ}, 1 < b → ∀ {x y : ℕ}, y ≠ 0 → (Nat.log b y < x ↔ y < b ^ x)

theorem Nat.log_eq_zero_iff E0 #

∀ {b n : ℕ}, Nat.log b n = 0 ↔ n < b ∨ b ≤ 1

theorem Nat.log_pos_iff E0 #

∀ {b n : ℕ}, 0 < Nat.log b n ↔ b ≤ n ∧ 1 < b

theorem Nat.log_pos E1 #

∀ {b n : ℕ}, 1 < b → b ≤ n → 0 < Nat.log b n

theorem Nat.log_of_one_lt_of_le E1 #

∀ {b n : ℕ}, 1 < b → b ≤ n → Nat.log b n = Nat.log b (n / b) + 1

theorem Nat.log_zero_left E0 #

∀ (n : ℕ), Nat.log 0 n = 0

theorem Nat.log_zero_right E0 #

∀ (b : ℕ), Nat.log b 0 = 0

theorem Nat.log_one_left E0 #

∀ (n : ℕ), Nat.log 1 n = 0

theorem Nat.log_one_right E0 #

∀ (b : ℕ), Nat.log b 1 = 0

theorem Nat.le_log_iff_pow_le E1 #

∀ {b : ℕ}, 1 < b → ∀ {x y : ℕ}, y ≠ 0 → (x ≤ Nat.log b y ↔ b ^ x ≤ y)

(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 Nat.pow_le_of_le_log E1 #

∀ {b x y : ℕ}, y ≠ 0 → x ≤ Nat.log b y → b ^ x ≤ y

theorem Nat.le_log_of_pow_le E1 #

∀ {b x y : ℕ}, 1 < b → b ^ x ≤ y → x ≤ Nat.log b y

theorem Nat.pow_log_le_self E1 #

∀ (b : ℕ) {x : ℕ}, x ≠ 0 → b ^ Nat.log b x ≤ x

theorem Nat.log_lt_of_lt_pow E1 #

∀ {b x y : ℕ}, y ≠ 0 → y < b ^ x → Nat.log b y < x

See also log_lt_of_lt_pow' for a version that assumes x ≠ 0 instead of y ≠ 0.

theorem Nat.log_lt_of_lt_pow' E1 #

∀ {b x y : ℕ}, x ≠ 0 → y < b ^ x → Nat.log b y < x

A version of log_lt_of_lt_pow that assumes x ≠ 0 instead of y ≠ 0.

theorem Nat.lt_pow_of_log_lt E1 #

∀ {b x y : ℕ}, 1 < b → Nat.log b y < x → y < b ^ x

theorem Nat.log_lt_self E1 #

∀ (b : ℕ) {x : ℕ}, x ≠ 0 → Nat.log b x < x

theorem Nat.log_le_self E1 #

∀ (b x : ℕ), Nat.log b x ≤ x

theorem Nat.lt_pow_succ_log_self E1 #

∀ {b : ℕ}, 1 < b → ∀ (x : ℕ), x < b ^ (Nat.log b x).succ

theorem Nat.log_eq_iff E0 #

∀ {b m n : ℕ}, m ≠ 0 ∨ 1 < b ∧ n ≠ 0 → (Nat.log b n = m ↔ b ^ m ≤ n ∧ n < b ^ (m + 1))

theorem Nat.log_eq_of_pow_le_of_lt_pow E1 #

∀ {b m n : ℕ}, b ^ m ≤ n → n < b ^ (m + 1) → Nat.log b n = m

theorem Nat.log_pow E0 #

∀ {b : ℕ}, 1 < b → ∀ (x : ℕ), Nat.log b (b ^ x) = x

theorem Nat.log_eq_one_iff' E1 #

∀ {b n : ℕ}, Nat.log b n = 1 ↔ b ≤ n ∧ n < b * b

theorem Nat.log_eq_one_iff E0 #

∀ {b n : ℕ}, Nat.log b n = 1 ↔ n < b * b ∧ 1 < b ∧ b ≤ n

theorem Nat.log_mul_base E0 #

∀ {b n : ℕ}, 1 < b → n ≠ 0 → Nat.log b (n * b) = Nat.log b n + 1

theorem Nat.pow_log_le_add_one E1 #

∀ (b x : ℕ), b ^ Nat.log b x ≤ x + 1

theorem Nat.log_monotone E1 #

∀ {b : ℕ}, Monotone (Nat.log b)

theorem Nat.log_mono_right E1 #

∀ {b n m : ℕ}, n ≤ m → Nat.log b n ≤ Nat.log b m

theorem Nat.log_lt_log_succ_iff E0 #

∀ (b : ℕ) {n : ℕ}, n ≠ 0 → (Nat.log b n < Nat.log b (n + 1) ↔ b ^ Nat.log b (n + 1) = n + 1)

theorem Nat.log_eq_log_succ_iff E0 #

∀ (b : ℕ) {n : ℕ}, n ≠ 0 → (Nat.log b n = Nat.log b (n + 1) ↔ b ^ Nat.log b (n + 1) ≠ n + 1)

theorem Nat.log_anti_left E1 #

∀ {b c n : ℕ}, 1 < c → c ≤ b → Nat.log b n ≤ Nat.log c n

theorem Nat.log_antitone_left E1 #

∀ {n : ℕ}, AntitoneOn (fun b ↦ Nat.log b n) (Set.Ioi 1)

theorem Nat.log_mono E1 #

∀ {b c m n : ℕ}, 1 < c → c ≤ b → m ≤ n → Nat.log b m ≤ Nat.log c n

theorem Nat.log_div_base E0 #

∀ (b n : ℕ), Nat.log b (n / b) = Nat.log b n - 1

theorem Nat.log_div_base_pow E1 #

∀ (b n k : ℕ), Nat.log b (n / b ^ k) = Nat.log b n - k

theorem Nat.log_div_mul_self E0 #

∀ (b n : ℕ), Nat.log b (n / b * b) = Nat.log b n

theorem Nat.add_pred_div_lt E1 #

∀ {b n : ℕ}, 1 < b → 2 ≤ n → (n + b - 1) / b < n

theorem Nat.log_two_bit E1 #

∀ {b : Bool} {n : ℕ}, n ≠ 0 → Nat.log 2 (Nat.bit b n) = Nat.log 2 n + 1

theorem Nat.log2_eq_log_two E1 #

∀ {n : ℕ}, n.log2 = Nat.log 2 n

theorem Nat.log_pow_left E0 #

∀ (b k n : ℕ), Nat.log (b ^ k) n = Nat.log b n / k

def Nat.clog E1 #

ℕ → ℕ → ℕ

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.

def Nat.clog.go E1 #

ℕ → ℕ → ℕ → ℕ × ℕ

An auxiliary definition for Nat.clog.

For n > 1, b > 1, n ≤ b ^ fuel, returns (b ^ clog b n / n, clog b n - 1).

theorem Nat.clog_of_left_le_one E1 #

∀ {b : ℕ}, b ≤ 1 → ∀ (n : ℕ), Nat.clog b n = 0

theorem Nat.clog_of_right_le_one E1 #

∀ {n : ℕ}, n ≤ 1 → ∀ (b : ℕ), Nat.clog b n = 0

theorem Nat.clog_zero_left E0 #

∀ (n : ℕ), Nat.clog 0 n = 0

theorem Nat.clog_zero_right E0 #

∀ (b : ℕ), Nat.clog b 0 = 0

theorem Nat.clog_one_left E0 #

∀ (n : ℕ), Nat.clog 1 n = 0

theorem Nat.clog_one_right E0 #

∀ (b : ℕ), Nat.clog b 1 = 0

theorem Nat.clog.go_spec E1 #

∀ {n b fuel : ℕ},
  1 < n →
    1 < b →
      n < b ^ fuel →
        (Nat.clog.go n b fuel).1 = b ^ ((Nat.clog.go n b fuel).2 + 1) / n ∧
          b ^ (Nat.clog.go n b fuel).2 < n ∧ n ≤ b ^ ((Nat.clog.go n b fuel).2 + 1)

theorem Nat.clog_le_iff_le_pow E1 #

∀ {b : ℕ}, 1 < b → ∀ {x y : ℕ}, Nat.clog b x ≤ y ↔ x ≤ b ^ y

For b > 1, clog b and (b ^ ·) form a Galois connection.

See also clog_le_of_le_pow for the implication that does not require 1 < b.

theorem Nat.clog_pos E1 #

∀ {b n : ℕ}, 1 < b → 1 < n → 0 < Nat.clog b n

theorem Nat.clog_of_one_lt E1 #

∀ {b n : ℕ}, 1 < b → 1 < n → Nat.clog b n = Nat.clog b ((n + b - 1) / b) + 1

theorem Nat.clog_of_two_le E1 #

∀ {b n : ℕ}, 1 < b → 2 ≤ n → Nat.clog b n = Nat.clog b ((n + b - 1) / b) + 1

theorem Nat.clog_eq_one E1 #

∀ {b n : ℕ}, 2 ≤ n → n ≤ b → Nat.clog b n = 1

theorem Nat.clog_le_of_le_pow E1 #

∀ {b x y : ℕ}, x ≤ b ^ y → Nat.clog b x ≤ y

theorem Nat.lt_clog_iff_pow_lt E1 #

∀ {b : ℕ}, 1 < b → ∀ {x y : ℕ}, y < Nat.clog b x ↔ b ^ y < x

theorem Nat.pow_lt_of_lt_clog E1 #

∀ {b x y : ℕ}, y < Nat.clog b x → b ^ y < x

theorem Nat.clog_pow E0 #

∀ (b x : ℕ), 1 < b → Nat.clog b (b ^ x) = x

theorem Nat.pow_pred_clog_lt_self E1 #

∀ (b : ℕ) {x : ℕ}, 1 < x → b ^ (Nat.clog b x).pred < x

theorem Nat.le_pow_clog E1 #

∀ {b : ℕ}, 1 < b → ∀ (x : ℕ), x ≤ b ^ Nat.clog b x

theorem Nat.clog_mono_right E1 #

∀ (b : ℕ) {n m : ℕ}, n ≤ m → Nat.clog b n ≤ Nat.clog b m

theorem Nat.clog_anti_left E1 #

∀ {b c n : ℕ}, 1 < c → c ≤ b → Nat.clog b n ≤ Nat.clog c n

theorem Nat.clog_monotone E1 #

∀ (b : ℕ), Monotone (Nat.clog b)

theorem Nat.clog_antitone_left E1 #

∀ {n : ℕ}, AntitoneOn (fun b ↦ Nat.clog b n) (Set.Ioi 1)

theorem Nat.clog_mono E1 #

∀ {b c m n : ℕ}, 1 < c → c ≤ b → m ≤ n → Nat.clog b m ≤ Nat.clog c n

theorem Nat.log_le_clog E0 #

∀ (b n : ℕ), Nat.log b n ≤ Nat.clog b n

theorem Nat.clog_lt_clog_succ_iff E0 #

∀ {b n : ℕ}, 1 < b → (Nat.clog b n < Nat.clog b (n + 1) ↔ b ^ Nat.clog b n = n)

theorem Nat.clog_eq_clog_succ_iff E0 #

∀ {b n : ℕ}, 1 < b → (Nat.clog b n = Nat.clog b (n + 1) ↔ b ^ Nat.clog b n ≠ n)

theorem Nat.clog_pow_left E2 #

∀ (b k n : ℕ), Nat.clog (b ^ k) n = (Nat.clog b n + (k - 1)) / k

This lemma says that ⌈log (b ^ k) n⌉ = ⌈(⌈log b n⌉ / k)⌉, using operations on natural numbers to express this equality.

Since Lean has no dedicated function for the ceiling division, we use (a + (b - 1)) / b for ⌈a / b⌉.