An exact, two-sided characterisation of primality: (n-1)! ≡ -1 (mod n) holds if and only if n is prime. Every non-identity residue pairs with its distinct inverse and cancels, leaving only 1 and -1 unpaired — so the factorial collapses to -1. Beautiful and definitive, but useless as a test: computing a factorial is far slower than just trial-dividing.
Compute (n-1)! mod n. For a prime it is n-1 (i.e. -1); for a composite it is 0 (a repeated factor divides it). The primality test that is an iff, not a probability: live demo
“Wilson proved Wilson’s theorem.” — no. John Wilson only stated it (published by Waring, 1770); the first proof is Lagrange’s (1771). And Ibn al-Haytham (Alhazen) knew it around 1000 CE — a Stigler’s-law name attached to neither the discoverer nor the prover. cited
The proof is the pairing: in Z/p every element except 1 and p-1 has an inverse different from itself, so the product of all of them is 1·(p-1) = -1 — a self-inverse argument, the same shape as a palindrome pairing off. Waring/Wilson 1770 / Lagrange 1771
On the canonical compiler (n-1)! mod n is n-1 for a prime and 0 for a composite — running the factorial modulo n so it never overflows: