◄ WORLD V · SONNY 5DART 183 · a helldive at the net

WILSON’S THEOREM (p-1)! is -1 mod p exactly when p is prime

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.

THE TECHNIQUE pair each residue with its inverse; only -1 survives

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


HISTORY & CREDIT stated Wilson 1770; proved Lagrange 1771

“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

~1000 · Ibn al-Haytham (Alhazen) — states the congruence in his work on number theory, seven centuries before Wilson.
1770 · Edward Waring — publishes it in Meditationes Algebraicae, crediting his student John Wilson (who gave no proof).
1771 · Joseph-Louis Lagrange — the first proof, and the converse (composite ⇒ not −1), making it an iff.

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

RECOMMEND FOR I-13 the factorial collapses, computed

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:

$ i13 run wilson.i13 # (n-1)! mod n, reduced every step 6! mod 7 = 6 -- 6 = 7-1 = -1 mod 7 -> 7 is PRIME 5! mod 6 = 0 -- a repeated factor divides it -> 6 is COMPOSITE
Recommend: Wilson’s theorem is LIT and the exact-iff counterpart to the Fermat test (182) on I-13 — verified 6! ≡ 6 ≡ -1 (mod 7) (prime) and 5! ≡ 0 (mod 6) (composite), computing the factorial modulo n at every step so it stays a small integer (native recursion, one %). It is a demonstration, not a practical test — (n-1)! is O(n) multiplications where trial division is O(√n) — but it is the one primality criterion that is a clean logical equivalence, and its pairing-off proof is the corpus’s self-inverse motif in modular arithmetic.