Perfect-power detection on the word RAM #
Step 1 of the AKS algorithm Agrawal, Kayal, Saxena, Section 4 asks whether n = a ^ b
for some b > 1. For n ≥ 2 such an a is at least 2, so b ≤ log₂ n, and n is a b-th
power exactly when Nat.nthRoot b n ^ b = n (Cslib.AKS.isPerfectPower_iff_nthRoot). The program
computes ⌊log₂ n⌋, and for each b from 2 to ⌊log₂ n⌋ takes the b-th root by binary search
(Cslib.Algorithms.WordRAM.NatNthRoot.program) and compares its capped b-th power with n.
Every intermediate value is at most n² + 2n + 1.
Main definitions #
Cslib.Algorithms.WordRAM.AKS.PerfectPower.program: the perfect-power test.Cslib.Algorithms.WordRAM.AKS.PerfectPower.time: its instruction bound,O(log³ n).
Main results #
Cslib.Algorithms.WordRAM.AKS.PerfectPower.exec: the program decidesCslib.AKS.IsPerfectPowerforn ≥ 2withintime ninstructions.
References #
The candidate n, preserved.
The current exponent b.
The root found by the search.
Upper end of the search interval.
Probe of the search.
Counter of the capped power.
Loop test of the search.
⌊log₂ n⌋ + 1, the end of the exponent loop.
The answer: 1 for a perfect power.
Argument of the logarithm, consumed.
The root search runs on the first ten registers.
Test one exponent: take the root, raise it back, and record equality with n.
Compute ⌊log₂ n⌋, then test every exponent 2 ≤ b ≤ ⌊log₂ n⌋.
The instruction bound of program, which is O(log³ n).
The perfect-power test. For n ≥ 2 and a word size holding n² + 2n + 2, the program
leaves 1 in flag if n is a perfect power and 0 otherwise, within time n instructions,
preserving n and memory.