Wilson Theorem Calculator

Check Wilson's theorem for a prime by reducing the factorial of p minus 1 modulo p.

Math and PercentUpdated 2026-11-192 min read
Wilson Theorem Calculator
A prime number from 2 to 100,000.
Factorial residue
...
Congruence...
Factorial index...
Prime used...

What it does

Wilson's theorem states that for a prime p, the product of all integers from 1 to p minus 1 leaves remainder p minus 1 on division by p, which is the same as minus one modulo p. The converse is also true, so the congruence characterizes primes.

This tool verifies the modulus is prime by trial division, then multiplies the integers from 2 up to p minus 1 while reducing modulo p at each step. It reports the final residue and the congruence in words.

The page opens with p equal to 7. The factorial of 6 is 720, and 720 modulo 7 is 6, which is minus one modulo 7.

The proof pairs every number from 2 to p minus 2 with its multiplicative inverse. Each pair multiplies to 1, leaving only 1 and p minus 1, so the product is minus one.

Although the theorem is a clean theoretical primality test, computing a huge factorial is far slower than modern tests, so it is mainly of theoretical interest.

How to use

  1. Enter a prime number p.
  2. Read the factorial residue as the main result.
  3. Check the congruence row for the minus one form.
  4. Verify a small case such as p = 5 by hand.
  5. Try a composite number to see the prime check reject it.
  6. Compare with Fermat's little theorem.

Formula

If p is prime, then (p - 1)! is congruent to -1 modulo p
p
A prime number.
(p-1)!
The product of all integers from 1 to p minus 1.
congruence
Equality of remainders modulo p.

Keeping the running product reduced modulo p prevents the factorial from overflowing. The factorial of p minus 1 equals p minus 1 modulo p exactly when p is prime, so the theorem also gives a primality criterion.

Worked example

Example: p = 7

The page opens with p = 7.

  1. The factorial index is p minus 1, which is 6.
  2. Six factorial is 720.
  3. Divide 720 by 7: 7 times 102 is 714, leaving remainder 6.
  4. Six is the same as minus one modulo 7.
  5. The tool reports result 6 and congruence -1 modulo 7.
  6. The theorem is confirmed for p = 7.

Reference table

Wilson residues for sample primes
Prime pFactorial indexResidueCongruence
766-1 modulo 7
131212-1 modulo 13

Limits

  • Only prime moduli are accepted.
  • The modulus is limited to 100,000.
  • Primality is checked by trial division.
  • The tool does not compute the full factorial, only its residue.

Common mistakes

  • Using a composite modulus.
  • Confusing p factorial with (p minus 1) factorial.
  • Reading the residue p minus 1 as a failure of the theorem.
  • Assuming this is an efficient primality test.
  • Forgetting that minus one modulo p is p minus 1.

Try it yourself

Quick checks. Answers sit tucked under each question.

Wilson residue for p = 13?

12, which is minus one modulo 13.

What is minus one modulo 7?

6.

Does the converse hold?

Yes. The congruence characterizes primes.

Questions

What does Wilson's theorem say?

That (p minus 1) factorial is congruent to minus one modulo p for prime p.

Can it test primality?

Yes in principle, but computing the factorial is too slow for large numbers.

Why does the product reduce to minus one?

All numbers pair with their inverses to give one, leaving only one and p minus one.

How is the factorial computed?

By repeated multiplication with reduction modulo p after each step.

How is this related to Fermat's little theorem?

Both are classical results about arithmetic modulo a prime.

What is the residue for p = 2?

One factorial is 1, which is minus one modulo 2.

Sources

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.