Wilsons Theorem

Download as PDF

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.

Wilsons Theorem — an original Algebra911 reference diagram defining wilsons theorem with its key formula and a worked example.
Wilson's Theorem: A Surprising Test for Prime Numbers

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 p>1 is a prime number if and only if the factorial of p1 is one less than a multiple of p. In the language of modular arithmetic, this is written as a clean, powerful formula.

(p1)!1(modp)

Let's break that down. A prime number is a whole number greater than 1 whose only factors are 1 and itself. Examples include 2,3,5,7,11, and so on. The term 'if and only if' is crucial here. It means the rule works in both directions:

  • If p is a prime number, then (p1)!1(modp) is guaranteed to be true.
  • If (p1)!1(modp) is true for some integer p, then p 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 5, written as 5!, is:

5!=5×4×3×2×1=120

Here are a few more examples:

  • 1!=1
  • 2!=2×1=2
  • 3!=3×2×1=6
  • 4!=4×3×2×1=24

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:

10+53(mod12)

The symbol means 'is congruent to', and (mod12) tells us the modulus is 12. Two numbers are congruent modulo m if they have the same remainder when divided by m. For example, 15÷12 gives a remainder of 3, so 153(mod12).

Now, what does 1 mean in Wilson's Theorem? Let's look at the expression (p1)!1(modp). This means that when you divide (p1)! by p, the remainder is the same as when you divide 1 by p. A remainder of 1 is simply a mathematical shortcut for a remainder of p1. In other words, (p1)! is exactly one less than a multiple of p. For instance, 241(mod5) is true because 24 is one less than 25, which is a multiple of 5.

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.

Example 1

Test if p=5 is a prime number using Wilson's Theorem.

First, we identify p. Here, p=5. The theorem uses (p1)!. So we need to calculate (51)!, which is 4!.

Step 1: Calculate the factorial.
4!=4×3×2×1=24

Step 2: Check the congruence modulo p.
We need to check if 241(mod5). This is asking: is 24 one less than a multiple of 5? The multiples of 5 are 5,10,15,20,25,.... Indeed, 24 is one less than 25. So, the statement is true.
Alternatively, we can find the remainder of 24÷5. 24=4×5+4. The remainder is 4. Since 4 is the same as 51, we can say the remainder is 1 in modular arithmetic. So, 244(mod5), which is the same as 241(mod5).

Since the condition holds, Wilson's Theorem confirms that 5 is a prime number.

Example 2

Test if p=7 is a prime number.

Here, p=7. We need to calculate (71)!=6!.

Step 1: Calculate the factorial.
6!=6×5×4×3×2×1=720

Step 2: Check the congruence modulo p.
We check if 7201(mod7). Let's divide 720 by 7 to find the remainder.
720÷7=102 with a remainder of 6.
Since the remainder is 6, and 6=71, the condition is satisfied. 720 is one less than 721, and 721=7×103. So, 7206(mod7), which is equivalent to 7201(mod7).

The theorem holds, confirming that 7 is a prime number.

The theorem works for all primes, but as you can see, the factorial calculation becomes very large, very fast. Calculating 16! to test if 17 is prime would be quite a task by hand!

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 n can be factored into smaller integers. These factors will appear in the product of (n1)!, causing the result to be a multiple of n, which means the remainder will be 0.

There are two main cases for a composite number n>4:

  1. n has two distinct factors, a and b, where 1<a<b<n. For example, n=10. Its factors are 2 and 5. Both 2 and 5 are less than 9, so they will both be part of the product in (101)!=9!.
    9!=9×8×7×6×5×4×3×2×1
    Since both 2 and 5 are in the product, their product, 10, must be a factor of 9!. If 10 is a factor of 9!, then 9! is a multiple of 10, and the remainder when dividing 9! by 10 is 0. So, 9!0(mod10). This is not 1, so 10 is not prime.
  2. n is the square of a prime number, n=p2. For example, n=9=32. Consider (91)!=8!. The factors in this product include 3 and 6.
    8!=8×7×6×5×4×3×2×1
    The product contains both 3 and 6. Their product is 3×6=18. Since 18 is a multiple of 9, 8! must also be a multiple of 9. Therefore, 8!0(mod9). Again, this is not 1, so 9 is not prime. This logic works for any p2 as long as 2p<p2, which is true for all primes p>2.

The only special case is n=4. Here, (41)!=3!=6. When we check 6(mod4), we get a remainder of 2. So 3!2(mod4). Since the result is not 1, the theorem correctly tells us that 4 is not prime.

Example 3

Test if n=6 is a prime number.

Here, n=6. We need to calculate (61)!=5!.

Step 1: Calculate the factorial.
5!=5×4×3×2×1=120

Step 2: Check the congruence modulo n.
We check if 1201(mod6). Let's divide 120 by 6.
120÷6=20 with a remainder of 0.
So, 1200(mod6). This is not congruent to 1.

Because the condition fails, Wilson's Theorem correctly shows that 6 is a composite number.

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)TypeCalculation: (n-1)!Result: (n-1)! mod nIs it -1 (mod n)?
2Prime(2-1)! = 1! = 11 mod 2 ≡ -1 mod 2Yes
3Prime(3-1)! = 2! = 22 mod 3 ≡ -1 mod 3Yes
4Composite(4-1)! = 3! = 66 mod 4 ≡ 2 mod 4No
5Prime(5-1)! = 4! = 2424 mod 5 ≡ -1 mod 5Yes
6Composite(6-1)! = 5! = 120120 mod 6 ≡ 0 mod 6No
7Prime(7-1)! = 6! = 720720 mod 7 ≡ -1 mod 7Yes
8Composite(8-1)! = 7! = 50405040 mod 8 ≡ 0 mod 8No
9Composite(9-1)! = 8! = 4032040320 mod 9 ≡ 0 mod 9No
10Composite(10-1)! = 9! = 362880362880 mod 10 ≡ 0 mod 10No
11Prime(11-1)! = 10! = 36288003628800 mod 11 ≡ -1 mod 11Yes

Notice the clear pattern: for every prime number, the result is congruent to 1. For every composite number greater than 4, the result is 0.

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 (p1)!, not p!. Always subtract one from your number before you calculate the factorial. For p=5, you use 4!, not 5!.
  • Misunderstanding 1: The result doesn't have to be the number 1. The expression 1(modp) means the remainder is p1. For p=7, the remainder is 6, 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 100! 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 1,234,567,891 is prime, calculating the factorial of one less than that number is computationally impossible. Simpler methods, like trial division, are far more efficient for smaller numbers, and more advanced algorithms are used for very large numbers in fields like cryptography.

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 p>1 is a prime number if and only if (p1)!1(modp).
  • What it Means: If you take a prime number p, calculate the factorial of the number just below it, and divide by p, the remainder will always be p1.
  • For Composite Numbers: If n is a composite number greater than 4, then (n1)!0(modn). For n=4, the result is (41)!2(mod4). 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.