给定一个整数N ,其中1≤N≤10 5 ,任务是确定是否(N-1)! %N = N – 1或不。
例子:
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.
幼稚的方法:要解决上述问题,幼稚的方法是找到(N – 1)!并检查是否(N – 1)! %N = N – 1或不。但是这种方法会导致溢出,因为1≤N≤10 5
高效方法:为了以最佳方式解决上述问题,我们将使用威尔逊定理,该定理指出,当且仅当以下情况时,自然数p> 1是质数
(p – 1) ! ≡ -1 mod p
or; (p – 1) ! ≡ (p-1) mod p
因此,现在我们只需要检查N是否是质数(包括1)即可。
下面是上述方法的实现:
C++
// C++ implementation to check
// the following expression for
// an integer N is valid or not
#include
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
输出:
Yes
时间复杂度: O(sqrt(N))