# Largest power of k in n! (factorial) where k may not be prime

• Difficulty Level : Medium
• Last Updated : 08 Jul, 2021

Given two numbers k and n, find the largest power of k that divides n!
Constraints:

` K > 1`

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

```Input : n = 7, k = 2
Output : 4
Explanation : 7! = 5040
The largest power of 2 that
divides 5040 is 24.

Input : n = 10, k = 9
Output :  2
The largest power of 9 that
divides 10! is 92.```

We have discussed a solution in below post when k is always prime.
Legendre’s formula (Given p and n, find the largest x such that p^x divides n!)
Now to find the power of any non-prime number k in n!, we first find all the prime factors of the number k along with the count of number of their occurrences. Then for each prime factor, we count occurrences using Legendre’s formula which states that the largest possible power of a prime number p in n is ⌊n/p⌋ + ⌊n/(p2)⌋ + ⌊n/(p3)⌋ + ……
Over all the prime factors p of K, the one with the minimum value of findPowerOfK(n, p)/count will be our answer where count is number of occurrences of p in k.

## C++

 `// CPP program to find the largest power``// of k that divides n!``#include ``using` `namespace` `std;` `// To find the power of a prime p in``// factorial N``int` `findPowerOfP(``int` `n, ``int` `p)``{``    ``int` `count = 0;``    ``int` `r=p;``    ``while` `(r <= n) {` `        ``// calculating floor(n/r)``        ``// and adding to the count``        ``count += (n / r);` `        ``// increasing the power of p``        ``// from 1 to 2 to 3 and so on``        ``r = r * p;``    ``}``    ``return` `count;``}` `// returns all the prime factors of k``vector > primeFactorsofK(``int` `k)``{``    ``// vector to store all the prime factors``    ``// along with their number of occurrence``    ``// in factorization of k``    ``vector > ans;` `    ``for` `(``int` `i = 2; k != 1; i++) {``        ``if` `(k % i == 0) {``            ``int` `count = 0;``            ``while` `(k % i == 0) {``                ``k = k / i;``                ``count++;``            ``}` `            ``ans.push_back(make_pair(i, count));``        ``}``    ``}``    ``return` `ans;``}` `// Returns largest power of k that``// divides n!``int` `largestPowerOfK(``int` `n, ``int` `k)``{``    ``vector > vec;``    ``vec = primeFactorsofK(k);``    ``int` `ans = INT_MAX;``    ``for` `(``int` `i = 0; i < vec.size(); i++)` `        ``// calculating minimum power of all``        ``// the prime factors of k``        ``ans = min(ans, findPowerOfP(n,``              ``vec[i].first) / vec[i].second);` `    ``return` `ans;``}` `// Driver code``int` `main()``{``    ``cout << largestPowerOfK(7, 2) << endl;``    ``cout << largestPowerOfK(10, 9) << endl;``    ``return` `0;``}`

## Java

 `// JAVA program to find the largest power``// of k that divides n!``import` `java.util.*;` `class` `GFG``{``    ` `static` `class` `pair``{``    ``int` `first, second;``    ``public` `pair(``int` `first, ``int` `second)``    ``{``        ``this``.first = first;``        ``this``.second = second;``    ``}``}``// To find the power of a prime p in``// factorial N``static` `int` `findPowerOfP(``int` `n, ``int` `p)``{``    ``int` `count = ``0``;``    ``int` `r = p;``    ``while` `(r <= n)``    ``{` `        ``// calculating Math.floor(n/r)``        ``// and adding to the count``        ``count += (n / r);` `        ``// increasing the power of p``        ``// from 1 to 2 to 3 and so on``        ``r = r * p;``    ``}``    ``return` `count;``}` `// returns all the prime factors of k``static` `Vector primeFactorsofK(``int` `k)``{``    ``// vector to store all the prime factors``    ``// along with their number of occurrence``    ``// in factorization of k``    ``Vector ans = ``new` `Vector();` `    ``for` `(``int` `i = ``2``; k != ``1``; i++)``    ``{``        ``if` `(k % i == ``0``)``        ``{``            ``int` `count = ``0``;``            ``while` `(k % i == ``0``)``            ``{``                ``k = k / i;``                ``count++;``            ``}` `            ``ans.add(``new` `pair(i, count));``        ``}``    ``}``    ``return` `ans;``}` `// Returns largest power of k that``// divides n!``static` `int` `largestPowerOfK(``int` `n, ``int` `k)``{``    ``Vector vec = ``new` `Vector();``    ``vec = primeFactorsofK(k);``    ``int` `ans = Integer.MAX_VALUE;``    ``for` `(``int` `i = ``0``; i < vec.size(); i++)` `        ``// calculating minimum power of all``        ``// the prime factors of k``        ``ans = Math.min(ans, findPowerOfP(n,``            ``vec.get(i).first) / vec.get(i).second);` `    ``return` `ans;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``System.out.print(largestPowerOfK(``7``, ``2``) +``"\n"``);``    ``System.out.print(largestPowerOfK(``10``, ``9``) +``"\n"``);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program to find the largest power``# of k that divides n!``import` `sys` `# To find the power of a prime p in``# factorial N``def` `findPowerOfP(n, p) :` `    ``count ``=` `0``    ``r ``=` `p``    ``while` `(r <``=` `n) :` `        ``# calculating floor(n/r)``        ``# and adding to the count``        ``count ``+``=` `(n ``/``/` `r)` `        ``# increasing the power of p``        ``# from 1 to 2 to 3 and so on``        ``r ``=` `r ``*` `p``     ` `    ``return` `count` `# returns all the prime factors of k``def` `primeFactorsofK(k) :` `    ``# vector to store all the prime factors``    ``# along with their number of occurrence``    ``# in factorization of k``    ``ans ``=` `[]``    ``i ``=` `2``    ``while` `k !``=` `1` `:``        ``if` `k ``%` `i ``=``=` `0` `:``            ``count ``=` `0``            ``while` `k ``%` `i ``=``=` `0` `:``                ``k ``=` `k ``/``/` `i``                ``count ``+``=` `1``            ``ans.append([i , count])``        ``i ``+``=` `1` `    ``return` `ans` `# Returns largest power of k that``# divides n!``def` `largestPowerOfK(n, k) :` `    ``vec ``=` `primeFactorsofK(k)``    ``ans ``=` `sys.maxsize``    ``for` `i ``in` `range``(``len``(vec)) :` `        ``# calculating minimum power of all``        ``# the prime factors of k``        ``ans ``=` `min``(ans, findPowerOfP(n, vec[i][``0``]) ``/``/` `vec[i][``1``])` `    ``return` `ans` `print``(largestPowerOfK(``7``, ``2``))``print``(largestPowerOfK(``10``, ``9``))` `# This code is contributed by divyesh072019`

## C#

 `// C# program to find the largest power``// of k that divides n!``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{``    ` `class` `pair``{``    ``public` `int` `first, second;``    ``public` `pair(``int` `first, ``int` `second)``    ``{``        ``this``.first = first;``        ``this``.second = second;``    ``}``}` `// To find the power of a prime p in``// factorial N``static` `int` `findPowerOfP(``int` `n, ``int` `p)``{``    ``int` `count = 0;``    ``int` `r = p;``    ``while` `(r <= n)``    ``{` `        ``// calculating Math.Floor(n/r)``        ``// and adding to the count``        ``count += (n / r);` `        ``// increasing the power of p``        ``// from 1 to 2 to 3 and so on``        ``r = r * p;``    ``}``    ``return` `count;``}` `// returns all the prime factors of k``static` `List primeFactorsofK(``int` `k)``{``    ``// vector to store all the prime factors``    ``// along with their number of occurrence``    ``// in factorization of k``    ``List ans = ``new` `List();` `    ``for` `(``int` `i = 2; k != 1; i++)``    ``{``        ``if` `(k % i == 0)``        ``{``            ``int` `count = 0;``            ``while` `(k % i == 0)``            ``{``                ``k = k / i;``                ``count++;``            ``}` `            ``ans.Add(``new` `pair(i, count));``        ``}``    ``}``    ``return` `ans;``}` `// Returns largest power of k that``// divides n!``static` `int` `largestPowerOfK(``int` `n, ``int` `k)``{``    ``List vec = ``new` `List();``    ``vec = primeFactorsofK(k);``    ``int` `ans = ``int``.MaxValue;``    ``for` `(``int` `i = 0; i < vec.Count; i++)` `        ``// calculating minimum power of all``        ``// the prime factors of k``        ``ans = Math.Min(ans, findPowerOfP(n,``            ``vec[i].first) / vec[i].second);` `    ``return` `ans;``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``Console.Write(largestPowerOfK(7, 2) +``"\n"``);``    ``Console.Write(largestPowerOfK(10, 9) +``"\n"``);``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

```4
2```

This article is contributed by ShivamKD. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.