Given two numbers
and find the greatest power of that divides
Writing the factorial expression explicitely
We can see that every
The sum is actually finite and the maximum value of
The sum discovered by
Adrien-Marie Legendre
is called
Legendreβs Formula
, let
/**
* Computes the maximum power of `k` that is a divisor of `n!`
*
* @param {int} n
* @param {int} k
* @return {int}
*/
int max_power_in_factorial(int n, int k) {
int ans = 0;
while (n) {
n /= k;
ans += n;
}
return ans;
}