The Wayback Machine - https://web.archive.org/web/20240703214229/https://www.geeksforgeeks.org/wilsons-theorem/
Open In App

Wilson’s Theorem

Last Updated : 24 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Wilson’s theorem states that a natural number p > 1 is a prime number if and only if


(p - 1) ! ≡ -1 mod p
OR (p - 1) ! ≡ (p-1) mod p

Examples:

p  = 5
(p-1)! = 24
24 % 5 = 4

p = 7
(p-1)! = 6! = 720
720 % 7 = 6

How does it work?

1) We can quickly check result for p = 2 or p = 3. 2) For p > 3: If p is composite, then its positive divisors are among the integers 1, 2, 3, 4, … , p-1 and it is clear that gcd((p-1)!,p) > 1, so we can not have (p-1)! = -1 (mod p). 3) Now let us see how it is exactly -1 when p is a prime. If p is a prime, then all numbers in [1, p-1] are relatively prime to p. And for every number x in range [2, p-2], there must exist a pair y such that (x*y)%p = 1. So

    [1 * 2 * 3 * ... (p-1)]%p 
= [1 * 1 * 1 ... (p-1)] // Group all x and y in [2..p-2]
// such that (x*y)%p = 1
= (p-1)

How can it be useful?

Consider the problem of computing factorial under modulo of a prime number which is close to input number, i.e., we want to find value of “n! % p” such that n < p, p is a prime and n is close to p. For example (25! % 29). From Wilson’s theorem, we know that 28! is -1. So we basically need to find [ (-1) * inverse(28, 29) * inverse(27, 29) * inverse(26) ] % 29. The inverse function inverse(x, p) returns inverse of x under modulo p (See

this

for details). See

this

for more applications of Wilson’s Theorem.

References:

https://en.wikipedia.org/wiki/Wilson’s_theoremhttps://primes.utm.edu/notes/proofs/Wilsons.html

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


Previous Article
Next Article

Similar Reads

Cook–Levin theorem or Cook's theorem
In computational complexity theory, the Cook–Levin theorem, also known as Cook's theorem, states that the Boolean satisfiability problem is NP-complete. That is, it is in NP, and any problem in NP can be reduced in polynomial time by a deterministic Turing machine to the Boolean satisfiability problem. Stephen Arthur Cook and L.A. Levin in 1973 ind
4 min read
Compute nCr % p | Set 4 (Chinese Remainder theorem with Lucas Theorem)
Given three numbers n, r and p, the task is to compute the value of nCr % p. Note: p is a square-free number and the largest prime factor of p ≤ 50. Examples: Input: n = 10, r = 2, p = 13Output: 6Explanation: 10C2 is 45 and 45 % 13= 6 Input: n = 6, r = 2, p = 13Output: 2 Approach: We have already discussed several other approaches of this problem.
10 min read
Fermat's little theorem
Fermat's little theorem states that if p is a prime number, then for any integer a, the number a p - a is an integer multiple of p. Here p is a prime number ap ≡ a (mod p). Special Case: If a is not divisible by p, Fermat's little theorem is equivalent to the statement that a p-1-1 is an integer multiple of p. ap-1 ≡ 1 (mod p) OR ap-1 % p = 1 Here
8 min read
Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
The positive odd numbers in ascending order as 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, .... and grouped as (1), (3, 5), (7, 9, 11), (13, 15, 17, 19), .... and so on. Thus, the first group is (1), the second group is (3, 5) and the third group is (7, 9, 11), etc. in general, the kth group contains the next k elements of the sequence. Given k, find the su
7 min read
Midy's theorem
According to Midy's theorem, if the period of a repeating decimal for [Tex]a / p [/Tex], where p is prime and [Tex]a / p [/Tex]is a reduced fraction, has an even number of digits, then dividing the repeating portion into halves and adding gives a string of 9s. Examples : a = 1 and p = 7 1/7 = 0.14285714285.. So 1/7 is a repeating decimal with 14285
12 min read
Advanced master theorem for divide and conquer recurrences
The Master Theorem is a tool used to solve recurrence relations that arise in the analysis of divide-and-conquer algorithms. The Master Theorem provides a systematic way of solving recurrence relations of the form: T(n) = aT(n/b) + f(n) where a, b, and f(n) are positive functions and n is the size of the problem. The Master Theorem provides conditi
5 min read
Extended Midy's theorem
According to Midy's theorem, if the period of a repeating decimal for [Tex]a / p [/Tex], where p is prime and [Tex]a / p [/Tex]is a reduced fraction, has an even number of digits, then dividing the repeating portion into halves and adding gives a string of 9s. For example 1/7 = 0.14285714285.. is a repeating decimal with 142857 being repeated. Now,
13 min read
Nicomachus's Theorem
Nicomachus's Theorem states that sum of cubes of first n natural numbers is equal to squares of natural number sum.[Tex]1^{3}+2^{3}+3^{3}+\cdots +n^{3}=\left(1+2+3+\cdots +n\right)^{2} [/Tex]In other words[Tex]1^{3}+2^{3}+3^{3}+\cdots +n^{3}=\left(n*(n+1)/2)^{2} [/Tex]Or we can say that the sum is equal to square of n-th triangular number.Mathemati
3 min read
Euclid Euler Theorem
According to Euclid Euler Theorem, a perfect number which is even, can be represented in the form [Tex](2^n - 1)*(2^n / 2) )) [/Tex]where n is a prime number and [Tex]2^n - 1 [/Tex]is a Mersenne prime number. It is a product of a power of 2 with a Mersenne prime number. This theorem establishes a connection between a Mersenne prime and an even perf
10 min read
Vantieghems Theorem for Primality Test
Vantieghems Theorem is a necessary and sufficient condition for a number to be prime. It states that for a natural number n to be prime, the product of [Tex]2^i - 1 [/Tex]where [Tex]0 &lt; i &lt; n [/Tex], is congruent to [Tex]n~(mod~(2^n - 1)) [/Tex]. In other words, a number n is prime if and only if.[Tex]{\displaystyle \prod _{1\leq i\leq n-1}\l
4 min read
three90RightbarBannerImg