Work out wilson theorem instantly with clear inputs, formula shown and shareable results.
Wilson's theorem gives an exact primality criterion: p is prime if and only if (p − 1)! ≡ −1 (mod p). It is beautiful but useless for large p, since computing the factorial is far slower than trial division.
Wilson's theorem
(p − 1)! ≡ −1 (mod p) ⇔ p is prime
12! mod 13 = 12, which is −1 mod 13, confirming 13 is prime.
For composite p above 4, the factors of p all appear among 1 to p − 1, so the factorial is divisible by p and the residue is 0.