Grace/Cumulus/Nat/Root.g17

Open in Workbench Download source

Imports

Least · PowLemmas

Declarations

Source

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)15  requires positive : 1 ≤ q16  shows n ∈ rootCandidates(q, n)17proof18  have one : 1 ≤ n + 1 by arith19  have grows := pow.exponent_monotone at n + 1, 1, q with one, positive20  rw pow.one_right at grows21  have nNat : Nat(n) by typecheck22  have nMember := nat.omega.forward with nNat23  have bound : n < (n + 1) ^ q by arith24  simp [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 ≤ q31proof32  have candidate := rootCandidates.inhabited at q, n with positive33  have inhabited : Nonempty(rootCandidates(q, n)) := ⟨n, candidate⟩34  have naturals : rootCandidates(q, n) ⊆ ℕ by simp35  have least := leastNatural.member with *36  obtain ⟨leastMember, above⟩ := rootCandidates.membership.forward with least37  have leastNat := nat.omega.backward with leastMember38  have shape := natural.zero_or_successor with leastNat39  witness leastNatural(rootCandidates(q, n))40  refine ⟨leastMember, _, above⟩41  cases shape with zero | successor42  . rw zero43    have qPositive : 0 < q by arith44    obtain ⟨p, pNat, shifted⟩ := Less.exists_add_one at 0, q with qPositive45    have exponent : q = p + 1 by arith46    rw exponent47    rw pow.zero_left48    arith49  . obtain ⟨j, jNat, successorEquation⟩ := successor50    have plusOne := add.right_one at j51    rw ← plusOne at successorEquation52    have equation : leastNatural(rootCandidates(q, n)) = j + 153      exact successorEquation54    rw equation55    have split := Less.or_LessEqual at n, (j + 1) ^ q56    cases split with jAbove | below57    . have jMember := nat.omega.forward with jNat58      have jCandidate : j ∈ rootCandidates(q, n)59        simp [rootCandidates.membership, jMember, jAbove]60      have bound := leastNatural.least at rootCandidates(q, n), j with *61      rw equation at bound62      exfalso63      arith64    . exact below65uniqueness66  intros r rMember rBounds s sMember sBounds67  have rNat := nat.omega.backward with rMember68  have sNat := nat.omega.backward with sMember69  rcases rBounds with ⟨rLower, rUpper⟩70  rcases sBounds with ⟨sLower, sUpper⟩71  have rAtMost : r ≤ s72    have split := Less.or_LessEqual at s, r73    cases split with above | atMost74    . have step : s + 1 ≤ r by arith75      have grows := pow.base_monotone at s + 1, r, q with step76      exfalso77      arith78    . exact atMost79  have sAtMost : s ≤ r80    have split := Less.or_LessEqual at r, s81    cases split with above | atMost82    . have step : r + 1 ≤ s by arith83      have grows := pow.base_monotone at r + 1, s, q with step84      exfalso85      arith86    . exact atMost87  arith8889theorem rootNat.natural (q n : Nat)90  requires positive : 1 ≤ q91  shows Nat(rootNat(q, n))92proof93  obtain ⟨member, bounds⟩ := rootNat.selected at q, n with *94  exact nat.omega.backward with member9596theorem rootNat.pow_le (q n : Nat)97  requires positive : 1 ≤ q98  shows rootNat(q, n) ^ q ≤ n99proof100  obtain ⟨member, lower, upper⟩ := rootNat.selected at q, n with *101  exact lower102103theorem rootNat.lt_succ_pow (q n : Nat)104  requires positive : 1 ≤ q105  shows n < (rootNat(q, n) + 1) ^ q106proof107  obtain ⟨member, lower, upper⟩ := rootNat.selected at q, n with *108  exact upper109110-- The bounds characterize the root.111theorem rootNat.eq_of_bounds (q n r : Nat)112  requires positive : 1 ≤ q113  requires lower : r ^ q ≤ n114  requires upper : n < (r + 1) ^ q115  shows rootNat(q, n) = r116proof117  obtain ⟨member, rootLower, rootUpper⟩ := rootNat.selected at q, n with *118  have rNat : Nat(r) by typecheck119  have rMember := nat.omega.forward with rNat120  have unique := rootNat.unique at q, n with positive121  have rootBounds : rootNat(q, n) ^ q ≤ n ∧ n < (rootNat(q, n) + 1) ^ q122    exact ⟨rootLower, rootUpper⟩123  have rBounds : r ^ q ≤ n ∧ n < (r + 1) ^ q := ⟨lower, upper⟩124  exact 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)128  requires positive : 1 ≤ q129  shows r ≤ rootNat(q, n) ↔ r ^ q ≤ n130proof131  have natural := rootNat.natural at q, n with positive132  have lower := rootNat.pow_le at q, n with positive133  have upper := rootNat.lt_succ_pow at q, n with positive134  constructor135  . intro below136    have grows := pow.base_monotone at r, rootNat(q, n), q with below137    arith138  . intro bound139    have split := Less.or_LessEqual at rootNat(q, n), r140    cases split with above | atMost141    . have step : rootNat(q, n) + 1 ≤ r by arith142      have grows := pow.base_monotone at rootNat(q, n) + 1, r, q with step143      exfalso144      arith145    . exact atMost146147-- ⌊n^(1/q)⌋ < r exactly when n < r ^ q.148theorem rootNat.lt_iff (q n r : Nat)149  requires positive : 1 ≤ q150  shows rootNat(q, n) < r ↔ n < r ^ q151proof152  have natural := rootNat.natural at q, n with positive153  have adjunction := rootNat.le_iff at q, n, r with positive154  have left := Less.iff_not_LessEqual at rootNat(q, n), r155  have right := Less.iff_not_LessEqual at n, r ^ q156  rw [left, right, adjunction]157158theorem rootNat.pow (q r : Nat)159  requires positive : 1 ≤ q160  shows rootNat(q, r ^ q) = r161proof162  have qPositive : 0 < q by arith163  obtain ⟨p, pNat, shifted⟩ := Less.exists_add_one at 0, q with qPositive164  have exponent : q = p + 1 by arith165  have rNat : Nat(r) by typecheck166  have lessSucc : r < r + 1 by arith167  have strict := pow.base_strict at r, r + 1, p with lessSucc168  rw ← exponent at strict169  have lower : r ^ q ≤ r ^ q by arith170  exact rootNat.eq_of_bounds with positive, lower, strict171172theorem rootNat.monotone (q n m : Nat)173  requires positive : 1 ≤ q174  requires below : n ≤ m175  shows rootNat(q, n) ≤ rootNat(q, m)176proof177  have natural := rootNat.natural at q, n with positive178  have lower := rootNat.pow_le at q, n with positive179  have bound : rootNat(q, n) ^ q ≤ m by arith180  have adjunction := rootNat.le_iff at q, m, rootNat(q, n) with positive181  exact adjunction.backward with bound182183theorem rootNat.le_self (q n : Nat)184  requires positive : 1 ≤ q185  shows rootNat(q, n) ≤ n186proof187  have natural := rootNat.natural at q, n with positive188  have lower := rootNat.pow_le at q, n with positive189  have shape := natural.zero_or_successor at rootNat(q, n)190  cases shape with zero | successor191  . rw zero192    arith193  . have one : 1 ≤ rootNat(q, n)194      obtain ⟨j, jNat, equation⟩ := successor195      have plusOne := add.right_one at j196      rw ← plusOne at equation197      arith198    have grows := pow.exponent_monotone at rootNat(q, n), 1, q with199      one, positive200    rw pow.one_right at grows201    arith202