Given two integers. We need to find if the first number x is divisible by all prime divisors of y.

**Examples :**

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".

A **simple solution** is to find all prime factors of y. For every prime factor, check if it divides x or not.

An **efficient solution ** is based on below facts.

1) if y == 1, then it no prime divisors. Hence answer is “Yes”

2) We find GCD of x and y.

a) If GCD == 1, then clearly there are no common divisors of x and y, hence answer is “No”.

b) If GCD > 1, the GCD contains prime divisors which divide x also. Now, we have all unique prime divisor if and only if y/GCD has such unique prime divisor. So we have to find uniqueness for pair (x, y/GCD) using recursion.

## C++

`// CPP program to find if all prime factors ` `// of y divide x. ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find if all ` `// prime factors of y divide x. ` ` ` `function` `gcd (` `$a` `, ` `$b` `) ` `{ ` ` ` `return` `$b` `== 0 ? ` `$a` `: ` ` ` `gcd(` `$b` `, ` `$a` `% ` `$b` `); ` `} ` ` ` `// Returns true if all prime ` `// factors of y divide x. ` `function` `isDivisible(` `$x` `, ` `$y` `) ` `{ ` ` ` `if` `(` `$y` `== 1) ` ` ` `return` `true; ` ` ` ` ` `$z` `= gcd(` `$x` `, ` `$y` `); ` ` ` ` ` `if` `(` `$z` `== 1) ` ` ` `return` `false; ` ` ` ` ` `return` `isDivisible(` `$x` `, ` `$y` `/ ` `$z` `); ` `} ` ` ` `// Driver Code ` `$x` `= 18; ` `$y` `= 12; ` `if` `(isDivisible(` `$x` `, ` `$y` `)) ` ` ` `echo` `"Yes"` `; ` `else` ` ` `echo` `"No"` `; ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Yes

**Time Complexity:**Time complexity for calculating GCD is O(log min(x, y)), and recursion will terminate after log y steps because we are reducing it by a factor greater than one. Overall Time complexity: **O(log ^{2}y)**

This article is contributed by **Harsha Mogali**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Find sum of divisors of all the divisors of a natural number
- Check if count of even divisors of N is equal to count of odd divisors
- Sum of all prime divisors of all the numbers in range L-R
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Divisors of n-square that are not divisors of n
- Check if a number has prime count of divisors
- Check if a number can be expressed as a product of exactly K prime divisors
- Sum of all the prime divisors of a number
- Generating all divisors of a number using its prime factorization
- Number of divisors of a given number N which are divisible by K
- Product of divisors of a number from a given list of its prime factors
- Count of the non-prime divisors of a given number
- Check if LCM of array elements is divisible by a prime number or not
- Smallest N digit number divisible by all possible prime digits
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count of numbers below N whose sum of prime divisors is K
- Sum of numbers in a range [L, R] whose count of divisors is prime