Number Theory
Advanced

Wilson Theorem

A primality criterion for p.

Formula

(p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod{p}

Variables

pPrime

Example

Holds iff p is prime

How it works

Wilson’s Theorem says that when p is prime, the product of all nonzero residue classes modulo p, namely 1·2·3·…·(p−1), leaves a remainder of p−1, which is the same as −1 modulo p. The reason is that every number from 1 to p−1 has a unique multiplicative inverse modulo p, and these inverses pair up to give factors of 1, except for the self-inverse elements 1 and p−1; their product is −1. This makes the theorem both a deep structural fact about the arithmetic of primes and a useful one-line test for primality.

When to use it

Use this when you need a theoretical primality criterion or when proving properties of modular inverses and residue classes. It is especially useful in advanced number theory proofs, though it is rarely practical for large-number primality testing because computing (p−1)! directly is expensive.

Worked example, step by step

Check p = 5. 1! = 1 2! = 1×2 = 2 ≡ 2 mod 5 3! = 1×2×3 = 6 ≡ 1 mod 5 4! = 1×2×3×4 = 24 ≡ 4 mod 5 So (5−1)! = 24 ≡ 4 mod 5. Since 4 ≡ −1 mod 5, we have (5−1)! ≡ −1 mod 5. Therefore Wilson’s Theorem holds for p = 5.

Common mistakes to avoid

  • Forgetting that the theorem is stated modulo p, so the result p−1 is the same as −1 only after reducing mod p
  • Trying to use the theorem as a fast computational primality test for large p, even though factorial growth makes it inefficient
  • Assuming the theorem works for composite p; it is an iff statement, so failure of the congruence means p is not prime

Did You Know?

Wilson's theorem gives a perfect but hopelessly slow test for whether a number is prime.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026