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

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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

Yes

Time Complexity: O(sqrt(N))
 

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.