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 the 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 divisors if and only if y/GCD has such unique prime divisor. So we have to find uniqueness for pair (x, y/GCD) using recursion.
Below is the implementation of the above approach:
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 ; int z = __gcd(x, y); if (z == 1) return false ; return isDivisible(x, y / z); } // 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. public 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. |
Javascript
<script> // Javascript 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 ; var z = gcd(x, y); if (z == 1) return false ; return isDivisible(x, y / z); } // Driver program to test above functions var x = 18, y = 12; if (isDivisible(x, y)) document.write( "Yes" ); else document.write( "No" ); // This code is contributed by Amit Katiyar </script> |
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 ?> |
Yes
Time Complexity: The 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(log2y)
Auxiliary Space: O(log min(x, y))
Approach 2:Factorization:
Here’s another approach to check if all prime factors of y divide x:
- Find all the prime factors of y.
For each prime factor p, check if it divides x or not. If it doesn’t, return false.
If all prime factors of y divide x, return true. - First, the function isDivisible() finds all the prime factors of y using trial division. It starts with 2 and goes up to the square root of y, checking if each number i divides y or not. If i divides y, it is added to a vector factors. The loop continues until i*i becomes greater than y. If y is still greater than 1 after this loop, it means that y itself is a prime factor and it is added to the vector.
- Next, the function checks if each prime factor in the vector factors divides x or not. If any prime factor does not divide x, the function returns false. Otherwise, if all prime factors of y divide x, the function returns true.
Here is the code of this approach:
C++
#include <bits/stdc++.h> using namespace std; // Function to check if all prime factors of y divide x bool isDivisible( int x, int y) { // Find all prime factors of y vector< int > factors; for ( int i = 2; i * i <= y; i++) { while (y % i == 0) { factors.push_back(i); y /= i; } } if (y > 1) factors.push_back(y); // Check if all prime factors divide x for ( int p : factors) if (x % p != 0) return false ; return true ; } // Driver code int main() { int x = 18, y = 12; if (isDivisible(x, y)) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
Java
import java.util.ArrayList; import java.util.List; class Main { // Function to check if all prime factors of y divide x static boolean isDivisible( int x, int y) { // Find all prime factors of y List<Integer> factors = new ArrayList<>(); for ( int i = 2 ; i * i <= y; i++) { while (y % i == 0 ) { factors.add(i); y /= i; } } if (y > 1 ) factors.add(y); // Check if all prime factors divide x for ( int p : factors) if (x % p != 0 ) return false ; return true ; } // Driver code 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 Prajwal Kandekar |
Python
import math # Function to check if all prime factors of y divide x def is_divisible(x, y): # Find all prime factors of y factors = [] for i in range ( 2 , int (math.sqrt(y)) + 1 ): while y % i = = 0 : factors.append(i) y / / = i if y > 1 : factors.append(y) # Check if all prime factors divide x for p in factors: if x % p ! = 0 : return False return True # Driver code if __name__ = = '__main__' : x = 18 y = 12 if is_divisible(x, y): print ( "Yes" ) else : print ( "No" ) |
C#
using System; using System.Collections.Generic; class Program { // Function to check if all prime factors of y divide x static bool IsDivisible( int x, int y) { // Find all prime factors of y List< int > factors = new List< int >(); for ( int i = 2; i * i <= y; i++) { while (y % i == 0) { factors.Add(i); y /= i; } } if (y > 1) factors.Add(y); // Check if all prime factors divide x foreach ( int p in factors) if (x % p != 0) return false ; return true ; } // Driver code static void Main( string [] args) { int x = 18, y = 12; if (IsDivisible(x, y)) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } |
Javascript
// Function to check if all prime factors of y divide x function isDivisible(x, y) { // Find all prime factors of y let factors = []; for (let i = 2; i * i <= y; i++) { while (y % i === 0) { factors.push(i); y /= i; } } if (y > 1) factors.push(y); // Check if all prime factors divide x for (let p of factors) if (x % p !== 0) return false ; return true ; } // Driver code let x = 18, y = 12; if (isDivisible(x, y)) console.log( "Yes" ); else console.log( "No" ); |
Output
Yes
Time Complexity: O(sqrt(y) + log(x)), where sqrt(y) is the time required to find all prime factors of y, and log(x) is the time required to check if each prime factor
Auxiliary Space: O(sqrt(y)), as we are storing the prime factors in a vector.
This article is contributed by Aarti_Rathi and Harsha Mogali. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!