Primality Test — Definition, Formula & Examples
A primality test is a method or algorithm used to determine whether a given positive integer is a prime number. The simplest approach, called trial division, checks whether any integer from 2 up to the square root of the number divides it evenly.
A primality test is a decision procedure that, given an integer , outputs 'prime' if has no positive divisors other than and , and 'composite' otherwise. Trial division tests all potential factors where ; if none divides , then is prime.
Key Formula
Where:
- = The positive integer being tested for primality
- = A candidate divisor
- = The largest integer not exceeding the square root of n
How It Works
To test whether is prime by trial division, divide by every integer from 2 up to . If any divides evenly (with remainder 0), then is composite. If none of them divide , then is prime. You only need to check up to because if and both and are greater than , then , which is a contradiction. For efficiency, you can skip even candidates after checking 2.
Worked Example
Problem: Determine whether 97 is prime using trial division.
Step 1: Compute the square root of 97 and round down to find the upper bound for trial divisors.
Step 2: Test each integer from 2 to 9 as a divisor of 97. We only need to check primes: 2, 3, 5, and 7.
Step 3: None of these divisions produced a remainder of 0, so no integer from 2 to 9 divides 97 evenly.
Answer: 97 is prime.
Why It Matters
Primality tests are foundational in cryptography — RSA encryption relies on the difficulty of factoring large numbers while making it easy to verify that each factor is prime. In math competitions and number theory courses, efficient primality testing is a core skill for solving problems about divisibility and prime decomposition.
Common Mistakes
Mistake: Checking divisors all the way up to n − 1 instead of stopping at the square root of n.
Correction: You only need to test divisors up to . If n has a factor larger than its square root, it must also have a corresponding factor smaller than its square root.
