Check if a number is divisible by all prime divisors of another number
Last Updated :
01 Aug, 2023
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++
#include <bits/stdc++.h>
using namespace std;
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);
}
int main()
{
int x = 18, y = 12;
if (isDivisible(x, y))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
public class Divisible
{
public static int gcd( int a, int b) {
return b == 0 ? a : gcd(b, a % b); }
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);
}
public static void main(String[] args)
{
int x = 18 , y = 12 ;
if (isDivisible(x, y))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
};
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
else :
return gcd(b, a % b)
def isDivisible(x,y):
if (y = = 1 ):
return 1
z = gcd(x, y);
if (z = = 1 ):
return false;
return isDivisible(x, y / z);
x = 18
y = 12
if (isDivisible(x, y)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
public static int gcd( int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
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);
}
public static void Main()
{
int x = 18, y = 12;
if (isDivisible(x, y))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function gcd(a , b) {
return b == 0 ? a : gcd(b, a % b); }
function isDivisible(x , y)
{
if (y == 1)
return true ;
var z = gcd(x, y);
if (z == 1)
return false ;
return isDivisible(x, y / z);
}
var x = 18, y = 12;
if (isDivisible(x, y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function gcd ( $a , $b )
{
return $b == 0 ? $a :
gcd( $b , $a % $b );
}
function isDivisible( $x , $y )
{
if ( $y == 1)
return true;
$z = gcd( $x , $y );
if ( $z == 1)
return false;
return isDivisible( $x , $y / $z );
}
$x = 18;
$y = 12;
if (isDivisible( $x , $y ))
echo "Yes" ;
else
echo "No" ;
?>
|
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;
bool isDivisible( int x, int 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);
for ( int p : factors)
if (x % p != 0)
return false ;
return true ;
}
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 {
static boolean isDivisible( int x, int 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);
for ( int p : factors)
if (x % p != 0 )
return false ;
return true ;
}
public static void main(String[] args) {
int x = 18 , y = 12 ;
if (isDivisible(x, y))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python
import math
def is_divisible(x, 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)
for p in factors:
if x % p ! = 0 :
return False
return True
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 {
static bool IsDivisible( int x, int 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);
foreach ( int p in factors) if (x % p
!= 0) return false ;
return true ;
}
static void Main( string [] args)
{
int x = 18, y = 12;
if (IsDivisible(x, y))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isDivisible(x, 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);
for (let p of factors)
if (x % p !== 0)
return false ;
return true ;
}
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.
Share your thoughts in the comments
Please Login to comment...