Given a number N, the task is to check if it is prime or not using Wilson Primality Test. Print '1' if 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 pExamples:
Input: p = 5
Output: Yes
Explanation: (p - 1)! = 24
24 % 5 = 4Input: p = 7
Output: Yes
Explanation: (p-1)! = 6! = 720
720 % 7 = 6
Below is a simple implementation of Wilson Primality Test
// C++ implementation to check if a number is
// prime or not using Wilson Primality Test
#include <bits/stdc++.h>
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 == 1)
return false;
return (fact(p-1) % p == p-1);
}
// Driver code
int main()
{
cout << isPrime(17);
return 0;
}
public class WilsonPrimalityTest {
// 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 boolean isPrime(int p) {
if (p == 1) {
return false;
}
return fact(p - 1) % p == p - 1;
}
// Driver code
public static void main(String[] args) {
System.out.println(isPrime(17));
}
}
def fact(p):
if p <= 1:
return 1
return p * fact(p - 1)
def is_prime(p):
if p == 1:
return False
return fact(p - 1) % p == p - 1
# Driver code
if __name__ == "__main__":
print(is_prime(17))
using System;
class WilsonPrimalityTest
{
// 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 bool IsPrime(int p)
{
if (p == 1)
return false;
return Fact(p - 1) % p == p - 1;
}
// Driver code
static void Main()
{
Console.WriteLine(IsPrime(17));
}
}
// Function to calculate the factorial
function fact(p) {
if (p <= 1)
return 1;
return p * fact(p - 1);
}
// Function to check if the number is prime or not
function isPrime(p) {
if (p === 1)
return false;
return fact(p - 1) % p === p - 1;
}
// Driver code
console.log(isPrime(17));
Output
1
Please note that the Wilson's implementation cannot be used even for slightly large numbers due to integer overflow that happens in factorial computation. For example, the above code works file till 34 and fails for 35 and numbers after it. Below is an optimized implementation of the Wilson Primality Test that works faster and works till 41.
// C++ implementation to check if a number is
// prime or not using Wilson Primality Test
#include <bits/stdc++.h>
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 == 1 || p == 4)
return false;
if (p == 2 || p == 3)
return true;
return bool(fact(p >> 1) % p);
}
// Driver code
int main()
{
cout << isPrime(37);
return 0;
}
// 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 == 1 || p == 4)
return false;
if (p == 2 || p == 3)
return true;
return (fact(p >> 1) % p);
}
public static void main(String[] args)
{
if (isPrime(37) == 0) {
System.out.println(0);
}
else {
System.out.println(1);
}
}
}
// This code is contributed by divyesh072019
# 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 == 1 || p == 4):
return False
if (p == 2 || p == 3):
return True
return (fact(p >> 1) % p)
# Driver code
if (isPrime(37) == 0):
print(0)
else:
print(1)
# This code is contributed by rag2127
// 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 == 1 || p == 4)
return false;
if (p == 2 || p == 3)
return true;
return (fact(p >> 1) % p);
}
static void Main() {
if(isPrime(37) == 0)
{
Console.WriteLine(0);
}
else{
Console.WriteLine(1);
}
}
}
// This code is contributed by divyeshrabadiya07
// Function to calculate the factorial
function fact(p) {
if (p <= 1)
return 1;
return p * fact(p - 1);
}
// Function to check if the number is prime or not
function isPrime(p) {
if (p === 1 || p === 4)
return false;
if (p === 2 || p === 3)
return true;
return (fact(p - 1) % p) === p - 1;
}
// Driver code
if (isPrime(37)) {
console.log(1);
} else {
console.log(0);
}
Output
1
How does it work?
- We can quickly check result for p = 2 or p = 3.
- 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).
- 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)
Time Complexity: O(N) as recursive factorial function takes O(N) time
Auxiliary Space: O(N), for using recursive stack space.