Last updated
Primality Testing Algorithm Comparison
- Trial division: Test all primes up to √n — simple, O(√n), good for small numbers
- Miller-Rabin: Probabilistic, O(k log²n) — fast, used for large numbers, error probability < 4^(-k)
- AKS primality test: Deterministic, polynomial time — theoretically important but slower in practice
- Lucas-Lehmer test: Specialized for Mersenne numbers — very efficient for 2^p - 1 form
- For cryptographic use: Combine Miller-Rabin with multiple witnesses for near-certain results
Examples
Example 1: Checking a Prime Number
Input: 97
Result: PRIME ✓
Details:
Position: 97 is the 25th prime number
Previous prime: 89
Next prime: 101
Gap before: 97 - 89 = 8
Gap after: 101 - 97 = 4
Special properties:
✓ Twin prime? No (101 - 97 = 4, not 2)
✓ Sophie Germain prime? Yes (2×97+1 = 195 = 3×5×13 — No)
✓ Safe prime? No
✓ Emirp? Yes (79 is also prime)
Example 2: Checking a Composite Number
Input: 91
Result: COMPOSITE ✗
Prime factorization: 91 = 7 × 13
Details:
Smallest prime factor: 7
All factors: 1, 7, 13, 91
Number of divisors: 4
Note: 91 is a common "trick" number — it looks prime
but is actually 7 × 13. Many people incorrectly
assume it's prime.
Example 3: Twin Primes
Input: 11
Result: PRIME ✓
Twin prime pair: (11, 13) — differ by 2
Other twin prime pairs:
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
(101, 103)
(107, 109)
Twin Prime Conjecture: There are infinitely many twin
prime pairs — unproven but widely believed to be true.