1-- Integer roots of products and of roots, and cancelling powers2-- 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.RootLemmas8import Root910-- Section: Cancelling powers11-- Equal exponents cancel: a ^ s ≤ b ^ s gives a ≤ b.12theorem pow.base_cancel (a b s : Nat)13requires positive : 1≤ s14requires below : a ^ s ≤ b ^ s15shows a ≤ b16proof17have pa := pow.closed at a, s18have pb := pow.closed at b, s19have grows := rootNat.monotone at s, a ^ s, b ^ s with positive, below20have ra := rootNat.pow at s, a with positive21have rb := rootNat.pow at s, b with positive22rw [ra, rb] at grows23exact grows2425-- A base above 1 cancels: b ^ m ≤ b ^ n gives m ≤ n.26theorem pow.exponent_cancel (b m n : Nat)27requires above : 1 < b28requires below : b ^ m ≤ b ^ n29shows m ≤ n30proof31havesplit:= Less.or_LessEqual at n, m32casessplitwith larger | atMost33 . have grows := pow.exponent_strict at b, n, m with above, larger34have pm := pow.closed at b, m35have pn := pow.closed at b, n36have notBelow := LessEqual.not_Less at b ^ m, b ^ n with below37exfalso38exact notBelow with grows39 . exact atMost4041-- Section: Bounds by the root42-- 1 ≤ ⌊n^(1/q)⌋ for 1 ≤ n.43theorem rootNat.ge_one (q n : Nat)44requires positive : 1≤ q45requires one : 1≤ n46shows1≤ rootNat(q, n)47proof48have unit := pow.one_left at q49have adjunction := rootNat.le_iff at q, n, 1with positive50rw unit at adjunction51exact adjunction.backward with one5253-- The floor costs at most a factor 2 ^ q:54-- n ≤ 2 ^ q · ⌊n^(1/q)⌋ ^ q for 1 ≤ n.55theorem rootNat.pow_lower (q n : Nat)56requires positive : 1≤ q57requires one : 1≤ n58shows n ≤2 ^ q * rootNat(q, n) ^ q59proof60have rNat := rootNat.natural at q, n with positive61have rOne := rootNat.ge_one at q, n with positive, one62have above := rootNat.lt_succ_pow at q, n with positive63have step := LessEqual.add_monotone_left at641, rootNat(q, n), rootNat(q, n) with rOne65have twice : rootNat(q, n) + rootNat(q, n) =2* rootNat(q, n) byring66rw twice at step67have grows := pow.base_monotone at68 rootNat(q, n) +1, 2* rootNat(q, n), q with step69havesplit:= pow.mul_base at2, rootNat(q, n), q70rwsplitat grows71have succPow := pow.closed at rootNat(q, n) +1, q72have twoPow := pow.closed at2, q73have rootPow := pow.closed at rootNat(q, n), q74have strict := Less.transitive_LessEqual at n, (rootNat(q, n) +1) ^ q,752 ^ q * rootNat(q, n) ^ q with above, grows76have succ := Less.add_one_le_of_lt at n, 2 ^ q * rootNat(q, n) ^ q with strict77haveself:= LessEqual.add_one at n78exact LessEqual.transitive at n, n +1, 2 ^ q * rootNat(q, n) ^ q with79self, succ8081-- Section: Roots of products82-- ⌊X^(1/q)⌋ · ⌊Y^(1/q)⌋ ≤ ⌊(X · Y)^(1/q)⌋.83theorem rootNat.mul_lower (q X Y : Nat)84requires positive : 1≤ q85shows rootNat(q, X) * rootNat(q, Y) ≤ rootNat(q, X * Y)86proof87have xNat := rootNat.natural at q, X with positive88have yNat := rootNat.natural at q, Y with positive89have xBelow := rootNat.pow_le at q, X with positive90have yBelow := rootNat.pow_le at q, Y with positive91have xPow := pow.closed at rootNat(q, X), q92have yPow := pow.closed at rootNat(q, Y), q93have both := LessEqual.mul at rootNat(q, X) ^ q, X, rootNat(q, Y) ^ q, Y with94 xBelow, yBelow95havesplit:= pow.mul_base at rootNat(q, X), rootNat(q, Y), q96rw←splitat both97have adjunction := rootNat.le_iff at98 q, X * Y, rootNat(q, X) * rootNat(q, Y) with positive99exact adjunction.backward with both100101-- ⌊(X · Y)^(1/q)⌋ < (⌊X^(1/q)⌋ + 1) · (⌊Y^(1/q)⌋ + 1).102theorem rootNat.mul_upper (q X Y : Nat)103requires positive : 1≤ q104shows rootNat(q, X * Y) < (rootNat(q, X) +1) * (rootNat(q, Y) +1)105proof106have xNat := rootNat.natural at q, X with positive107have yNat := rootNat.natural at q, Y with positive108have xAbove := rootNat.lt_succ_pow at q, X with positive109have yAbove := rootNat.lt_succ_pow at q, Y with positive110have xPow := pow.closed at rootNat(q, X) +1, q111have yPow := pow.closed at rootNat(q, Y) +1, q112have both := Less.mul at X, (rootNat(q, X) +1) ^ q, Y, (rootNat(q,113 Y) +1) ^ q with xAbove, yAbove114havesplit:= pow.mul_base at rootNat(q, X) +1, rootNat(q, Y) +1, q115rw←splitat both116have adjunction := rootNat.lt_iff at q, X * Y, (rootNat(q,117 X) +1) * (rootNat(q, Y) +1) with positive118exact adjunction.backward with both119120-- A q-th power factors out from below:121-- ⌊X^(1/q)⌋ · Y ≤ ⌊(X · Y ^ q)^(1/q)⌋.122theorem rootNat.mul_pow_lower (q X Y : Nat)123requires positive : 1≤ q124shows rootNat(q, X) * Y ≤ rootNat(q, X * Y ^ q)125proof126have rNat := rootNat.natural at q, X with positive127have rPow := pow.closed at rootNat(q, X), q128have yPow := pow.closed at Y, q129have below := rootNat.pow_le at q, X with positive130have scaled := LessEqual.mul_right at rootNat(q, X) ^ q, X, Y ^ q with below131havesplit:= pow.mul_base at rootNat(q, X), Y, q132rw←splitat scaled133have adjunction := rootNat.le_iff at q, X * Y ^ q, rootNat(q, X) * Y with134 positive135exact adjunction.backward with scaled136137-- And from above, for 1 ≤ Y:138-- ⌊(X · Y ^ q)^(1/q)⌋ < (⌊X^(1/q)⌋ + 1) · Y.139theorem rootNat.mul_pow_upper (q X Y : Nat)140requires positive : 1≤ q141requires one : 1≤ Y142shows rootNat(q, X * Y ^ q) < (rootNat(q, X) +1) * Y143proof144have rNat := rootNat.natural at q, X with positive145have rPow := pow.closed at rootNat(q, X) +1, q146have yPow := pow.closed at Y, q147have above := rootNat.lt_succ_pow at q, X with positive148have yPositive := (LessEqual.one_iff_positive at Y).forward with one149have powPositive := pow.positive at Y, q with yPositive150have nonzero := Less.ne_zero_of_positive at Y ^ q with powPositive151have scaled := Less.mul_right at X, (rootNat(q, X) +1) ^ q, Y ^ q with152 nonzero, above153havesplit:= pow.mul_base at rootNat(q, X) +1, Y, q154rw←splitat scaled155have adjunction := rootNat.lt_iff at156 q, X * Y ^ q, (rootNat(q, X) +1) * Y with positive157exact adjunction.backward with scaled158159-- Section: Roots of roots160-- ⌊y^(1/(s·d))⌋ ^ d ≤ ⌊y^(1/s)⌋.161theorem rootNat.mul_index_lower (s d y : Nat)162requires positive : 1≤ s163requires positiveD : 1≤ d164shows rootNat(s * d, y) ^ d ≤ rootNat(s, y)165proof166have product := LessEqual.one_le_mul at s, d with positive, positiveD167have rootNatural := rootNat.natural at s * d, y with product168have power := pow.closed at rootNat(s * d, y), d169have below := rootNat.pow_le at s * d, y with product170have regroup := pow.mul_exponent at rootNat(s * d, y), d, s171have swap : d * s = s * d byring172rw swap at regroup173rw← regroup at below174have adjunction := rootNat.le_iff at s, y, rootNat(s * d, y) ^ d with positive175exact adjunction.backward with below176177-- ⌊y^(1/s)⌋ < (⌊y^(1/(s·d))⌋ + 1) ^ d.178theorem rootNat.mul_index_upper (s d y : Nat)179requires positive : 1≤ s180requires positiveD : 1≤ d181shows rootNat(s, y) < (rootNat(s * d, y) +1) ^ d182proof183have product := LessEqual.one_le_mul at s, d with positive, positiveD184have rootNatural := rootNat.natural at s * d, y with product185have power := pow.closed at rootNat(s * d, y) +1, d186have above := rootNat.lt_succ_pow at s * d, y with product187have regroup := pow.mul_exponent at rootNat(s * d, y) +1, d, s188have swap : d * s = s * d byring189rw swap at regroup190rw← regroup at above191have adjunction := rootNat.lt_iff at s, y, (rootNat(s * d, y) +1) ^ d with192 positive193exact adjunction.backward with above194