Given two positive integers x and y, the task is to determine whether x is divisible by all the prime divisors of y. That means, every prime number that divides y should also divide x. If this condition holds true, print Yes, otherwise print No.
Examples:
Input: x = 120, y = 75
Output: Yes
Explanation: The prime divisors of 75 are 3 and 5. Since 120 is divisible by both 3 and 5, the answer is Yes.Input: x = 15, y = 6
Output: No
Explanation: The prime divisors of 6 are 2 and 3. Although 15 is divisible by 3, it is not divisible by 2. Hence, the answer is No.Input: x = 210, y = 30
Output: Yes
Explanation: The prime divisors of 30 are 2, 3, and 5. Since 210 is divisible by all of them, the answer is Yes.
Table of Content
[Expected Approach - 1] Finding All Prime Divisors of y
The idea is to check all prime divisors of y and verify if x is divisible by each of them. The thought process is to iterate from 2 to sqrt(y), and for every factor i, check if it's a prime divisor by dividing y completely. If x is not divisible by any such prime, we return false. After the loop, if any prime > sqrt(y) remains in y, we handle it separately.
// C++ program to check if x is divisible
// by all prime divisors of y
// by finding all divisors of y
#include <bits/stdc++.h>
using namespace std;
// Function to check if x is divisible by
// all prime factors of y
bool isDivisible(int x, int y) {
// Check for every prime factor of y
for (int i = 2; i * i <= y; i++) {
// If i divides y, then it's a factor
if (y % i == 0) {
// Check if i is prime factor of y
// and x is divisible by it
if (x % i != 0) {
return false;
}
// Remove all occurrences of i in y
while (y % i == 0) {
y /= i;
}
}
}
// If remaining y > 1, it's a prime factor
if (y > 1 && x % y != 0) {
return false;
}
return true;
}
// Driver code
int main() {
int x = 120, y = 75;
if (isDivisible(x, y)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
// Java program to check if x is divisible
// by all prime divisors of y by
// finding all divisors of y
class GfG {
// Function to check if x is divisible by
// all prime factors of y
static boolean isDivisible(int x, int y) {
// Check for every prime factor of y
for (int i = 2; i * i <= y; i++) {
// If i divides y, then it's a factor
if (y % i == 0) {
// Check if i is prime factor of y
// and x is divisible by it
if (x % i != 0) {
return false;
}
// Remove all occurrences of i in y
while (y % i == 0) {
y /= i;
}
}
}
// If remaining y > 1, it's a prime factor
if (y > 1 && x % y != 0) {
return false;
}
return true;
}
// Driver code
public static void main(String[] args) {
int x = 120, y = 75;
if (isDivisible(x, y)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
# Python program to check if x is divisible
# by all prime divisors of y by
# finding all divisors of y
# Function to check if x is divisible by
# all prime factors of y
def isDivisible(x, y):
# Check for every prime factor of y
i = 2
while i * i <= y:
# If i divides y, then it's a factor
if y % i == 0:
# Check if i is prime factor of y
# and x is divisible by it
if x % i != 0:
return False
# Remove all occurrences of i in y
while y % i == 0:
y //= i
i += 1
# If remaining y > 1, it's a prime factor
if y > 1 and x % y != 0:
return False
return True
# Driver code
if __name__ == "__main__":
x = 120
y = 75
if isDivisible(x, y):
print("Yes")
else:
print("No")
// C# program to check if x is divisible
// by all prime divisors of y by
// finding all divisors of y
using System;
class GfG {
// Function to check if x is divisible by
// all prime factors of y
static bool isDivisible(int x, int y) {
// Check for every prime factor of y
for (int i = 2; i * i <= y; i++) {
// If i divides y, then it's a factor
if (y % i == 0) {
// Check if i is prime factor of y
// and x is divisible by it
if (x % i != 0) {
return false;
}
// Remove all occurrences of i in y
while (y % i == 0) {
y /= i;
}
}
}
// If remaining y > 1, it's a prime factor
if (y > 1 && x % y != 0) {
return false;
}
return true;
}
// Driver code
static void Main() {
int x = 120, y = 75;
if (isDivisible(x, y)) {
Console.WriteLine("Yes");
} else {
Console.WriteLine("No");
}
}
}
// JavaScript program to check if x is divisible
// by all prime divisors of y by
// finding all divisors of y
// Function to check if x is divisible by
// all prime factors of y
function isDivisible(x, y) {
// Check for every prime factor of y
for (let i = 2; i * i <= y; i++) {
// If i divides y, then it's a factor
if (y % i === 0) {
// Check if i is prime factor of y
// and x is divisible by it
if (x % i !== 0) {
return false;
}
// Remove all occurrences of i in y
while (y % i === 0) {
y = Math.floor(y / i);
}
}
}
// If remaining y > 1, it's a prime factor
if (y > 1 && x % y !== 0) {
return false;
}
return true;
}
// Driver code
let x = 120, y = 75;
if (isDivisible(x, y)) {
console.log("Yes");
} else {
console.log("No");
}
Output
Yes
Time Complexity: O(sqrt(y)), we check up to sqrt(y) for factors.
Space Complexity: O(1), no extra space or data structures used.
[Expected Approach - 2] Using the GCD Concept
The idea is to check if all prime divisors of y divide x by cleverly using the GCD (Greatest Common Divisor) concept. Instead of finding and checking each prime factor individually, we repeatedly remove common factors (given by GCD) from y.
The thought process is, if we keep dividing y by gcd(x, y), we are stripping away the part of y that is also in x. If after all removals, y becomes 1, then all prime factors of y were present in x.
The key observation is: any prime factor of y that is not in x will remain in y, preventing it from reaching 1. So, if y ≠ 1 at the end, it means x does not include all prime divisors of y.
// C++ program to check if x is divisible
// by all prime divisors of y using GCD
#include <bits/stdc++.h>
using namespace std;
// Function find GCD
int gcd(int a, int b) {
while (b != 0) {
int rem = a % b;
a = b;
b = rem;
}
return a;
}
// Function to check if x is divisible by
// all prime factors of y
bool isDivisible(int x, int y) {
int g = gcd(x, y);
// Remove all common factors from y
while (g != 1) {
y /= g;
g = gcd(x, y);
}
// If remaining y is 1, all
// factors were in x
return (y == 1);
}
// Driver code
int main() {
int x = 120, y = 75;
if (isDivisible(x, y)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
// Java program to check if x is divisible
// by all prime divisors of y using GCD
class GfG {
// Function find GCD
static int gcd(int a, int b) {
while (b != 0) {
int rem = a % b;
a = b;
b = rem;
}
return a;
}
// Function to check if x is divisible by
// all prime factors of y
static boolean isDivisible(int x, int y) {
int g = gcd(x, y);
// Remove all common factors from y
while (g != 1) {
y /= g;
g = gcd(x, y);
}
// If remaining y is 1, all
// factors were in x
return (y == 1);
}
// Driver code
public static void main(String[] args) {
int x = 120, y = 75;
if (isDivisible(x, y)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
# Python program to check if x is divisible
# by all prime divisors of y using GCD
# Function find GCD
def gcd(a, b):
while b != 0:
rem = a % b
a = b
b = rem
return a
# Function to check if x is divisible by
# all prime factors of y
def isDivisible(x, y):
g = gcd(x, y)
# Remove all common factors from y
while g != 1:
y //= g
g = gcd(x, y)
# If remaining y is 1, all
# factors were in x
return y == 1
# Driver code
if __name__ == "__main__":
x = 120
y = 75
if isDivisible(x, y):
print("Yes")
else:
print("No")
// C# program to check if x is divisible
// by all prime divisors of y using GCD
using System;
class GfG {
// Function find GCD
static int gcd(int a, int b) {
while (b != 0) {
int rem = a % b;
a = b;
b = rem;
}
return a;
}
// Function to check if x is divisible by
// all prime factors of y
static bool isDivisible(int x, int y) {
int g = gcd(x, y);
// Remove all common factors from y
while (g != 1) {
y /= g;
g = gcd(x, y);
}
// If remaining y is 1, all
// factors were in x
return (y == 1);
}
// Driver code
static void Main() {
int x = 120, y = 75;
if (isDivisible(x, y)) {
Console.WriteLine("Yes");
} else {
Console.WriteLine("No");
}
}
}
// JavaScript program to check if x is divisible
// by all prime divisors of y using GCD
// Function find GCD
function gcd(a, b) {
while (b !== 0) {
let rem = a % b;
a = b;
b = rem;
}
return a;
}
// Function to check if x is divisible by
// all prime factors of y
function isDivisible(x, y) {
let g = gcd(x, y);
// Remove all common factors from y
while (g !== 1) {
y = Math.floor(y / g);
g = gcd(x, y);
}
// If remaining y is 1, all
// factors were in x
return y === 1;
}
// Driver code
let x = 120, y = 75;
if (isDivisible(x, y)) {
console.log("Yes");
} else {
console.log("No");
}
Output
Yes
Time Complexity: O(log(y) * log(x)), since each GCD operation takes O(log x) time (as x remains constant) and we may perform this operation up to O(log y) times (as y gets reduced each time).
Space Complexity: O(1), Uses constant space without any additional data structures.