# Wilson's Theorem

> A number above one is prime exactly when one less than it, factorialised, is one short of a multiple of it.

**Field:** Mathematics & proof · **Reliability:** Empirical · **Named after:** John Wilson

*Also known as: Wilson's prime criterion; Al-Haytham's theorem*

## What it means

Wilson's theorem is a clean characterisation of the prime numbers using factorials. It states that a whole number greater than one is prime if and only if the factorial of one less than it, the product of all positive integers below it, is congruent to minus one modulo that number; equivalently, that factorial plus one is divisible by the number. The condition is both necessary and sufficient, so it perfectly separates primes from composites, unlike weaker tests that admit exceptions. For example, four factorial is twenty-four, and twenty-four plus one is twenty-five, a multiple of five, confirming five is prime. Elegant and exact, the theorem ties together factorials, divisibility, and primality in a single congruence.

## How it works

The reason lies in how numbers pair up under multiplication modulo a prime. Among the numbers from one to one less than a prime, each has a unique partner whose product with it leaves remainder one, and only one and the number one less than the prime are their own partners. Pairing off all the others cancels their contributions to the factorial, leaving just the product of the two self-paired numbers, which works out to minus one modulo the prime. For a composite number greater than four, by contrast, its factors reappear inside the factorial and make it divisible by the number, giving remainder zero rather than minus one, so the congruence fails, which is exactly why the criterion detects composites.

## Examples

- **Checking five:** Four factorial equals twenty-four, and twenty-four is congruent to minus one modulo five since twenty-four plus one is twenty-five, so five passes and is prime.
- **The composite signature:** For any composite number above four, the factorial of one less than it is congruent to zero, not minus one, because its factors already appear in the product.
- **An ancient discovery:** The result was known to Ibn al-Haytham around the year one thousand, long before John Wilson, and the first published proof was given by Lagrange in the eighteenth century.

## Try it

This entry is interactive: a live calculator.
Open it at https://conyso.com/lawtome/laws/wilsons-theorem/ — free, runs in the browser, sends nothing anywhere.

```
(n-1)! mod n = n-1 if and only if n is prime
```

- `n` — The number under test

## Why it matters

Wilson's theorem is prized as a rare exact characterisation of primality, a single congruence that is true for every prime and false for every composite, with no exceptions of the kind that plague probabilistic tests like the Fermat test and its Carmichael numbers. It is a jewel of elementary number theory, linking factorials to primes and illustrating the group structure of arithmetic modulo a prime. Though beautiful, it is impractical as an actual primality test, because computing the factorial of one less than a large number is far more work than modern tests require, so its value is theoretical and pedagogical rather than computational.

## Where it breaks down

As a primality test the theorem is impractical, because evaluating the factorial of one less than a number requires more computation than the number of steps efficient modern tests need, so it is prized for elegance rather than use. It applies to whole numbers greater than one, and the small composite four is a special case giving remainder two rather than the zero that all larger composites give. The theorem characterises primality perfectly but offers no shortcut to factoring a composite it flags, so its exactness buys certainty about whether a number is prime without any help in decomposing it.

## Common misreadings

The theorem is an exact if-and-only-if characterisation, not a one-directional or probabilistic test, so unlike the Fermat test it has no false positives or Carmichael-number exceptions. Its perfection does not make it practical: the factorial involved is astronomically expensive, so it is not used to test large numbers despite being always correct. And the composite case gives remainder zero only for composites greater than four; the number four itself is the lone small exception, giving remainder two, which is a quirk to remember rather than a flaw.

## Origin

Known to Ibn al-Haytham around the year one thousand, stated by John Wilson and reported by Edward Waring in his Meditationes Algebraicae of 1770, and first proved by Joseph-Louis Lagrange in 1771.

## Related

- [Fermat's Little Theorem](https://conyso.com/lawtome/laws/fermats-little-theorem/) — kindred
- [The Chinese Remainder Theorem](https://conyso.com/lawtome/laws/the-chinese-remainder-theorem/) — kindred

## Sources

- [Wilson's theorem, Wikipedia. — the if-and-only-if factorial congruence, the n equals five example, the Ibn al-Haytham, Wilson-Waring, and Lagrange history, and the impracticality as a test.](<https://en.wikipedia.org/wiki/Wilson%27s_theorem>)
- [Wilson's Theorem, Wolfram MathWorld. — the prime characterisation and its contrast with Fermat's little theorem.](<https://mathworld.wolfram.com/WilsonsTheorem.html>)

---
Source: https://conyso.com/lawtome/laws/wilsons-theorem/ · The Law Tome — a sourced index of named laws.
