Given two integers. We need to find if the first number x is divisible by all prime divisors of 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".
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.
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(log2y)
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 firstname.lastname@example.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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check if a number has prime count of divisors
- Check if a number can be expressed as a product of exactly K prime divisors
- Check if LCM of array elements is divisible by a prime number or not
- Number of divisors of a given number N which are divisible by K
- Sum of all the prime divisors of a number
- Count of the non-prime divisors of a given number
- Generating all divisors of a number using its prime factorization
- Check if the number is a Prime power number
- Check if a prime number can be expressed as sum of two Prime Numbers
- Sum of largest divisible powers of p (a prime number) in a range
- Check if a number is divisible by 47 or not
- Check if a number is divisible by 31 or not
- Check if the given number is divisible by 71 or not
- Check if the number is divisible 43 or not
- Check if a number is divisible by 41 or not