给定两个整数A和B ,任务是查找如果允许您从A减去质数P多次,则是否有可能使A等于B。
例子:
Input: A = 10, B = 4
Output: YES
Explanation:
Let P = 2 and after subtracting it
three times from A
Input: A = 41, B = 40
Output: NO
方法:问题中的关键观察是我们必须将数字A表示为
,我们知道每个数字都可以被除1以外的某个质数整除。因此,如果我们发现数字的差异
如果差值大于1,则可以通过从A减去素数X来使两个数相等。
下面是上述方法的实现:
C++
// C++ implementation to find if
// it is possible to make a equal to b
#include
using namespace std;
// Function to find if
// it is possible to make
// A equal to B
bool isPossible(int A, int B)
{
return (A - B > 1);
}
// Driver Code
int main()
{
int A = 10, B = 4;
// Function Call
if (isPossible(A, B))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation to find if
// it is possible to make a equal to b
class GFG{
// Function to find if
// it is possible to make
// A equal to B
static boolean isPossible(int A, int B)
{
return (A - B > 1);
}
// Driver Code
public static void main (String[] args)
{
int A = 10, B = 4;
// Function Call
if (isPossible(A, B))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 implementation to find if
# it is possible to make a equal to b
# Function to find if
# it is possible to make
# A equal to B
def isPossible(A, B):
return (A - B > 1);
# Driver Code
A = 10; B = 4;
# Function Call
if (isPossible(A, B)):
print("Yes");
else:
print("No");
# This code is contributed by Code_Mech
C#
// C# implementation to find if
// it is possible to make a equal to b
using System;
class GFG{
// Function to find if
// it is possible to make
// A equal to B
static bool isPossible(int A, int B)
{
return (A - B > 1);
}
// Driver Code
public static void Main()
{
int A = 10, B = 4;
// Function Call
if (isPossible(A, B))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Code_Mech
Javascript
输出:
是的
是的