A charged native integer k-th root #
The k-th root Nat.nthRoot k n, the largest a with a ^ k ≤ n, is found by binary search on
[0, n + 1). Each probe compares m ^ k with n by a capped power: repeated multiplication
that stops as soon as the partial product exceeds n, so every product stays below n (n + 1)
and fits a word of about twice the bit length of n. The search halves the interval, so it makes
at most ⌈log₂ (n + 1)⌉ probes.
Main definitions #
NatNthRoot.CapPow.program: comparem ^ kwith a boundnby capped multiplication.NatNthRoot.program: binary search forNat.nthRoot k n.
Main results #
NatNthRoot.CapPow.exec: within6 k + 5instructions, the capped power is at mostnexactly whenm ^ k ≤ n, and then equalsm ^ k.NatNthRoot.exec:Nat.nthRoot k nwithin⌈log₂ (n + 1)⌉ (6 k + 13) + 7instructions.
Base, preserved.
Exponent, preserved.
Bound, preserved.
Capped power, the output.
Stop once the partial product exceeds the bound; otherwise multiply once more.
Capped repeated multiplication.
For a positive base, the capped power is at most the bound exactly when the true power is,
and then the two agree. The products stay below n m.
Run the capped power on any injectively chosen registers.
Root degree k, preserved.
Lower end of the search interval, and the output.
Upper end of the search interval.
Capped power of the probe.
Counter of the capped power.
lo + 1, for the loop test.
The registers of the capped power inside the search.
One probe: halve the interval according to the capped power of its midpoint.
Compute Nat.nthRoot k n for k ≥ 1 within ⌈log₂ (n + 1)⌉ (6 k + 13) + 7 instructions,
preserving n, k and memory. Products and search bounds are at most n² + 2 n + 1, but the
probe counters run up to k whatever n is, hence the separate hypothesis hkfit.
Run the binary search on any injectively chosen registers.