Related Articles

# Implementation of Wilson Primality test

• Difficulty Level : Medium
• Last Updated : 09 Apr, 2021

Given a number N, the task is to check if it is prime or not using Wilson Primality Test. Print ‘1’ isf the number is prime, else print ‘0’.
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:

```Input: p = 5
Output: Yes
(p - 1)! = 24
24 % 5  = 4

Input: p = 7
Output: Yes
(p-1)! = 6! = 720
720 % 7  = 6```

Below is the implementation of Wilson Primality Test

## C++

 `// C++ implementation to check if a number is``// prime or not using Wilson Primality Test``#include ``using` `namespace` `std;` `// Function to calculate the factorial``long` `fact(``const` `int``& p)``{``    ``if` `(p <= 1)``        ``return` `1;``    ``return` `p * fact(p - 1);``}` `// Function to check if the``// number is prime or not``bool` `isPrime(``const` `int``& p)``{``    ``if` `(p == 4)``        ``return` `false``;``    ``return` `bool``(fact(p >> 1) % p);``}` `// Driver code``int` `main()``{``    ``cout << isPrime(127);``    ``return` `0;``}`

## Java

 `// Java implementation to check if a number is ``// prime or not using Wilson Primality Test``public` `class` `Main``{``    ``// Function to calculate the factorial``    ``public` `static` `long` `fact(``int` `p)``    ``{``        ``if` `(p <= ``1``)``            ``return` `1``;``        ``return` `p * fact(p - ``1``);``    ``}``      ` `    ``// Function to check if the``    ``// number is prime or not``    ``public` `static` `long` `isPrime(``int` `p)``    ``{``        ``if` `(p == ``4``)``            ``return` `0``;``        ``return` `(fact(p >> ``1``) % p);``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``if``(isPrime(``127``) == ``0``)``        ``{``            ``System.out.println(``0``);``        ``}``        ``else``{``            ``System.out.println(``1``);``        ``}``    ``}``}` `// This code is contributed by divyesh072019`

## Python3

 `# Python3 implementation to check if a number is``# prime or not using Wilson Primality Test` `# Function to calculate the factorial``def` `fact(p):``    ` `    ``if` `(p <``=` `1``):``        ``return` `1` `    ``return` `p ``*` `fact(p ``-` `1``)` `# Function to check if the``# number is prime or not``def` `isPrime(p):``    ` `    ``if` `(p ``=``=` `4``):``        ``return` `0``        ` `    ``return` `(fact(p >> ``1``) ``%` `p)` `# Driver code``if` `(isPrime(``127``) ``=``=` `0``):``    ``print``(``0``)``else``:``    ``print``(``1``)` `# This code is contributed by rag2127`

## C#

 `// C# implementation to check if a number is ``// prime or not using Wilson Primality Test``using` `System;``class` `GFG {``    ` `    ``// Function to calculate the factorial``    ``static` `long` `fact(``int` `p)``    ``{``        ``if` `(p <= 1)``            ``return` `1;``        ``return` `p * fact(p - 1);``    ``}``       ` `    ``// Function to check if the``    ``// number is prime or not``    ``static` `long` `isPrime(``int` `p)``    ``{``        ``if` `(p == 4)``            ``return` `0;``        ``return` `(fact(p >> 1) % p);``    ``}``    ` `  ``static` `void` `Main() {``    ``if``(isPrime(127) == 0)``    ``{``        ``Console.WriteLine(0);``    ``}``    ``else``{``        ``Console.WriteLine(1);``    ``}``  ``}``}` `// This code is contributed by divyeshrabadiya07`

## Javascript

 ``
Output:
`1`

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)```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up