Mathwords logoMathwords

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 n2n \geq 2, outputs 'prime' if nn has no positive divisors other than 11 and nn, and 'composite' otherwise. Trial division tests all potential factors dd where 2dn2 \leq d \leq \lfloor\sqrt{n}\rfloor; if none divides nn, then nn is prime.

Key Formula

Check all d where 2dn\text{Check all } d \text{ where } 2 \leq d \leq \lfloor\sqrt{n}\rfloor
Where:
  • nn = The positive integer being tested for primality
  • dd = A candidate divisor
  • n\lfloor\sqrt{n}\rfloor = The largest integer not exceeding the square root of n

How It Works

To test whether nn is prime by trial division, divide nn by every integer dd from 2 up to n\lfloor\sqrt{n}\rfloor. If any dd divides nn evenly (with remainder 0), then nn is composite. If none of them divide nn, then nn is prime. You only need to check up to n\sqrt{n} because if n=a×bn = a \times b and both aa and bb are greater than n\sqrt{n}, then a×b>na \times b > n, 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.
97=9.849=9\lfloor\sqrt{97}\rfloor = \lfloor 9.849\ldots \rfloor = 9
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.
97÷2=48 R 1,97÷3=32 R 1,97÷5=19 R 2,97÷7=13 R 697 \div 2 = 48\text{ R }1,\quad 97 \div 3 = 32\text{ R }1,\quad 97 \div 5 = 19\text{ R }2,\quad 97 \div 7 = 13\text{ R }6
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 n\lfloor\sqrt{n}\rfloor. If n has a factor larger than its square root, it must also have a corresponding factor smaller than its square root.