📌  相关文章
📜  检查一个数字是否可被另一个数字的所有主除数整除

📅  最后修改于: 2021-04-24 20:21:35             🧑  作者: Mango

给定两个整数。我们需要确定第一个数字x是否可以被y的所有素数除尽。
例子 :

Input  : x = 120, y = 75
Output : Yes
Explanation :
120 = (2^3)*3*5
75  = 3*(5^2)
120 is divisible by both 3 and 5 which 
are the prime divisors of 75. Hence, 
answer is "Yes".

Input  :  x = 15, y = 6
Output : No
Explanation : 
15 = 3*5.
 6 = 2*3,
15 is not divisible by 2 which is a 
prime divisor of 6. Hence, answer 
is "No".

一个简单的解决方案是找到y的所有素因子。对于每个素数因子,检查其是否除以x。
一个有效的解决方案基于以下事实。
1)如果y == 1,则没有素数除数。因此答案是“是”
2)我们找到x和y的GCD。
a)如果GCD == 1,则显然没有x和y的共同因数,因此答案为“否”。
b)如果GCD> 1,则GCD包含除以x的素数除数。现在,当且仅当y / GCD具有此类唯一除数时,我们才具有唯一质除数。因此,我们必须使用递归找到对(x,y / GCD)的唯一性。

C++
// CPP program to find if all prime factors
// of y divide x.
#include 
using namespace std;
 
// Returns true if all prime factors of y
// divide x.
bool isDivisible(int x, int y)
{
    if (y == 1)
        return true;
 
    if (__gcd(x, y) == 1)
        return false;
    return isDivisible(x, y / gcd);
}
 
// Driver Code
int main()
{
    int x = 18, y = 12;
    if (isDivisible(x, y))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}


Java
// Java program to find if all
// prime factors of y divide x.
class Divisible
{
    public static int gcd(int a, int b) {
      return b == 0 ? a : gcd(b, a % b); }
     
    // Returns true if all prime factors
    // of y divide x.
    static boolean isDivisible(int x, int y)
    {
        if (y == 1)
            return true;
             
        int z = gcd(x, y);
     
        if (z == 1)
            return false;
     
        return isDivisible(x, y / z);
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        int x = 18, y = 12;
        if (isDivisible(x, y))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
// This code is contributed by Prerna Saini


Python3
# python program to find if all
# prime factors of y divide x.
 
def gcd(a, b):
    if(b == 0):
        return a
    else:
        return gcd(b, a % b)
     
# Returns true if all prime
# factors of y divide x.
def isDivisible(x,y):
     
    if (y == 1):
        return 1
 
    z = gcd(x, y);
     
    if (z == 1):
        return false;
     
    return isDivisible(x, y / z);
 
# Driver Code
x = 18
y = 12
if (isDivisible(x, y)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Sam007


C#
// C# program to find if all
// prime factors of y divide x.
using System;
 
class GFG {
     
    public static int gcd(int a, int b)
    {
        return b == 0 ? a : gcd(b, a % b);
    }
     
    // Returns true if all prime factors
    // of y divide x.
    static bool isDivisible(int x, int y)
    {
        if (y == 1)
            return true;
             
        int z = gcd(x, y);
     
        if (z == 1)
            return false;
     
        return isDivisible(x, y / z);
    }
 
    // Driver program to test above functions
    public static void Main()
    {
        int x = 18, y = 12;
         
        if (isDivisible(x, y))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

Yes