Wilson's theorem

Wilson's theorem states that any number p is prime if, and only if, (p - 1)! + 1 is divisible by p. We can easily check this for some small numbers: (2 - 1)! + 1 = 2, which is divisible by 2; (5 - 1)! + 1 = 25, which is divisible by 5; (9 - 1)! + 1 = 40321, which is not divisible by 9. The theorem is named for Sir John Wilson (1741–1793), who came across it (but left no formal proof) while he was a student at Peterhouse College, Cambridge. Wilson went on to become a judge and seems to have done little else in mathematics. The theorem was first published and named after Wilson by Edward Waring around 1770. However, it is now clear that the result was known to Gottfried Leibniz and perhaps, much earlier to Ibn al-Haytham (965–1040). The first known proof was provided by Joseph Lagrange.