📌  相关文章
📜  检查是否可以通过将素数减为A转换为B

📅  最后修改于: 2021-04-22 06:14:08             🧑  作者: Mango

给定两个整数AB ,任务是查找如果允许您从A减去质数P多次,则是否有可能使A等于B。
例子:

方法:问题中的关键观察是我们必须将数字A表示为

A = B + X * P

,我们知道每个数字都可以被除1以外的某个质数整除。因此,如果我们发现数字的差异

A - B

如果差值大于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


输出:
是的