Open In App

Check if the remainder of N-1 factorial when divided by N is N-1 or not

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given an integer N where 1 ? N ? 105, the task is to find whether (N-1)! % N = N – 1 or not.
Examples:

Input: N = 3 
Output: Yes 
Explanation: 
Here, n = 3 so (3 – 1)! = 2! = 2 
=> 2 % 3 = 2 which is N – 1 itself

Input: N = 4 
Output: No 
Explanation: 
Here, n = 4 so (4 – 1)! = 3! = 6 
=> 6 % 3 = 0 which is not N – 1.

Naive approach: To solve the question mentioned above the naive method is to find (N – 1)! and check if (N – 1)! % N = N – 1 or not. But this approach will cause an overflow since 1 ? N ? 105

Efficient approach: To solve the above problem in an optimal way we will use Wilson’s theorem which 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

So, now we just have to check if N is a prime number(including 1) or not.

Below is the implementation of the above approach:

C++




// C++ implementation to check
// the following expression for
// an integer N is valid or not
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number
// holds the condition
// (N-1)! % N = N - 1
bool isPrime(int n)
{
    // Corner cases
    if (n == 1)
        return true;
    if (n <= 3)
        return true;
 
    // Number divisible by 2
    // or 3 are not prime
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    // Iterate from 5 and keep
    // checking for prime
    for (int i = 5; i * i <= n; i = i + 6)
 
        if (n % i == 0
            || n % (i + 2) == 0)
            return false;
 
    return true;
}
 
// Function to check the
// expression for the value N
void checkExpression(int n)
{
    if (isPrime(n))
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver Program
int main()
{
    int N = 3;
    checkExpression(N);
    return 0;
}


Java




// Java implementation to check
// the following expression for
// an integer N is valid or not
class GFG{
 
// Function to check if a number
// holds the condition
// (N-1)! % N = N - 1
static boolean isPrime(int n)
{
     
    // Corner cases
    if (n == 1)
        return true;
    if (n <= 3)
        return true;
 
    // Number divisible by 2
    // or 3 are not prime
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    // Iterate from 5 and keep
    // checking for prime
    for(int i = 5; i * i <= n; i = i + 6)
       if (n % i == 0 || n % (i + 2) == 0)
           return false;
            
    return true;
}
 
// Function to check the
// expression for the value N
static void checkExpression(int n)
{
    if (isPrime(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver code
public static void main(String[] args)
{
    int N = 3;
     
    checkExpression(N);
}
}
 
// This code is contributed by shivanisinghss2110


Python3




# Python3 implementation to check
# the following expression for
# an integer N is valid or not
 
# Function to check if a number
# holds the condition
# (N-1)! % N = N - 1
def isPrime(n):
     
    # Corner cases
    if (n == 1):
        return True
    if (n <= 3):
        return True
 
    # Number divisible by 2
    # or 3 are not prime
    if ((n % 2 == 0) or (n % 3 == 0)):
        return False
 
    # Iterate from 5 and keep
    # checking for prime
    i = 5
    while (i * i <= n):
        if ((n % i == 0) or
            (n % (i + 2) == 0)):
            return False;
            i += 6
 
    return true;
 
# Function to check the
# expression for the value N
def checkExpression(n):
     
    if (isPrime(n)):
        print("Yes")
    else:
        print("No")
 
# Driver code
if __name__ == '__main__':
     
    N = 3
     
    checkExpression(N)
 
# This code is contributed by jana_sayantan


C#




// C# implementation to check
// the following expression for
// an integer N is valid or not
using System;
class GFG{
 
// Function to check if a number
// holds the condition
// (N-1)! % N = N - 1
static bool isPrime(int n)
{
     
    // Corner cases
    if (n == 1)
        return true;
    if (n <= 3)
        return true;
 
    // Number divisible by 2
    // or 3 are not prime
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    // Iterate from 5 and keep
    // checking for prime
    for(int i = 5; i * i <= n; i = i + 6)
       if (n % i == 0 || n % (i + 2) == 0)
           return false;
             
    return true;
}
 
// Function to check the
// expression for the value N
static void checkExpression(int n)
{
    if (isPrime(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
 
// Driver code
public static void Main()
{
    int N = 3;
     
    checkExpression(N);
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
 
    // Javascript implementation to check
    // the following expression for
    // an integer N is valid or not
 
    // Function to check if a number
    // holds the condition
    // (N-1)! % N = N - 1
    function isPrime(n)
    {
        // Corner cases
        if (n == 1)
            return true;
        if (n <= 3)
            return true;
 
        // Number divisible by 2
        // or 3 are not prime
        if (n % 2 == 0 || n % 3 == 0)
            return false;
 
        // Iterate from 5 and keep
        // checking for prime
        for (let i = 5; i * i <= n; i = i + 6)
 
            if (n % i == 0
                || n % (i + 2) == 0)
                return false;
 
        return true;
    }
 
    // Function to check the
    // expression for the value N
    function checkExpression(n)
    {
        if (isPrime(n))
            document.write("Yes");
        else
            document.write("No");
    }
     
    let N = 3;
    checkExpression(N);
 
</script>


Output: 

Yes

Time Complexity: O(sqrt(N)), as the loop will run only till (N1/2)
Auxiliary Space: O(1)



Last Updated : 25 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads