Given two numbers N and M, the task is to find the highest power of M that divides N.
Note: M > 1
Input: N = 48, M = 4
48 % (4^2) = 0
Input: N = 32, M = 20
32 % (20^0) = 0
Approach: Initially prime factorize both the numbers N and M and store the count of prime factors in freq1 and freq2 respectively for N and M. For every prime factor of M, check if its freq2[num] is greater than freq1[num] or not. If it is for any prime factor of M, then max power will be 0. Else the maximum power will be minimum of all freq1[num] / freq2[num] for every prime factor of M.
For a number N = 24, the prime factors will 2^3 * 3^1. Hence freq1 = 3 and freq1 = 1.
Below is the implementation of the above approach:
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.
- Highest power of 2 that divides a number represented in binary
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Highest power of 2 less than or equal to given number
- Find maximum power of a number that divides a factorial
- Count of prime digits of a Number which divides the number
- Highest power of 2 less than or equal to given Integer
- Highest and Smallest power of K less than and greater than equal to N respectively
- Check if a large number is divisible by a number which is a power of 2
- Check if the number is a Prime power number
- Largest number that divides x and is co-prime with y
- Check if the sum of digits of a number N divides it
- Find the Smallest number that divides X^X
- Minimum value that divides one number and divisible by other
- Check if a given number divides the sum of the factorials of its digits
- Check if given number is a power of d where d is a power of 2
- Greatest divisor which divides all natural number in range [L, R]
- Find a number that divides maximum array elements
- Find integers that divides maximum number of elements of the array
- Find the largest composite number that divides N but is strictly lesser than N
- GCD of a number raised to some power and another number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.