Mathlib theorem · Existing formal mathematics

Existing in Mathlib · Number theory

Wilson's Theorem

For each natural number n other than 1, n is prime exactly when the natural-number factorial (n−1)!, cast into ZMod n, equals −1. The n = 0 case uses Lean's natural subtraction and factorial conventions and is handled separately, rather than by informal predecessor-product notation.

Exact theorem

Exact Mathlib statement

theorem Nat.prime_iff_fac_equiv_neg_one {n : ℕ} (h : n ≠ 1) : Nat.Prime n ↔ ((n - 1)! : ZMod n) = -1

The theorem at a glance

Wilson's Theorem — an exact factorial characterization of primes

A visual guide to the theorem's hypotheses, structure, and conclusion; the exact statement gives the formal detail.

The product of all nonzero residues modulo a prime is −1, and the same factorial congruence rules out compositeness when n ≠ 1. Explanatory diagram.
Detailed visual description

The exact equivalence is supported by two distinct source movements: the prime direction identifies the factorial with the product of all units and pairs inverses, while the converse handles n = 0 separately before using a proper divisor to contradict residue −1.

Statement structure

From hypotheses to conclusion

Statement map for Wilson's TheoremFor n ≠ 1, primality is equivalent to the cast of (n−1)! being −1 in ZMod n. The pinned upstream declaration is Nat.prime_iff_fac_equiv_neg_one. The exact checked statement is theorem Nat.prime_iff_fac_equiv_neg_one {n : ℕ} (h : n ≠ 1) : Nat.Prime n ↔ ((n - 1)! : ZMod n) = -1.Mathematical readingFor n ≠ 1, primality isequivalent to the castof (n−1)! being −1 inZMod n.Pinned declarationmathlib ·Nat.prime_iff_fac_equiv_neg_oneExact checked formtheoremNat.prime_iff_fac_equiv_neg_one{n : ℕ} (h : n ≠ 1) :Nat.Prime n ↔ ((n - 1)!: ZMod n) = -1Statement map for Wilson's TheoremFor n ≠ 1, primality is equivalent to the cast of (n−1)! being −1 in ZMod n. The pinned upstream declaration is Nat.prime_iff_fac_equiv_neg_one. The exact checked statement is theorem Nat.prime_iff_fac_equiv_neg_one {n : ℕ} (h : n ≠ 1) : Nat.Prime n ↔ ((n - 1)! : ZMod n) = -1.Mathematical readingFor n ≠ 1, primality isequivalent to the castof (n−1)! being −1 inZMod n.Pinned declarationmathlib ·Nat.prime_iff_fac_equiv_neg_oneExact checked formtheoremNat.prime_iff_fac_equiv_neg_one{n : ℕ} (h : n ≠ 1) :Nat.Prime n ↔ ((n - 1)!: ZMod n) = -1

This map summarizes the statement's structure; the exact Mathlib declaration remains authoritative.

Inverse pairing gives the factorial residue for primes, with 1 = −1 at p = 2; the converse handles n = 0 first and then uses a proper divisor for composite n > 1. Explanatory diagram.
Detailed visual description

For odd primes, non-fixed units pair with their inverses and the distinct fixed residues are 1 and −1; in ZMod 2 these are one residue. Conversely, the source rejects n = 0 directly. For a remaining nonprime n > 1, a divisor m with 1 < m < n divides (n−1)!, and the asserted residue equality would force m ∣ 1.

Why it matters

A mathematical landmark

Wilson's theorem gives one of the classical exact characterizations of prime numbers and connects elementary factorial arithmetic to the multiplicative structure of finite residue rings.

ProofAtlas record

What has been checked

Upstream indexedPinned source bytes verified locally
Locally reproducedExact upstream declaration replayed
Reviewed pageCurrent public presentation reviewed
Accepted Atlas resultNot recorded for the preferred artifact

Mathlib is the source of the theorem; the local Lean replay and page review are separate.

Claim boundary

No new theorem is claimed

This target indexes Mathlib's equivalence for every natural number n ≠ 1: n is prime exactly when the cast of (n−1)! in ZMod n equals −1. The prime direction includes n = 2, where 1 and −1 are the same residue. The converse handles n = 0 separately before treating the remaining nonprime case. It does not assert a fast primality algorithm, a result for n = 1, or a new proof.

Source and local evidence

Where the theorem comes from

Existing declaration
Nat.prime_iff_fac_equiv_neg_one in mathlib
Relationship
The checked artifact is the upstream declaration itself
Local Lean evidence
artifact.library.mathlib.wilson-theorem.v001
Source
Open the pinned upstream reference