This is an implementation of the AKS Primality test that serves as a companion demo for presenting AKS Primality testing in Math 058: Number Theory with Professor Whitehead.
- Step 1: If
$n = a^b$ for$a \in \mathbb{N}$ and$b > 1$ , then output composite - Step 2: Find the smallest
$r \in \mathbb{N}$ such that$ord_{r}(n) > \log^2(n)$ - Step 3: If
$1 < gcd(a, n) < n$ for some$a \leq r$ , then output composite - Step 4: If
$n \leq r$ , then output prime - Step 5: For
$a = 1$ to$\lfloor \sqrt{\phi(r)}\log(n) \rfloor$ doIf
$(X + a)^n \neq X^n + a \text{ (mod } X^r - 1, n)$ , then output composite - Step 6: Output prime
powerCheck(
It is important to note that the purpose of AKS is to detect large primes where other forms of primality testing may be computationally infeasible. This means that the storage size of
powerMod(
We loop through all powers of two up to the number and each operation using
defineR(
We run a loop of size
polyMultiMod(
Looping through all combinations of
polySolve(
As seen before, looping through all powers of two requires
eulerPhi(
We loop through all values up to
aks(
Bringing everything together we can determine the run time of the aks function. Determining perfect powers is