1-- Integer roots: the base r with r ^ q ≤ n < (r + 1) ^ q2-- 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.Root8import Least9import PowLemmas1011-- The bases whose next power exceeds n.12def rootCandidates (q n : Nat) : Set:= { r ∈ ℕ | n < (r +1) ^ q }1314theorem rootCandidates.inhabited (q n : Nat)15requires positive : 1≤ q16shows n ∈ rootCandidates(q, n)17proof18have one : 1≤ n +1byarith19have grows := pow.exponent_monotone at n +1, 1, q with one, positive20rw pow.one_right at grows21have nNat : Nat(n) bytypecheck22have nMember := nat.omega.forward with nNat23have bound : n < (n +1) ^ q byarith24simp [rootCandidates, nMember, bound]2526-- The least candidate is the integer q-th root. For q = 0 no base27-- qualifies and the value is 0.28defunique rootNat (q n : Nat) (r ∈ ℕ) :=29 r ^ q ≤ n ∧ n < (r +1) ^ q30requires positive : 1≤ q31proof32have candidate := rootCandidates.inhabited at q, n with positive33have inhabited : Nonempty(rootCandidates(q, n)) :=⟨n, candidate⟩34have naturals : rootCandidates(q, n) ⊆ ℕ bysimp35have least := leastNatural.member with*36obtain⟨leastMember, above⟩:= rootCandidates.membership.forward with least37have leastNat := nat.omega.backward with leastMember38have shape := natural.zero_or_successor with leastNat39witness leastNatural(rootCandidates(q, n))40refine⟨leastMember, _, above⟩41cases shape with zero | successor42 . rw zero43have qPositive : 0 < q byarith44obtain⟨p, pNat, shifted⟩:= Less.exists_add_one at0, q with qPositive45have exponent : q = p +1byarith46rw exponent47rw pow.zero_left48arith49 . obtain⟨j, jNat, successorEquation⟩:= successor50have plusOne := add.right_one at j51rw← plusOne at successorEquation52have equation : leastNatural(rootCandidates(q, n)) = j +153exact successorEquation54rw equation55havesplit:= Less.or_LessEqual at n, (j +1) ^ q56casessplitwith jAbove | below57 . have jMember := nat.omega.forward with jNat58have jCandidate : j ∈ rootCandidates(q, n)59simp [rootCandidates.membership, jMember, jAbove]60have bound := leastNatural.least at rootCandidates(q, n), j with*61rw equation at bound62exfalso63arith64 . exact below65uniqueness66intros r rMember rBounds s sMember sBounds67have rNat := nat.omega.backward with rMember68have sNat := nat.omega.backward with sMember69rcases rBounds with⟨rLower, rUpper⟩70rcases sBounds with⟨sLower, sUpper⟩71have rAtMost : r ≤ s72havesplit:= Less.or_LessEqual at s, r73casessplitwith above | atMost74 . have step : s +1≤ r byarith75have grows := pow.base_monotone at s +1, r, q with step76exfalso77arith78 . exact atMost79have sAtMost : s ≤ r80havesplit:= Less.or_LessEqual at r, s81casessplitwith above | atMost82 . have step : r +1≤ s byarith83have grows := pow.base_monotone at r +1, s, q with step84exfalso85arith86 . exact atMost87arith8889theorem rootNat.natural (q n : Nat)90requires positive : 1≤ q91showsNat(rootNat(q, n))92proof93obtain⟨member, bounds⟩:= rootNat.selected at q, n with*94exact nat.omega.backward with member9596theorem rootNat.pow_le (q n : Nat)97requires positive : 1≤ q98shows rootNat(q, n) ^ q ≤ n99proof100obtain⟨member, lower, upper⟩:= rootNat.selected at q, n with*101exact lower102103theorem rootNat.lt_succ_pow (q n : Nat)104requires positive : 1≤ q105shows n < (rootNat(q, n) +1) ^ q106proof107obtain⟨member, lower, upper⟩:= rootNat.selected at q, n with*108exact upper109110-- The bounds characterize the root.111theorem rootNat.eq_of_bounds (q n r : Nat)112requires positive : 1≤ q113requires lower : r ^ q ≤ n114requires upper : n < (r +1) ^ q115shows rootNat(q, n) = r116proof117obtain⟨member, rootLower, rootUpper⟩:= rootNat.selected at q, n with*118have rNat : Nat(r) bytypecheck119have rMember := nat.omega.forward with rNat120have unique := rootNat.unique at q, n with positive121have rootBounds : rootNat(q, n) ^ q ≤ n ∧ n < (rootNat(q, n) +1) ^ q122exact⟨rootLower, rootUpper⟩123have rBounds : r ^ q ≤ n ∧ n < (r +1) ^ q :=⟨lower, upper⟩124exact unique with rootNat(q, n), member, rootBounds, r, rMember, rBounds125126-- r ≤ ⌊n^(1/q)⌋ exactly when r ^ q ≤ n.127theorem rootNat.le_iff (q n r : Nat)128requires positive : 1≤ q129shows r ≤ rootNat(q, n) ↔ r ^ q ≤ n130proof131have natural := rootNat.natural at q, n with positive132have lower := rootNat.pow_le at q, n with positive133have upper := rootNat.lt_succ_pow at q, n with positive134constructor135 . intro below136have grows := pow.base_monotone at r, rootNat(q, n), q with below137arith138 . intro bound139havesplit:= Less.or_LessEqual at rootNat(q, n), r140casessplitwith above | atMost141 . have step : rootNat(q, n) +1≤ r byarith142have grows := pow.base_monotone at rootNat(q, n) +1, r, q with step143exfalso144arith145 . exact atMost146147-- ⌊n^(1/q)⌋ < r exactly when n < r ^ q.148theorem rootNat.lt_iff (q n r : Nat)149requires positive : 1≤ q150shows rootNat(q, n) < r ↔ n < r ^ q151proof152have natural := rootNat.natural at q, n with positive153have adjunction := rootNat.le_iff at q, n, r with positive154haveleft:= Less.iff_not_LessEqual at rootNat(q, n), r155haveright:= Less.iff_not_LessEqual at n, r ^ q156rw [left, right, adjunction]157158theorem rootNat.pow (q r : Nat)159requires positive : 1≤ q160shows rootNat(q, r ^ q) = r161proof162have qPositive : 0 < q byarith163obtain⟨p, pNat, shifted⟩:= Less.exists_add_one at0, q with qPositive164have exponent : q = p +1byarith165have rNat : Nat(r) bytypecheck166have lessSucc : r < r +1byarith167have strict := pow.base_strict at r, r +1, p with lessSucc168rw← exponent at strict169have lower : r ^ q ≤ r ^ q byarith170exact rootNat.eq_of_bounds with positive, lower, strict171172theorem rootNat.monotone (q n m : Nat)173requires positive : 1≤ q174requires below : n ≤ m175shows rootNat(q, n) ≤ rootNat(q, m)176proof177have natural := rootNat.natural at q, n with positive178have lower := rootNat.pow_le at q, n with positive179have bound : rootNat(q, n) ^ q ≤ m byarith180have adjunction := rootNat.le_iff at q, m, rootNat(q, n) with positive181exact adjunction.backward with bound182183theorem rootNat.le_self (q n : Nat)184requires positive : 1≤ q185shows rootNat(q, n) ≤ n186proof187have natural := rootNat.natural at q, n with positive188have lower := rootNat.pow_le at q, n with positive189have shape := natural.zero_or_successor at rootNat(q, n)190cases shape with zero | successor191 . rw zero192arith193 . have one : 1≤ rootNat(q, n)194obtain⟨j, jNat, equation⟩:= successor195have plusOne := add.right_one at j196rw← plusOne at equation197arith198have grows := pow.exponent_monotone at rootNat(q, n), 1, q with199 one, positive200rw pow.one_right at grows201arith202