Check if the remainder of N-1 factorial when divided by N is N-1 or not
Last Updated :
25 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n == 1)
return true ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0
|| n % (i + 2) == 0)
return false ;
return true ;
}
void checkExpression( int n)
{
if (isPrime(n))
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int N = 3;
checkExpression(N);
return 0;
}
|
Java
class GFG{
static boolean isPrime( int n)
{
if (n == 1 )
return true ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static void checkExpression( int n)
{
if (isPrime(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
int N = 3 ;
checkExpression(N);
}
}
|
Python3
def isPrime(n):
if (n = = 1 ):
return True
if (n < = 3 ):
return True
if ((n % 2 = = 0 ) or (n % 3 = = 0 )):
return False
i = 5
while (i * i < = n):
if ((n % i = = 0 ) or
(n % (i + 2 ) = = 0 )):
return False ;
i + = 6
return true;
def checkExpression(n):
if (isPrime(n)):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
N = 3
checkExpression(N)
|
C#
using System;
class GFG{
static bool isPrime( int n)
{
if (n == 1)
return true ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static void checkExpression( int n)
{
if (isPrime(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main()
{
int N = 3;
checkExpression(N);
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n == 1)
return true ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0
|| n % (i + 2) == 0)
return false ;
return true ;
}
function checkExpression(n)
{
if (isPrime(n))
document.write( "Yes" );
else
document.write( "No" );
}
let N = 3;
checkExpression(N);
</script>
|
Time Complexity: O(sqrt(N)), as the loop will run only till (N1/2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...