For an odd prime number , put . Both and are divisible by . By Wilson's theorem, they are also congruent modulo , because each is modulo . The coprime integers therefore both divide their difference, giving the displayed integer congruence. This combines the prime-modulus factorial identity with an elementary divisibility constraint.
Wilson's theorem 2026-10-14
For an integer , Wilson's theorem characterizes prime numbers by a factorial:
The right side is an integer congruence, so the least nonnegative remainder is . For example, certifies that five is a prime number, whereas rules out six. [ref]
Figure 1.
Factorial residues distinguish primes from composites, with inverse pairs explaining the prime case
. Details.
For an odd prime number , each nonzero congruence class has a unique modular multiplicative inverse. Pair each class with its inverse: each distinct pair contributes one. A class is its own inverse exactly when ; since , Euclid's lemma leaves only . Their product is . The prime number two needs a separate check: . Conversely, a proper divisor divides both and , so the displayed integer congruence would force , a contradiction. [ref]
The composite numbers have an even sharper pattern: for every composite number , but . To check the zero claim, write with . If , both occur as different factors in the factorial. If , the different factors and both occur since , and their product is divisible by . The only excluded case is . Thus four is an exception to the zero pattern, but still fails Wilson's theorem: its required remainder would be three.
This is an exact primality testing criterion, but direct evaluation is expensive: even reducing modulo after every multiplication still takes a number of multiplications proportional to , which grows exponentially with the number of binary digits of . The problem is not merely storing a huge factorial. The more useful role of Wilson's theorem is simplifying factorial expressions in modular arithmetic. For instance, if is a prime number, then gives by a modular multiplicative inverse.