Short answer: The AKS primality test decides whether an integer n is prime in time polynomial in the number of bits of n. It generalizes the observation that n is prime exactly when (x−1)^n ≡ x^n − 1 (mod n) as a polynomial, and it checks a related congruence modulo a carefully chosen polynomial x^r − 1 and modulo n, for a polynomial number of shifts. The test is deterministic and unconditional. Its exponent is large enough that Miller-Rabin remains the test people run.
What the problem actually asks
Trial division is exponential in the bit length. Miller-Rabin is fast and probabilistic, or deterministic only inside a verified range. AKS was the first published deterministic test whose worst-case time is polynomial in log n, with no unproved hypothesis. The congruence it checks is strong enough to force primality and weak enough that only polynomially many coefficients need to be examined.
The original exponent was high. Later improvements brought the complexity down to roughly (log n)^6 times polyloglog factors in one widely cited version. Even that is far above a few Miller-Rabin rounds for any n anyone tests.
What the algorithm guarantees
The algorithm returns prime or composite correctly for every integer n > 1. The running time is polynomial in log n. The degree r of the polynomial modulus is polylogarithmic in n, chosen so that r has a large prime factor and n has a suitable order modulo r. Extra memory holds a polynomial of that degree. The test does not produce a factor when it says composite.
Perfect power detection is a required first step. A prime power p^k for k ≥ 2 must be rejected before the congruence phase, and it can be found by integer roots in logarithmic time. Skipping that step misclassifies powers.
The working steps
- If n is a perfect power a^b with b ≥ 2, return composite.
- Find the smallest r such that the order of n modulo r is large enough for the theorem, greater than (log n)² in the usual statement. This search is part of the polynomial bound.
- If n shares a proper factor with any integer up to r, return composite. If n is at most r, return prime.
- For a from 1 to the stated bound, which is on the order of sqrt(φ(r)) · log n, check that (x+a)^n ≡ x^n + a modulo (x^r − 1, n).
- If every congruence holds, return prime. A failed congruence returns composite.
Polynomial time here is a theoretical classification. It is not a claim that AKS should replace Miller-Rabin in a library. The deterministic 64-bit Miller-Rabin base sets are proofs inside their range and are vastly faster. AKS does not factor n. Its role is the existence of a deterministic polynomial-time primality proof.
Approaches that look simpler and fail
| Approach | Why it fails when the perfect-power check is omitted |
|---|---|
| Run only the polynomial congruences | A prime power can satisfy the later setup poorly or be misread by a shortcut implementation. The perfect-power test is a required case, not an optimization. |
| Replace AKS with a fixed list of Miller-Rabin bases for every size of n | A fixed list is deterministic only up to the bound for which it was proved. AKS is the test whose proof has no size cap. |
| Expect a factor as a byproduct | The congruences fail or hold. A factorisation is a different algorithm. |
| Cite AKS as the practical test inside a crypto library | The degree and the number of coefficient checks are far beyond a handful of modular exponentiations. Libraries use Miller-Rabin or a deterministic variant. |
Practical primality is Miller-Rabin. Enumeration of small primes is a sieve. AKS is the deterministic polynomial-time primality theorem turned into an algorithm.
A working example
This Python fragment rejects perfect powers, finds a modulus whose multiplicative order is larger than (log n)², and checks the polynomial congruence (x+a)^n ≡ x^n + a. It prints composite prime for 9 and 31.
import math
def perfect_power(n):
for exp in range(2, int(math.log2(n)) + 1):
base = round(n ** (1 / exp))
if base >= 2 and base ** exp == n:
return True
return False
def multiplicative_order(n, modulus):
if math.gcd(n, modulus) != 1:
return None
value = n % modulus
k = 1
while value != 1:
value = value * n % modulus
k += 1
if k > modulus:
return None
return k
def poly_pow(base, exp, degree, mod):
result = [0] * degree
result[0] = 1
while exp:
if exp & 1:
nxt = [0] * degree
for i, ai in enumerate(result):
if not ai:
continue
for j, bj in enumerate(base):
nxt[(i + j) % degree] = (nxt[(i + j) % degree] + ai * bj) % mod
result = nxt
nxt = [0] * degree
for i, ai in enumerate(base):
if not ai:
continue
for j, bj in enumerate(base):
nxt[(i + j) % degree] = (nxt[(i + j) % degree] + ai * bj) % mod
base = nxt
exp >>= 1
return result
def aks(n):
if n < 2 or perfect_power(n):
return False
need = math.log2(n) ** 2
r = 2
while True:
order = multiplicative_order(n, r)
if order is not None and order > need:
break
r += 1
for a in range(2, r + 1):
g = math.gcd(n, a)
if 1 < g < n:
return False
if n <= r:
return True
phi = sum(1 for i in range(r) if math.gcd(i, r) == 1)
bound = int((phi ** 0.5) * math.log2(n)) + 1
for a in range(1, bound + 1):
base = [0] * r
base[0] = a % n
base[1 % r] = (base[1 % r] + 1) % n
got = poly_pow(base, n, r, n)
expect = [0] * r
expect[0] = a % n
expect[n % r] = (expect[n % r] + 1) % n
if got != expect:
return False
return True
def sieve(limit):
prime = [False, False] + [True] * (limit - 1)
for i in range(2, int(limit ** 0.5) + 1):
if prime[i]:
for j in range(i * i, limit + 1, i):
prime[j] = False
return prime
prime = sieve(40)
assert all(aks(n) == prime[n] for n in range(2, 41))
print("composite" if not aks(9) else "prime", "prime" if aks(31) else "composite")
Where it is used
The AKS test is the proof that primality is in polynomial time. Key generation does not run it. Miller-Rabin is the probable-prime test inside OpenSSL and the other libraries that draw RSA primes.
When you can drop this algorithm
Drop AKS for any integer a normal program needs to test. Use Miller-Rabin, with a deterministic base set when the integer fits a proved range. Keep AKS as the algorithm that shows deterministic primality is in polynomial time.
Why this stays published
We publish this so a search for the AKS test returns the polynomial congruence, the perfect-power check, and the reason it is not the practical primality test. The AKS test decides primality in time polynomial in the number of bits, by perfect-power rejection and a polynomial number of congruences modulo x^r − 1.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
