# Finding powers of any number P in N!

Prerequisite: Print all prime factors and their powers
Given natural numbers N and P, the task is to find the power of P in the factorization of N!.

Examples

Input: N = 4, P = 2
Output: 3
Explanation:
Power of 2 in the prime factorization of 4! = 24 is 3

Input: N = 24, P = 4
Output: 11

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The idea is to find the power of P for each number from 1 to N and add them as we know during multiplication power is added.
Time Complexity: O(N*P)

Efficient Approach:
To find the power of the number P in N! do the following:

1. Find all the Prime Factors of the number P with their frequency by using the approach discussed in this article. Store the Prime Factors with their frequency in map.
2. Find the power of every Prime Factors of P in the factorization of N! by using the approach discussed in this article.
3. Divide the every power obtained in the above steps by their corresponding frequency in the map.
4. Store the result of above steps in an array and minimum of those element will give the power of P in the factorisation of N!.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the power ` `// of P in N! ` `#include ` `using` `namespace` `std; ` ` `  `// Map to store all the prime ` `// factors of P ` `unordered_map<``int``, ``int``> Map; ` ` `  `// Function to find the prime ` `// factors of N im Map ` `void` `findPrimeFactors(``int` `N) ` `{ ` `    ``int` `i; ` ` `  `    ``// Clear map ` `    ``Map.clear(); ` ` `  `    ``// Check for factors of 2 ` `    ``while` `(N % 2 == 0) { ` `        ``Map += 1; ` `        ``N /= 2; ` `    ``} ` ` `  `    ``// Find all the prime factors ` `    ``for` `(i = 3; i <= ``sqrt``(N); i += 2) { ` ` `  `        ``// If i is a factors ` `        ``// then increase the ` `        ``// frequency of i ` `        ``while` `(N % i == 0) { ` `            ``Map[i] += 1; ` `            ``N /= i; ` `        ``} ` `    ``} ` ` `  `    ``if` `(N > 2) { ` `        ``Map[N] += 1; ` `    ``} ` `} ` ` `  `// Function to find the power ` `// of prime number P in N! ` `int` `PowInFactN(``int` `N, ``int` `P) ` `{ ` `    ``int` `ans = 0; ` `    ``int` `temp = P; ` ` `  `    ``// Loop until temp <= N ` `    ``while` `(temp <= N) { ` ` `  `        ``// Add the number of ` `        ``// numbers divisible ` `        ``// by N ` `        ``ans += N / temp; ` ` `  `        ``// Each time multiply ` `        ``// temp by P ` `        ``temp = temp * P; ` `    ``} ` ` `  `    ``// Returns ans ` `    ``return` `ans; ` `} ` ` `  `// Function that find the ` `// powers of any P in N! ` `int` `findPowers(``int` `N, ``int` `P) ` `{ ` ` `  `    ``// Find all prime factors ` `    ``// of number P ` `    ``findPrimeFactors(P); ` ` `  `    ``// To store the powers of ` `    ``// all prime factors ` `    ``vector<``int``> Powers; ` ` `  `    ``// Traverse the map ` `    ``for` `(``auto``& it : Map) { ` ` `  `        ``// Prime factor and ` `        ``// corres. powers ` `        ``int` `primeFac = it.first; ` `        ``int` `facPow = it.second; ` ` `  `        ``// Find power of prime ` `        ``// factor primeFac ` `        ``int` `p = PowInFactN(N, ` `                           ``primeFac); ` ` `  `        ``// Divide frequency by ` `        ``// facPow ` `        ``p /= facPow; ` ` `  `        ``// Store the power of ` `        ``// primeFac^facPow ` `        ``Powers.push_back(p); ` `    ``} ` ` `  `    ``// Return the minimum ` `    ``// element in Power array ` `    ``return` `*min_element(Powers.begin(), ` `                        ``Powers.end()); ` `} ` ` `  `// Driver's Code ` `int` `main() ` `{ ` `    ``int` `N = 24, P = 4; ` ` `  `    ``// Function to find power of ` `    ``// P in N! ` `    ``cout << findPowers(N, P); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the power ` `// of P in N! ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Map to store all the prime ` `// factors of P ` `static` `HashMap Map = ``new` `HashMap(); ` `  `  `// Function to find the prime ` `// factors of N im Map ` `static` `void` `findPrimeFactors(``int` `N) ` `{ ` `    ``int` `i; ` `  `  `    ``// Clear map ` `    ``Map.clear(); ` `  `  `    ``// Check for factors of 2 ` `    ``while` `(N % ``2` `== ``0``) { ` `        ``if``(Map.containsKey(``2``)) ` `            ``Map.put(``2``, Map.get(``2``) + ``1``); ` `        ``else` `            ``Map.put(``2``, ``1``); ` `        ``N /= ``2``; ` `    ``} ` `  `  `    ``// Find all the prime factors ` `    ``for` `(i = ``3``; i <= Math.sqrt(N); i += ``2``) { ` `  `  `        ``// If i is a factors ` `        ``// then increase the ` `        ``// frequency of i ` `        ``while` `(N % i == ``0``) { ` `            ``if``(Map.containsKey(i)) ` `                ``Map.put(i, Map.get(i) + ``1``); ` `            ``else` `                ``Map.put(i, ``1``); ` `            ``N /= i; ` `        ``} ` `    ``} ` `  `  `    ``if` `(N > ``2``) { ` `        ``if``(Map.containsKey(N)) ` `            ``Map.put(N, Map.get(N) + ``1``); ` `        ``else` `            ``Map.put(N, ``1``); ` `    ``} ` `} ` `  `  `// Function to find the power ` `// of prime number P in N! ` `static` `int` `PowInFactN(``int` `N, ``int` `P) ` `{ ` `    ``int` `ans = ``0``; ` `    ``int` `temp = P; ` `  `  `    ``// Loop until temp <= N ` `    ``while` `(temp <= N) { ` `  `  `        ``// Add the number of ` `        ``// numbers divisible ` `        ``// by N ` `        ``ans += N / temp; ` `  `  `        ``// Each time multiply ` `        ``// temp by P ` `        ``temp = temp * P; ` `    ``} ` `  `  `    ``// Returns ans ` `    ``return` `ans; ` `} ` `  `  `// Function that find the ` `// powers of any P in N! ` `static` `int` `findPowers(``int` `N, ``int` `P) ` `{ ` `  `  `    ``// Find all prime factors ` `    ``// of number P ` `    ``findPrimeFactors(P); ` `  `  `    ``// To store the powers of ` `    ``// all prime factors ` `    ``Vector Powers = ``new` `Vector(); ` `  `  `    ``// Traverse the map ` `    ``for` `(Map.Entry it : Map.entrySet()) { ` `  `  `        ``// Prime factor and ` `        ``// corres. powers ` `        ``int` `primeFac = it.getKey(); ` `        ``int` `facPow = it.getValue(); ` `  `  `        ``// Find power of prime ` `        ``// factor primeFac ` `        ``int` `p = PowInFactN(N, ` `                           ``primeFac); ` `  `  `        ``// Divide frequency by ` `        ``// facPow ` `        ``p /= facPow; ` `  `  `        ``// Store the power of ` `        ``// primeFac^facPow ` `        ``Powers.add(p); ` `    ``} ` `  `  `    ``// Return the minimum ` `    ``// element in Power array ` `    ``return` `Collections.min(Powers); ` `} ` `  `  `// Driver's Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `N = ``24``, P = ``4``; ` `  `  `    ``// Function to find power of ` `    ``// P in N! ` `    ``System.out.print(findPowers(N, P)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## C#

 `// C# program to find the power ` `// of P in N! ` `using` `System; ` `using` `System.Linq; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` ` `  `// Map to store all the prime ` `// factors of P ` `static` `Dictionary<``int``,``int``> Map = ``new` `Dictionary<``int``,``int``>(); ` ` `  `// Function to find the prime ` `// factors of N im Map ` `static` `void` `findPrimeFactors(``int` `N) ` `{ ` `    ``int` `i; ` ` `  `    ``// Clear map ` `    ``Map.Clear(); ` ` `  `    ``// Check for factors of 2 ` `    ``while` `(N % 2 == 0) { ` `        ``if``(Map.ContainsKey(2)) ` `            ``Map = Map + 1; ` `        ``else` `            ``Map.Add(2, 1); ` `        ``N /= 2; ` `    ``} ` ` `  `    ``// Find all the prime factors ` `    ``for` `(i = 3; i <= Math.Sqrt(N); i += 2) { ` ` `  `        ``// If i is a factors ` `        ``// then increase the ` `        ``// frequency of i ` `        ``while` `(N % i == 0) { ` `            ``if``(Map.ContainsKey(i)) ` `                ``Map[i] = Map[i] + 1; ` `            ``else` `                ``Map.Add(i, 1); ` `            ``N /= i; ` `        ``} ` `    ``} ` ` `  `    ``if` `(N > 2) { ` `        ``if``(Map.ContainsKey(N)) ` `            ``Map[N] =Map[N] + 1; ` `        ``else` `            ``Map.Add(N, 1); ` `    ``} ` `} ` ` `  `// Function to find the power ` `// of prime number P in N! ` `static` `int` `PowInFactN(``int` `N, ``int` `P) ` `{ ` `    ``int` `ans = 0; ` `    ``int` `temp = P; ` ` `  `    ``// Loop until temp <= N ` `    ``while` `(temp <= N) { ` ` `  `        ``// Add the number of ` `        ``// numbers divisible ` `        ``// by N ` `        ``ans += N / temp; ` ` `  `        ``// Each time multiply ` `        ``// temp by P ` `        ``temp = temp * P; ` `    ``} ` ` `  `    ``// Returns ans ` `    ``return` `ans; ` `} ` ` `  `// Function that find the ` `// powers of any P in N! ` `static` `int` `findPowers(``int` `N, ``int` `P) ` `{ ` ` `  `    ``// Find all prime factors ` `    ``// of number P ` `    ``findPrimeFactors(P); ` ` `  `    ``// To store the powers of ` `    ``// all prime factors ` `    ``List<``int``> Powers = ``new` `List<``int``>(); ` ` `  `    ``// Traverse the map ` `    ``foreach` `(KeyValuePair<``int``, ``int``> it ``in` `Map) { ` ` `  `        ``// Prime factor and ` `        ``// corres. powers ` `        ``int` `primeFac = it.Key; ` `        ``int` `facPow = it.Value; ` ` `  `        ``// Find power of prime ` `        ``// factor primeFac ` `        ``int` `p = PowInFactN(N, ` `                        ``primeFac); ` ` `  `        ``// Divide frequency by ` `        ``// facPow ` `        ``p /= facPow; ` ` `  `        ``// Store the power of ` `        ``// primeFac^facPow ` `        ``Powers.Add(p); ` `    ``} ` ` `  `    ``// Return the minimum ` `    ``// element in Power array ` `    ``return` `Powers.Min(); ` `} ` ` `  `// Driver's Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `N = 24, P = 4; ` ` `  `    ``// Function to find power of ` `    ``// P in N! ` `    ``Console.Write(findPowers(N, P)); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```11
```

Time Complexity: O(sqrt(P)*(logP N))

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.