Highest power of a number that divides other number
Given two numbers N and M, the task is to find the highest power of M that divides N.
Note: M > 1
Examples:
Input: N = 48, M = 4
Output: 2
48 % (4^2) = 0
Input: N = 32, M = 20
Output: 0
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 the 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[2] = 3 and freq1[3] = 1.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to get the prime factors// and its count of times it dividesvoid primeFactors(int n, int freq[]){ int cnt = 0; // Count the number of 2s that divide n while (n % 2 == 0) { cnt++; n = n / 2; } freq[2] = cnt; // n must be odd at this point. So we can skip // one element (Note i = i +2) for (int i = 3; i <= sqrt(n); i = i + 2) { cnt = 0; // While i divides n, count i and divide n while (n % i == 0) { cnt++; n = n / i; } freq[i] = cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) freq[n] = 1;}// Function to return the highest powerint getMaximumPower(int n, int m){ // Initialize two arrays int freq1[n + 1], freq2[m + 1]; memset(freq1, 0, sizeof freq1); memset(freq2, 0, sizeof freq2); // Get the prime factors of n and m primeFactors(n, freq1); primeFactors(m, freq2); int maxi = 0; // Iterate and find the maximum power for (int i = 2; i <= m; i++) { // If i not a prime factor of n and m if (freq1[i] == 0 && freq2[i] == 0) continue; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if (freq2[i] > freq1[i]) return 0; // If i is a prime factor of M if (freq2[i]) { // get the maximum power maxi = max(maxi, freq1[i] / freq2[i]); } } return maxi;}// Drivers codeint main(){ int n = 48, m = 4; cout << getMaximumPower(n, m); return 0;} |
Java
// Java program to implement// the above approachclass GFG{// Function to get the prime factors// and its count of times it dividesstatic void primeFactors(int n, int freq[]){ int cnt = 0; // Count the number of 2s that divide n while (n % 2 == 0) { cnt++; n = n / 2; } freq[2] = cnt; // n must be odd at this point. So we can skip // one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i = i + 2) { cnt = 0; // While i divides n, count i and divide n while (n % i == 0) { cnt++; n = n / i; } freq[i] = cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) freq[n] = 1;}// Function to return the highest powerstatic int getMaximumPower(int n, int m){ // Initialize two arrays int freq1[] = new int[n + 1], freq2[] = new int[m + 1]; // Get the prime factors of n and m primeFactors(n, freq1); primeFactors(m, freq2); int maxi = 0; // Iterate and find the maximum power for (int i = 2; i <= m; i++) { // If i not a prime factor of n and m if (freq1[i] == 0 && freq2[i] == 0) continue; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if (freq2[i] > freq1[i]) return 0; // If i is a prime factor of M if (freq2[i] != 0) { // get the maximum power maxi = Math.max(maxi, freq1[i] / freq2[i]); } } return maxi;}// Drivers codepublic static void main(String[] args){ int n = 48, m = 4; System.out.println(getMaximumPower(n, m));}}// This code contributed by Rajput-Ji |
Python 3
import math# Python program to implement# the above approach# Function to get the prime factors# and its count of times it dividesdef primeFactors(n, freq): cnt = 0 # Count the number of 2s that divide n while n % 2 == 0: cnt = cnt + 1 n = int(n // 2) freq[2] = cnt # n must be odd at this point. So we can skip # one element (Note i = i+2) i=3 while i<=math.sqrt(n): cnt = 0 # While i divides n, count i and divide n while (n % i == 0): cnt = cnt+1 n = int(n // i) freq[int(i)] = cnt i=i + 2 # This condition is to handle the case when n # is a prime number greater than 2 if (n > 2): freq[int(n)] = 1# Function to return the highest powerdef getMaximumPower(n, m): # Initialize two arrays freq1 = [0] * (n + 1) freq2 = [0] * (m + 1) # Get the prime factors of n and m primeFactors(n, freq1) primeFactors(m, freq2) maxi = 0 # Iterate and find the maximum power i = 2 while i <= m: # If i not a prime factor of n and m if (freq1[i] == 0 and freq2[i] == 0): i = i + 1 continue # If i is a prime factor of n and m # If count of i dividing m is more # than i dividing n, then power will be 0 if (freq2[i] > freq1[i]): return 0 # If i is a prime factor of M if (freq2[i]): # get the maximum power maxi = max(maxi, int(freq1[i] // freq2[i])) i = i + 1 return maxi# Drivers coden = 48m = 4print(getMaximumPower(n, m))# This code is contributed by Shashank_Sharma |
C#
// C# program to implement// the above approachusing System;class GFG{// Function to get the prime factors// and its count of times it dividesstatic void primeFactors(int n, int []freq){ int cnt = 0; // Count the number of 2s that divide n while (n % 2 == 0) { cnt++; n = n / 2; } freq[2] = cnt; // n must be odd at this point. So we can skip // one element (Note i = i +2) for (int i = 3; i <= Math.Sqrt(n); i = i + 2) { cnt = 0; // While i divides n, count i and divide n while (n % i == 0) { cnt++; n = n / i; } freq[i] = cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) freq[n] = 1;}// Function to return the highest powerstatic int getMaximumPower(int n, int m){ // Initialize two arrays int []freq1 = new int[n + 1];int []freq2 = new int[m + 1]; // Get the prime factors of n and m primeFactors(n, freq1); primeFactors(m, freq2); int maxi = 0; // Iterate and find the maximum power for (int i = 2; i <= m; i++) { // If i not a prime factor of n and m if (freq1[i] == 0 && freq2[i] == 0) continue; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if (freq2[i] > freq1[i]) return 0; // If i is a prime factor of M if (freq2[i] != 0) { // get the maximum power maxi = Math.Max(maxi, freq1[i] / freq2[i]); } } return maxi;}// Drivers codepublic static void Main(String[] args){ int n = 48, m = 4; Console.WriteLine(getMaximumPower(n, m));}}// This code has been contributed by 29AjayKumar |
PHP
<?php// PHP program to implement// the above approach// Function to get the prime factors// and its count of times it dividesfunction primeFactors($n, $freq){ $cnt = 0; // Count the number of 2s that divide n while ($n % 2 == 0) { $cnt++; $n = floor($n / 2); } $freq[2] = $cnt; // n must be odd at this point. So we can skip // one element (Note i = i +2) for ($i = 3; $i <= sqrt($n); $i = $i + 2) { $cnt = 0; // While i divides n, count i and divide n while ($n % $i == 0) { $cnt++; $n = floor($n / $i); } $freq[$i] = $cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if ($n > 2) $freq[$n] = 1; return $freq ;}// Function to return the highest powerfunction getMaximumPower($n, $m){ $freq1 = array_fill(0,$n + 1,0); $freq2 = array_fill(0,$m + 1,0); // Get the prime factors of n and m $freq1 = primeFactors($n, $freq1); $freq2 = primeFactors($m, $freq2); $maxi = 0; // Iterate and find the maximum power for ($i = 2; $i <= $m; $i++) { // If i not a prime factor of n and m if ($freq1[$i] == 0 && $freq2[$i] == 0) continue; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if ($freq2[$i] > $freq1[$i]) return 0; // If i is a prime factor of M if ($freq2[$i]) { // get the maximum power $maxi = max($maxi, floor($freq1[$i] / $freq2[$i])); } } return $maxi;} // Drivers code $n = 48; $m = 4; echo getMaximumPower($n, $m); // This code is contributed by Ryuga?> |
Javascript
<script>// Javascript program to implement// the above approach// Function to get the prime factors// and its count of times it dividesfunction primeFactors(n, freq){ var cnt = 0; // Count the number of 2s that divide n while (n % 2 == 0) { cnt++; n = n / 2; } freq[2] = cnt; var i; // n must be odd at this point. So we can skip // one element (Note i = i +2) for (i = 3; i <= Math.sqrt(n); i = i + 2) { cnt = 0; // While i divides n, count i and divide n while (n % i == 0) { cnt++; n = n / i; } freq[i] = cnt; } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) freq[n] = 1;}// Function to return the highest powerfunction getMaximumPower(n, m){ // Initialize two arrays var freq1 = new Array(n+1); var freq2 = new Array(m+1); // Get the prime factors of n and m primeFactors(n, freq1); primeFactors(m, freq2); var maxi = 0; // Iterate and find the maximum power for(i = 2; i <= m; i++) { // If i not a prime factor of n and m if (freq1[i] == 0 && freq2[i] == 0) continue; // If i is a prime factor of n and m // If count of i dividing m is more // than i dividing n, then power will be 0 if (freq2[i] > freq1[i]) return 0; // If i is a prime factor of M if (freq2[i]) { // get the maximum power maxi = Math.max(maxi, freq1[i]/freq2[i]); } } return maxi;}// Drivers code var n = 48, m = 4; document.write(getMaximumPower(n, m));</script> |
2
Time Complexity: O(n log log n)
Auxiliary Space: O(max(m,n))


