AKS Primality Testing
AKS Primality Testing Algorithm
Short Description
The AKS (Agrawal–Kayal–Saxena) algorithm is a deterministic polynomial-time algorithm used to test whether a given number is prime. Unlike probabilistic methods such as the Fermat or Miller–Rabin tests, AKS always produces a correct result without relying on randomness. Introduced in 2002 by Agrawal, Kayal, and Saxena, this algorithm resolved a long-standing open problem in computer science by proving that primality testing belongs to the complexity class P.
2. Mathematical Foundation
2.1 Fermat’s Little Theorem (Classical)
If is prime and is not divisible by :
This is the basis of many probabilistic tests.
❗ Problem: Some composite numbers (Carmichael numbers) also satisfy this.
3. Key Insight Behind AKS
Instead of working with numbers, AKS works with polynomials.
AKS Theorem
For an integer ,
for suitably chosen values of and .
This polynomial identity always holds for primes, and fails for composites.
4. Why Does This Work?
4.1 If is prime
Using the binomial theorem:
When is prime:
-
All binomial coefficients for are divisible by
-
So modulo :
✅ Identity holds.
4.2 If is composite
At least one binomial coefficient is not divisible by .
So the identity breaks, revealing compositeness.
5. The AKS Algorithm – Step by Step
Step 1: Check if is a perfect power
The algorithm begins by checking whether the given number can be expressed as:
If such a representation exists, then is composite.
This step quickly eliminates numbers like 16, 27, or 64.
📌 Example:
-
-
→ composite
Step 2: Find a suitable
The next step is to find the smallest integer such that the multiplicative order of modulo is greater than
Where:
-
such that
This step limits polynomial degree and ensures efficiency.
This step limits polynomial degree and ensures efficiency.This condition ensures that polynomial computations later in the algorithm remain efficient and bounded. The choice of r is crucial for guaranteeing polynomial-time complexity.
Step 3: Check for small divisors
For every integer from 2 to , the algorithm checks:
If the GCD is greater than 1 but less than , then has a non-trivial divisor and is therefore composite.
For all
-
If , then composite
📌 Example:
-
,
Step 4: If , declare PRIME
Small numbers can be directly handled.
If the number is less than or equal to , and it has passed all previous checks, it is declared prime.
This step avoids unnecessary polynomial computations for small values of .
Step 5: Polynomial Congruence Test (Core Step)
This is the heart of the AKS algorithm. For values of from 1 up to:
This step is a generalization of Fermat’s Little Theorem to polynomial rings and guarantees deterministic correctness.
For:
Check:
If this congruence fails for any value of , the number is composite.
If it holds for all tested values, the number is prime.
6. Worked Examples
Example 1: (Prime)
Check:
Modulo :
✅ Identity holds → Prime
Example 2: (Composite)
Modulo :
❌ Identity fails → Composite
Example 3: Carmichael Number
-
Fermat test fails to detect composite
-
AKS polynomial test detects composite
✔️ This is a major strength of AKS.
7. Time Complexity
-
Original AKS:
-
Improved versions:
📌 Polynomial in number of digits of
8. Comparison with Other Primality Tests
| Test | Deterministic | Time | Error |
|---|---|---|---|
| Trial Division | Yes | Exponential | None |
| Fermat | No | Fast | High |
| Miller–Rabin | No | Very fast | Negligible |
| AKS | Yes | Polynomial | None |
Why AKS Matters
-
It is the first deterministic polynomial-time primality test
-
It proves that PRIMES ∈ P
-
It combines number theory and algebra in an elegant way
-
Although not used in practice due to speed, it is a landmark theoretical result
Conclusion
The AKS algorithm stands as a milestone in theoretical computer science. While faster probabilistic tests are preferred in practical applications, AKS provides a mathematically rigorous and deterministic method for primality testing, demonstrating that prime numbers can be identified efficiently and conclusively.
AKS Primality Testing Algorithm – Pseudocode
Algorithm: AKS(n)
Input: Integer
Output: PRIME or COMPOSITE
Explanation of Steps (Short – Exam Friendly)
-
Perfect power check eliminates obvious composites.
-
Finding r ensures polynomial-time execution.
-
GCD check detects small factors.
-
Polynomial congruence test verifies primality deterministically.
-
If all checks pass, n is prime.
Comments
Post a Comment