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

24 Mar, 2021
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 itselfInput: 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 ≤ 105Efficient 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++
Java
Python3
C#
Javascript
// 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 - 1bool 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 Nvoid checkExpression(int n){    if (isPrime(n))        cout << "Yes";    else        cout << "No";} // Driver Programint main(){    int N = 3;    checkExpression(N);    return 0;}
// Java implementation to check// the following expression for// an integer N is valid or notclass GFG{ // Function to check if a number// holds the condition// (N-1)! % N = N - 1static 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 Nstatic void checkExpression(int n){    if (isPrime(n))        System.out.println("Yes");    else        System.out.println("No");} // Driver codepublic static void main(String[] args){    int N = 3;         checkExpression(N);}} // This code is contributed by shivanisinghss2110
# 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 - 1def 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 Ndef checkExpression(n):         if (isPrime(n)):        print("Yes")    else:        print("No") # Driver codeif __name__ == '__main__':         N = 3         checkExpression(N) # This code is contributed by jana_sayantan
// C# implementation to check// the following expression for// an integer N is valid or notusing System;class GFG{ // Function to check if a number// holds the condition// (N-1)! % N = N - 1static 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 Nstatic void checkExpression(int n){    if (isPrime(n))        Console.Write("Yes");    else        Console.Write("No");} // Driver codepublic static void Main(){    int N = 3;         checkExpression(N);}} // This code is contributed by Code_Mech
<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>
Yes
Time Complexity: O(sqrt(N)) 
shivanisinghss2110
Code_Mech
jana_sayantan
divyeshrabadiya07
factorial
Modular Arithmetic
Mathematical
Mathematical
Modular Arithmetic
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Fizz Buzz Implementation
Program to multiply two matrices
Find pair with maximum GCD in an array
Find Union and Intersection of two unsorted arrays
Count all possible paths from top left to bottom right of a mXn matrix
Count ways to reach the n'th stair