-- Integer logarithms: the exponent k with b ^ k ≤ n < b ^ (k + 1) -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: András Salamon module Grace.Cumulus.Nat.Log import Least import PowLemmas -- The exponents whose next power exceeds n. def logCandidates (b n : Nat) : Set := { k ∈ ℕ | n < b ^ (k + 1) } theorem logCandidates.inhabited (b n : Nat) requires base : 2 ≤ b shows n ∈ logCandidates(b, n) proof have twoBelow := pow.two_gt at n + 1 have baseBelow := pow.base_monotone at 2, b, n + 1 with base have nNat : Nat(n) by typecheck have nMember := nat.omega.forward with nNat have bound : n < b ^ (n + 1) by arith simp [logCandidates, nMember, bound] -- The least candidate is the integer logarithm. Outside the stated -- hypotheses no exponent qualifies and the value is 0. defunique logNat (b n : Nat) (k ∈ ℕ) := b ^ k ≤ n ∧ n < b ^ (k + 1) requires base : 2 ≤ b requires positive : 1 ≤ n proof have candidate := logCandidates.inhabited at b, n with base have inhabited : Nonempty(logCandidates(b, n)) := ⟨n, candidate⟩ have naturals : logCandidates(b, n) ⊆ ℕ by simp have least := leastNatural.member with * obtain ⟨leastMember, above⟩ := logCandidates.membership.forward with least have leastNat := nat.omega.backward with leastMember have shape := natural.zero_or_successor with leastNat witness leastNatural(logCandidates(b, n)) refine ⟨leastMember, _, above⟩ cases shape with zero | successor . rw zero simp [pow] . obtain ⟨j, jNat, successorEquation⟩ := successor have plusOne := add.right_one at j rw ← plusOne at successorEquation have equation : leastNatural(logCandidates(b, n)) = j + 1 exact successorEquation rw equation have split := Less.or_LessEqual at n, b ^ (j + 1) cases split with jAbove | below . have jMember := nat.omega.forward with jNat have jCandidate : j ∈ logCandidates(b, n) simp [logCandidates.membership, jMember, jAbove] have bound := leastNatural.least at logCandidates(b, n), j with * rw equation at bound exfalso arith . exact below uniqueness intros k kMember kBounds m mMember mBounds have kNat := nat.omega.backward with kMember have mNat := nat.omega.backward with mMember rcases kBounds with ⟨kLower, kUpper⟩ rcases mBounds with ⟨mLower, mUpper⟩ have one : 1 ≤ b by arith have kAtMost : k ≤ m have split := Less.or_LessEqual at m, k cases split with above | atMost . have step : m + 1 ≤ k by arith have grows := pow.exponent_monotone at b, m + 1, k with one, step exfalso arith . exact atMost have mAtMost : m ≤ k have split := Less.or_LessEqual at k, m cases split with above | atMost . have step : k + 1 ≤ m by arith have grows := pow.exponent_monotone at b, k + 1, m with one, step exfalso arith . exact atMost arith theorem logNat.natural (b n : Nat) requires base : 2 ≤ b requires positive : 1 ≤ n shows Nat(logNat(b, n)) proof obtain ⟨member, bounds⟩ := logNat.selected at b, n with * exact nat.omega.backward with member theorem logNat.pow_le (b n : Nat) requires base : 2 ≤ b requires positive : 1 ≤ n shows b ^ logNat(b, n) ≤ n proof obtain ⟨member, lower, upper⟩ := logNat.selected at b, n with * exact lower theorem logNat.lt_pow_succ (b n : Nat) requires base : 2 ≤ b requires positive : 1 ≤ n shows n < b ^ (logNat(b, n) + 1) proof obtain ⟨member, lower, upper⟩ := logNat.selected at b, n with * exact upper -- The bounds characterize the logarithm. theorem logNat.eq_of_bounds (b n k : Nat) requires base : 2 ≤ b requires lower : b ^ k ≤ n requires upper : n < b ^ (k + 1) shows logNat(b, n) = k proof have one : 1 ≤ b by arith have powerPositive := pow.ge_one at b, k with one have positive : 1 ≤ n by arith obtain ⟨member, logLower, logUpper⟩ := logNat.selected at b, n with * have kNat : Nat(k) by typecheck have kMember := nat.omega.forward with kNat have unique := logNat.unique at b, n with base, positive have logBounds : b ^ logNat(b, n) ≤ n ∧ n < b ^ (logNat(b, n) + 1) exact ⟨logLower, logUpper⟩ have kBounds : b ^ k ≤ n ∧ n < b ^ (k + 1) := ⟨lower, upper⟩ exact unique with logNat(b, n), member, logBounds, k, kMember, kBounds -- a ≤ ⌊log_b n⌋ exactly when b ^ a ≤ n. theorem logNat.le_iff (b n a : Nat) requires base : 2 ≤ b requires positive : 1 ≤ n shows a ≤ logNat(b, n) ↔ b ^ a ≤ n proof have natural := logNat.natural with * have lower := logNat.pow_le with * have upper := logNat.lt_pow_succ with * have one : 1 ≤ b by arith constructor . intro below have grows := pow.exponent_monotone at b, a, logNat(b, n) with one, below arith . intro bound have split := Less.or_LessEqual at logNat(b, n), a cases split with above | atMost . have step : logNat(b, n) + 1 ≤ a by arith have grows := pow.exponent_monotone at b, logNat(b, n) + 1, a with one, step exfalso arith . exact atMost -- ⌊log_b n⌋ < a exactly when n < b ^ a. theorem logNat.lt_iff (b n a : Nat) requires base : 2 ≤ b requires positive : 1 ≤ n shows logNat(b, n) < a ↔ n < b ^ a proof have natural := logNat.natural with * have adjunction := logNat.le_iff at b, n, a with * have left := Less.iff_not_LessEqual at logNat(b, n), a have right := Less.iff_not_LessEqual at n, b ^ a rw [left, right, adjunction] theorem logNat.pow (b k : Nat) requires base : 2 ≤ b shows logNat(b, b ^ k) = k proof have above : 1 < b by arith have kNat : Nat(k) by typecheck have lessSucc : k < k + 1 by arith have upper := pow.exponent_strict at b, k, k + 1 with above, lessSucc have lower : b ^ k ≤ b ^ k by arith exact logNat.eq_of_bounds with base, lower, upper theorem logNat.monotone (b n m : Nat) requires base : 2 ≤ b requires positive : 1 ≤ n requires below : n ≤ m shows logNat(b, n) ≤ logNat(b, m) proof have mPositive : 1 ≤ m by arith have natural := logNat.natural at b, n with base, positive have lower := logNat.pow_le at b, n with base, positive have bound : b ^ logNat(b, n) ≤ m by arith have adjunction := logNat.le_iff at b, m, logNat(b, n) with base, mPositive exact adjunction.backward with bound theorem logNat.lt_self (b n : Nat) requires base : 2 ≤ b requires positive : 1 ≤ n shows logNat(b, n) < n proof have natural := logNat.natural with * have lower := logNat.pow_le with * have twoAbove := pow.two_gt at logNat(b, n) have baseAbove := pow.base_monotone at 2, b, logNat(b, n) with base arith