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:
- Highest power of 2 that divides a number represented in binary
- Highest power of 2 less than or equal to given number
- Find maximum power of a number that divides a factorial
- Largest number that divides x and is co-prime with y
- Check if the sum of digits of a number N divides it
- Minimum value that divides one number and divisible by other
- Check if a given number divides the sum of the factorials of its digits
- Greatest divisor which divides all natural number in range [L, R]
- Find a number that divides maximum array elements
- Check if given number is a power of d where d is a power of 2
- Find integers that divides maximum number of elements of the array
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if a number is a power of another number
- GCD of a number raised to some power and another number
- Sum of digits of a given number to a given power
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.