4

Given numbers $k$ and $n$ how can I find the maximum $x$ where: $n! \equiv\ 0 \pmod{k^x}$?

I tried to compute $n!$ and then make binary search over some range $[0,1000]$ for example compute $k^{500}$ if $n!$ mod $k^{500}$ is greater than $0$ then I compute $k^{250}$ and so on but I have to compute every time value $n!$ (storing it in bigint and everytime manipulate with it is a little ridiculous) And time to compute $n!$ is $O(n)$, so very bad. Is there any faster, math solution to this problem? Math friends?:) Cheers Chris

yunone
  • 22,333

4 Answers4

7

For a prime $p$, the highest power of $p$ that divides $n!$ is $$f(n,p) = \left\lfloor \frac{n}{p} \right\rfloor + \left\lfloor \frac{n}{p^2} \right\rfloor + \left\lfloor \frac{n}{p^3} \right\rfloor + \dots.$$

So if $k$ is prime, you have the answer. (You can plug in $k$ into the above formula in place of $p$.)

Else, let the prime factorisation of $k$ be $k = p_1^{e_1}p_2^{e_2}\dots p_r^{e_r}$. Then, the highest power of $p_i^{e_i}$ dividing $n!$ is $\displaystyle\left\lfloor\frac{f(n,p_i)}{e_i}\right\rfloor$. The minimum of this expression over all $i$ gives the answer for $k$.

ShreevatsaR
  • 41,374
  • Today i was checking it on bigger test cases with wolfram, but it doesn't work correctly (?). I picked n=100! then p=13 so we have $100/13=7$ $100/(13*13)=0$ so we end up with 13^13, but the highest power is 13^127 ( wolfram told that ). Am i missing something? – Krzysztof Lewko Jul 07 '11 at 17:05
  • 1
    @Chris: The highest power of 13 dividing $100!$ is indeed $13^7$ (in $100! = 100\cdot99\cdot\dots\cdot1$, there are exactly 7 terms divisible by 13). The formula is correct. What did you try that gave you $13^{127}$? It doesn't look like the answer to anything. (And presumably you mean $n=100$ rather than $n=100!$, because the question is about the factorial of $n$.) – ShreevatsaR Jul 07 '11 at 17:15
  • i tried 100! mod 13^127 and was checking when it's greater than 0, why it's incorrect? – Krzysztof Lewko Jul 07 '11 at 17:17
  • 1
    @Chris: The remainder being greater than zero means that it's not divisible. Just think for a while: where can 127 powers of 13 come from, when you multiply the numbers 1 to 100? (And why did you stop at 127? Why not try 128, 129, 1000, 10000, or whatever? And in fact $100! \bmod 13^8 \neq 0$ (it is 313742585), which shows that $100!$ is not even divisible by $13^8$. It is only divisible by $13^7$.) – ShreevatsaR Jul 07 '11 at 17:20
  • Oh, my bad, wolfram has something like "quick calculate", but after pressing ENTER you got totally different result :) Thanks once again – Krzysztof Lewko Jul 07 '11 at 17:23
4

Computing $n!$ it is a very bad idea for great numbers $n$. To find the desired exponent you should develop something similar to the Legendre formula.

You could also search for Legendre in the following document.

Beni Bogosel
  • 23,381
4

Here is a possible approach. If $p$ is prime, the largest value of $p$ dividing $n!$ is $$\sum_{k=1}^\infty \left\lfloor {n\over p^k}\right\rfloor.$$ If the number $k$ you are considering is easily factorable into primes, you can adapt this result to your needs.

ncmathsadist
  • 49,383
  • 3
    Probably obvious, but worth pointing out to avoid confusion: the index variable $k$ is unrelated to the $k$ in the question. – ShreevatsaR Jul 05 '11 at 19:55
  • Let's take $n=10$ and $k=2$, then we have : $10/2 = 5$ $10/4=2$ $10/8=0$, so we stop we see that we have 5+2 = 7 but 2^7 is not the highest, 2^8 is. Same situation with $k=3$ highest is 3^4 not 3^3. Should we always add 1 to our result? – Krzysztof Lewko Jul 05 '11 at 19:57
  • 2
    @Chris: $\lfloor 10/8 \rfloor = 1$, not $0$. Similarly, for $k=3$, we have $\lfloor 10/3 \rfloor =3$ and $\lfloor 10/9 \rfloor = 1$, so $3+1=4$ is right. (And BTW you should think yourself about how to adapt this result for the case when $k$ is not prime; if you don't get it then see my answer.) – ShreevatsaR Jul 05 '11 at 20:03
0

A little terminology: The highest power of a prime $p$ that can divide some number $m$ is called the multiplicity (or less memorably the $p$-adic order) of $p$ in $m$, denoted $\nu_p(m)$.

For a general $k$ you would need to consider the prime factors of $k$ in turn and find which $\dfrac{\nu_{p_i}(m)}{\nu_{p_i}(k)}$ is the least.

One common question is to ask how many trailing zeroes there are on some factorial. Then $k=10$ with single prime factors $2$ and $5$.

To find $\nu_p(n!)$ is a particularly rapid evaluation. Effectively we are being asked to sum the mulitplicities of $p$ across all the values up to $n$, and of course many of these values are coprime to $p$ - in fact only $s_1:=\left\lfloor \dfrac{n}{p} \right\rfloor$ values make any contribution. Then $s_2:=\left\lfloor \dfrac{n}{p^2} \right\rfloor=\left\lfloor \dfrac{s_1}{p} \right\rfloor$ have a second power of $p$ involved, and $s_3:=\left\lfloor \dfrac{n}{p^3} \right\rfloor=\left\lfloor \dfrac{s_2}{p} \right\rfloor$ have a third power of $p$, etc. So the simple process here is to repeatedly divide each term by the prime concerned until the term falls to zero.

For example, $\nu_2(555!) = 277+138+69+34+17+8+4+2+1 = 550$ and $\nu_5(555!) = 111+22+4 = 137$ so $555!$ has $137$ trailing zeroes.

Joffan
  • 39,627