Wilsons Theorem
Have you ever wondered if there's a foolproof formula to identify prime numbers? Wilson's Theorem offers a fascinating and elegant answer. This guide breaks down the theorem, explains the math behind it, and shows you exactly how to apply this unique test for primality.

What Is Wilson's Theorem?
Wilson's Theorem is a statement in number theory that gives a necessary and sufficient condition for a number to be prime. It states that an integer
Let's break that down. A prime number is a whole number greater than
- If
is a prime number, then is guaranteed to be true. - If
is true for some integer , then is guaranteed to be a prime number.
This two-way relationship makes Wilson's Theorem a complete test for primality. To understand how it works, we first need to get comfortable with its two main ingredients: factorials and modular arithmetic.
What Are Factorials and Modular Arithmetic?
Before we can test numbers with Wilson's Theorem, let's master the two key concepts it relies on. These might sound complicated, but they are built on simple ideas.
Factorials (n!)
A factorial, denoted by an exclamation mark (!), is the product of all positive integers up to that number. For example, the factorial of
Here are a few more examples:
As you can see, factorials grow very quickly! This is an important detail that we'll come back to later.
Modular Arithmetic (Clock Math)
Modular arithmetic is a system of arithmetic for integers, where numbers 'wrap around' upon reaching a certain value—the modulus. Think of a 12-hour clock. If it's 10 o'clock now, what time will it be in 5 hours? It won't be 15 o'clock. It will be 3 o'clock. In modular arithmetic, we would write this as:
The symbol
Now, what does
How Does Wilson's Theorem Work with Prime Numbers?
Now that we have the tools, let's see the theorem in action. We'll test a few known prime numbers to confirm that the formula holds true.
Test if
First, we identify
Step 1: Calculate the factorial.
Step 2: Check the congruence modulo
We need to check if
Alternatively, we can find the remainder of
Since the condition holds, Wilson's Theorem confirms that
Test if
Here,
Step 1: Calculate the factorial.
Step 2: Check the congruence modulo
We check if
Since the remainder is
The theorem holds, confirming that
The theorem works for all primes, but as you can see, the factorial calculation becomes very large, very fast. Calculating
Why Does the Theorem Fail for Composite Numbers?
A powerful feature of Wilson's Theorem is that it not only works for primes but also reliably fails for composite numbers (numbers greater than 1 that are not prime). Let's explore why.
A composite number
There are two main cases for a composite number
has two distinct factors, and , where . For example, . Its factors are and . Both and are less than , so they will both be part of the product in .
Since both and are in the product, their product, , must be a factor of . If is a factor of , then is a multiple of , and the remainder when dividing by is . So, . This is not , so is not prime. is the square of a prime number, . For example, . Consider . The factors in this product include and .
The product contains both and . Their product is . Since is a multiple of , must also be a multiple of . Therefore, . Again, this is not , so is not prime. This logic works for any as long as , which is true for all primes .
The only special case is
Test if
Here,
Step 1: Calculate the factorial.
Step 2: Check the congruence modulo
We check if
So,
Because the condition fails, Wilson's Theorem correctly shows that
Wilson's Theorem: A Quick Reference Table
Seeing the results side-by-side can make the pattern crystal clear. This table shows how Wilson's Theorem behaves for the first few integers.
| Number (n) | Type | Calculation: (n-1)! | Result: (n-1)! mod n | Is it -1 (mod n)? |
|---|---|---|---|---|
| 2 | Prime | (2-1)! = 1! = 1 | 1 mod 2 ≡ -1 mod 2 | Yes |
| 3 | Prime | (3-1)! = 2! = 2 | 2 mod 3 ≡ -1 mod 3 | Yes |
| 4 | Composite | (4-1)! = 3! = 6 | 6 mod 4 ≡ 2 mod 4 | No |
| 5 | Prime | (5-1)! = 4! = 24 | 24 mod 5 ≡ -1 mod 5 | Yes |
| 6 | Composite | (6-1)! = 5! = 120 | 120 mod 6 ≡ 0 mod 6 | No |
| 7 | Prime | (7-1)! = 6! = 720 | 720 mod 7 ≡ -1 mod 7 | Yes |
| 8 | Composite | (8-1)! = 7! = 5040 | 5040 mod 8 ≡ 0 mod 8 | No |
| 9 | Composite | (9-1)! = 8! = 40320 | 40320 mod 9 ≡ 0 mod 9 | No |
| 10 | Composite | (10-1)! = 9! = 362880 | 362880 mod 10 ≡ 0 mod 10 | No |
| 11 | Prime | (11-1)! = 10! = 3628800 | 3628800 mod 11 ≡ -1 mod 11 | Yes |
Notice the clear pattern: for every prime number, the result is congruent to
What Are Some Common Mistakes?
When first learning Wilson's Theorem, it's easy to make a few common errors. Be on the lookout for these pitfalls:
- Calculating the Wrong Factorial: The formula is for
, not . Always subtract one from your number before you calculate the factorial. For , you use , not . - Misunderstanding
: The result doesn't have to be the number . The expression means the remainder is . For , the remainder is , which satisfies the condition. - Arithmetic Errors: Factorials get very large, very fast. A small mistake in multiplication can lead to the wrong conclusion. Always double-check your calculations.
- Assuming It's a Practical Test for Large Numbers: While the theorem is always true, it is incredibly inefficient for testing large numbers. Calculating
would take a very long time. The theorem is more of a theoretical tool than a practical one for programmers or cryptographers. - Forgetting the 'If and Only If' Condition: Wilson's Theorem is powerful because it works both ways. Don't just remember that primes satisfy the rule; remember that if the rule is satisfied, the number must be prime.
Is Wilson's Theorem Actually Useful?
After seeing how quickly the factorials grow, you might be wondering if Wilson's Theorem has any practical use. If you want to check if a massive number like
So, why do we learn it? The value of Wilson's Theorem is not in its computational speed but in its theoretical elegance. It provides a characterization of prime numbers—a simple, non-obvious property that is true for all prime numbers and for no other numbers. It's a perfect, self-contained definition in a single equation.
Mathematicians use theorems like this as building blocks to prove other, more complex results in number theory. It connects ideas about primes, factorials, and modular arithmetic in a deep and surprising way. It's a beautiful piece of mathematical structure, and understanding it gives you a deeper appreciation for the patterns that govern the world of numbers.
Wilson's Theorem in a Nutshell
Here is a quick summary of the most important points to remember about Wilson's Theorem.
- The Core Rule: An integer
is a prime number if and only if . - What it Means: If you take a prime number
, calculate the factorial of the number just below it, and divide by , the remainder will always be . - For Composite Numbers: If
is a composite number greater than , then . For , the result is . In all cases, the condition for primality fails. - Theoretical vs. Practical: It is a theoretically beautiful and important theorem for proving other concepts in mathematics, but it is not a practical or efficient method for testing if large numbers are prime.
Frequently Asked Questions
Who was Wilson and did he discover the theorem?
John Wilson was an 18th-century British mathematician. He was a student of Edward Waring, who published the theorem in 1770 and attributed it to Wilson. However, the famous mathematician Lagrange gave the first proof, and it's believed that Leibniz may have known about it a century earlier.
What does `(p-1)! ≡ -1 (mod p)` actually mean in simple terms?
It means that if you calculate the product of all whole numbers up to one less than a prime number `p`, the result will be exactly one less than a multiple of `p`. For example, for the prime `p=5`, `4!` is `24`, which is one less than `25` (a multiple of 5).
Can I use Wilson's Theorem to find the next prime number?
Theoretically, yes, but it's extremely impractical. To find the next prime after 29, you would have to test 30, 31, 32, and so on. Calculating 30! is a massive task, making this method far too slow compared to simpler tests like trial division.
Does Wilson's theorem work for the first prime, p=2?
Yes, it does. For p=2, the formula is (2-1)! ≡ -1 (mod 2). This simplifies to 1! ≡ -1 (mod 2), which is 1 ≡ -1 (mod 2). This is true because 1 is one less than 2, which is a multiple of 2.
Why does the result for composite numbers usually end up as 0 (mod n)?
For a composite number `n`, its factors are smaller than `n` and will appear in the product `(n-1)!`. This means `n` itself is a factor of `(n-1)!`, so when you divide `(n-1)!` by `n`, the remainder is 0. The only exception is n=4.
Is there a simpler way to check if a number is prime?
For small numbers, the easiest method is trial division. You just check for divisibility by prime numbers up to the square root of the number. For example, to test if 101 is prime, you only need to check for divisibility by primes up to sqrt(101), which is about 10 (so, check 2, 3, 5, 7).
Why is modular arithmetic so important in number theory?
Modular arithmetic helps mathematicians focus on remainders, which reveals deep properties and patterns in integers. It's the foundation of concepts used in cryptography, computer science, and solving complex equations involving whole numbers.