# Is there a way to figure out the $n$ such that $n!$ ends with exactly $k$ zeroes?

Given $n$, I can find the number of zeroes at the end of the decimal representation of $n!$ by $$\sum_{i=1}^\infty\left\lfloor\frac{n}{5^i}\right\rfloor.$$

Is there a way to reverse this? That is, given $k$, is there a way to find out how many $n$ exist such that $n!$ has exactly $k$ zeroes at the end of its decimal representation besides making educated guesses and checking them?

## 3 Answers

There's a way to almost reverse this. I find it easier to think about this in terms of quinary (base $5$) representations. So your formula says that the number of zeroes in the decimal representation of $n!$ is the sum of all right-shifted and truncated versions of the quinary representation of $n$. That means that the $m$-th digit $a_m$ of the quinary representation of $n$ contributes

$$\sum_{i=0}^{m-1}5^i=\frac{5^m-1}4$$

zeroes, so we have

$$4k=\sum_m a_m5^m-\sum_ma_m=n-\sum_ma_m\;.$$

So $n$ is basically $4k$, and then you have to adjust a bit because the sum of the quinary digits gets subtracted.

• You use $z$ where the question uses $k$ and then you $k$ for something else. – Henry Oct 12 '12 at 7:22
• Also the question asks "how many" values of $n$ there are for a given $k$, not what these values are. But it seems likely one won't be able to answer "how many" (even though that's bound to be either $4$ or $0$) without knowing which values of $n$ give something close to $k$. – Marc van Leeuwen Oct 12 '12 at 7:43
• @Henry: Thanks; I hate it when people do that! ;-) Fixed. – joriki Oct 12 '12 at 7:43
• @Marc: You're right; though in "Is there a way to reverse this", "this" refers to $k$ expressed as a function of $n$, so that part of the question does at least implicitly ask for something like $n$ as a function of $k$. – joriki Oct 12 '12 at 7:46
• Thanks Joriki. How do you get to $4k=\sum_m a_m5^m-\sum_m a_m$? I don't understand why the $\sum_m a_m$ parts appeared. – Laura Viers Oct 12 '12 at 9:10

Comparing the amount of information in OEIS A027868 and OEIS A007845, you may not be able to do much better than an educated guess.

Your first educated guess might be to look at $$\sum_{i=1}^\infty\frac{n}{5^i}=\frac{n}{4}.$$ But using $4k$ will be too low, because of rounding.

How far too low?

Note that if $k=776$ then $n=3120 = 4 \times 776 +16$ (or $3121,3122,3123,3124$)

while if $k=781$ then $n=3125 = 4 \times 781+1$ (or the next four).

If $4k+1$ is just below a multiple of a large power of $5$ then the amount you have to add on will be greater than if $4k+1$ is equal to or just above a multiple of a large power of $5$, but I suspect it may be quicker to do this by experiment than to seek a formula.

This is wrong:

Well, a number is going to have as many 0's at the end as it has factors of 10 in it, and since $n!$ is going to have an abundance of 2's, you only need to look for 5's. So every time you hit a $n$ that is divisible by 5, $n!$ gains another 0. So there are 5 n such that $n!$ has k 0's. Specifically, $n = 5k,\, 5k+1,\, 5k+2,\, 5k+3,\, 5k+4$

At least I'm pretty sure I'm right, but I have no experience with number theory.

Why it is wrong: 25! has 2 5s, so does 50!, 75!; 125! has 3 etc. ie, Problems with higher powers of 5. But you will have either 5 or 0 n for any k.

• You're taking about the formula already given in the question. The question was how to invert it. – joriki Oct 12 '12 at 7:02
• @joriki "given k, is there a way to find out how many n exist such that n! has exactly k zeroes at the end of its decimal representation" I say there are 5 such n and that they are 5k to 5k+4 – fhyve Oct 12 '12 at 7:04
• But $n!$ gains two zeros when $n$ hits $25$, not one. – Rick Sladkey Oct 12 '12 at 7:07
• That is too high. $25!$ has $6$ trailing zeros – Henry Oct 12 '12 at 7:08
• So there will either be 0 or 5 n? And there won't be any n for k=5, 11, 17, 6i-1 until you hit n = 125 right? – fhyve Oct 12 '12 at 7:27