1-- Integer logarithms: the exponent k with b ^ k ≤ n < b ^ (k + 1)2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: András Salamon67module Grace.Cumulus.Nat.Log8import Least9import PowLemmas1011-- The exponents whose next power exceeds n.12def logCandidates (b n : Nat) : Set:= { k ∈ ℕ | n < b ^ (k +1) }1314theorem logCandidates.inhabited (b n : Nat)15requires base : 2≤ b16shows n ∈ logCandidates(b, n)17proof18have twoBelow := pow.two_gt at n +119have baseBelow := pow.base_monotone at2, b, n +1with base20have nNat : Nat(n) bytypecheck21have nMember := nat.omega.forward with nNat22have bound : n < b ^ (n +1) byarith23simp [logCandidates, nMember, bound]2425-- The least candidate is the integer logarithm. Outside the stated26-- hypotheses no exponent qualifies and the value is 0.27defunique logNat (b n : Nat) (k ∈ ℕ) :=28 b ^ k ≤ n ∧ n < b ^ (k +1)29requires base : 2≤ b30requires positive : 1≤ n31proof32have candidate := logCandidates.inhabited at b, n with base33have inhabited : Nonempty(logCandidates(b, n)) :=⟨n, candidate⟩34have naturals : logCandidates(b, n) ⊆ ℕ bysimp35have least := leastNatural.member with*36obtain⟨leastMember, above⟩:= logCandidates.membership.forward with least37have leastNat := nat.omega.backward with leastMember38have shape := natural.zero_or_successor with leastNat39witness leastNatural(logCandidates(b, n))40refine⟨leastMember, _, above⟩41cases shape with zero | successor42 . rw zero43simp [pow]44 . obtain⟨j, jNat, successorEquation⟩:= successor45have plusOne := add.right_one at j46rw← plusOne at successorEquation47have equation : leastNatural(logCandidates(b, n)) = j +148exact successorEquation49rw equation50havesplit:= Less.or_LessEqual at n, b ^ (j +1)51casessplitwith jAbove | below52 . have jMember := nat.omega.forward with jNat53have jCandidate : j ∈ logCandidates(b, n)54simp [logCandidates.membership, jMember, jAbove]55have bound := leastNatural.least at logCandidates(b, n), j with*56rw equation at bound57exfalso58arith59 . exact below60uniqueness61intros k kMember kBounds m mMember mBounds62have kNat := nat.omega.backward with kMember63have mNat := nat.omega.backward with mMember64rcases kBounds with⟨kLower, kUpper⟩65rcases mBounds with⟨mLower, mUpper⟩66have one : 1≤ b byarith67have kAtMost : k ≤ m68havesplit:= Less.or_LessEqual at m, k69casessplitwith above | atMost70 . have step : m +1≤ k byarith71have grows := pow.exponent_monotone at b, m +1, k with one, step72exfalso73arith74 . exact atMost75have mAtMost : m ≤ k76havesplit:= Less.or_LessEqual at k, m77casessplitwith above | atMost78 . have step : k +1≤ m byarith79have grows := pow.exponent_monotone at b, k +1, m with one, step80exfalso81arith82 . exact atMost83arith8485theorem logNat.natural (b n : Nat)86requires base : 2≤ b87requires positive : 1≤ n88showsNat(logNat(b, n))89proof90obtain⟨member, bounds⟩:= logNat.selected at b, n with*91exact nat.omega.backward with member9293theorem logNat.pow_le (b n : Nat)94requires base : 2≤ b95requires positive : 1≤ n96shows b ^ logNat(b, n) ≤ n97proof98obtain⟨member, lower, upper⟩:= logNat.selected at b, n with*99exact lower100101theorem logNat.lt_pow_succ (b n : Nat)102requires base : 2≤ b103requires positive : 1≤ n104shows n < b ^ (logNat(b, n) +1)105proof106obtain⟨member, lower, upper⟩:= logNat.selected at b, n with*107exact upper108109-- The bounds characterize the logarithm.110theorem logNat.eq_of_bounds (b n k : Nat)111requires base : 2≤ b112requires lower : b ^ k ≤ n113requires upper : n < b ^ (k +1)114shows logNat(b, n) = k115proof116have one : 1≤ b byarith117have powerPositive := pow.ge_one at b, k with one118have positive : 1≤ n byarith119obtain⟨member, logLower, logUpper⟩:= logNat.selected at b, n with*120have kNat : Nat(k) bytypecheck121have kMember := nat.omega.forward with kNat122have unique := logNat.unique at b, n with base, positive123have logBounds : b ^ logNat(b, n) ≤ n ∧ n < b ^ (logNat(b, n) +1)124exact⟨logLower, logUpper⟩125have kBounds : b ^ k ≤ n ∧ n < b ^ (k +1) :=⟨lower, upper⟩126exact unique with logNat(b, n), member, logBounds, k, kMember, kBounds127128-- a ≤ ⌊log_b n⌋ exactly when b ^ a ≤ n.129theorem logNat.le_iff (b n a : Nat)130requires base : 2≤ b131requires positive : 1≤ n132shows a ≤ logNat(b, n) ↔ b ^ a ≤ n133proof134have natural := logNat.natural with*135have lower := logNat.pow_le with*136have upper := logNat.lt_pow_succ with*137have one : 1≤ b byarith138constructor139 . intro below140have grows := pow.exponent_monotone at b, a, logNat(b, n) with one, below141arith142 . intro bound143havesplit:= Less.or_LessEqual at logNat(b, n), a144casessplitwith above | atMost145 . have step : logNat(b, n) +1≤ a byarith146have grows := pow.exponent_monotone at b, logNat(b, n) +1, a with147 one, step148exfalso149arith150 . exact atMost151152-- ⌊log_b n⌋ < a exactly when n < b ^ a.153theorem logNat.lt_iff (b n a : Nat)154requires base : 2≤ b155requires positive : 1≤ n156shows logNat(b, n) < a ↔ n < b ^ a157proof158have natural := logNat.natural with*159have adjunction := logNat.le_iff at b, n, a with*160haveleft:= Less.iff_not_LessEqual at logNat(b, n), a161haveright:= Less.iff_not_LessEqual at n, b ^ a162rw [left, right, adjunction]163164theorem logNat.pow (b k : Nat)165requires base : 2≤ b166shows logNat(b, b ^ k) = k167proof168have above : 1 < b byarith169have kNat : Nat(k) bytypecheck170have lessSucc : k < k +1byarith171have upper := pow.exponent_strict at b, k, k +1with above, lessSucc172have lower : b ^ k ≤ b ^ k byarith173exact logNat.eq_of_bounds with base, lower, upper174175theorem logNat.monotone (b n m : Nat)176requires base : 2≤ b177requires positive : 1≤ n178requires below : n ≤ m179shows logNat(b, n) ≤ logNat(b, m)180proof181have mPositive : 1≤ m byarith182have natural := logNat.natural at b, n with base, positive183have lower := logNat.pow_le at b, n with base, positive184have bound : b ^ logNat(b, n) ≤ m byarith185have adjunction := logNat.le_iff at b, m, logNat(b, n) with base, mPositive186exact adjunction.backward with bound187188theorem logNat.lt_self (b n : Nat)189requires base : 2≤ b190requires positive : 1≤ n191shows logNat(b, n) < n192proof193have natural := logNat.natural with*194have lower := logNat.pow_le with*195have twoAbove := pow.two_gt at logNat(b, n)196have baseAbove := pow.base_monotone at2, b, logNat(b, n) with base197arith198