TITLE: Trailing Zeros Problem - What am I doing wrong?
QUESTION [5 upvotes]: I am using this method. Click for method.
However, I keep getting the wrong answer. I need to find for what $n$ does $n!$ end in 37 trailing zeros. I have used the above method but with one more term $a_3$ and found that $a_1=0, a_2=1, a_3=1$ are the only solutions, then you add $a_0\in\{0,1,2,3,4\}$. I keep getting 30 up to 34 as the answers. The answer is supposed to be 150 to 154. The answer I had gotten is $$a_1+6a_2+31a_3=37$$
I really appreciate everyone taking their time to provide full answers, but I am more interested in where I have gone wrong.
I am trying to figure out where I have gone wrong. Is it because I found the answer to be 30, but I am in base 5, so I need to multiply buy 5?
Edit: I see where I have gone wrong.
After finding the values of $a_1=0, a_2=1, a_3=1$, I went back and plugged them in. But what I did was plug them into:
$$a_0+5a_1+25a_2$$
$$\Rightarrow 5(1)+25(1)=30+a_0 $$
But what I should have done was this
$$a_0+5a_1+25a_2+125a_3$$
$$\Rightarrow a_0+5(0)+25(1)+125(1)$$
$$\Rightarrow n=150+a_0$$
$$\Rightarrow n=\{150,151,152,153,154\}$$

REPLY [2 votes]: To get $37$ trailing zeros, you need to have $37$ tens in the factorial.  Said more precisely, you need $37$ twos and $37$ fives (such that you get $2^{37}5^{37}$).  If you have $37$ fives, then you certainly have $37$ twos, so we just need to find the smallest number for which there are $37$ fives in all of the factors of all of the numbers up to that smallest $n$.
There $\left\lfloor \frac{n}{5}\right\rfloor$ numbers divisible by $5$ up to $n$.  However, there are $\left\lfloor\frac{n}{25}\right\rfloor$ numbers which have two factors of $5$ up to $n$.  Then there are $\left\lfloor\frac{n}{5^3}\right\rfloor$ numbers with three factors of five up to $n$, so on and so forth.  We just need to add one for each of those (for $5^2$ we already counted the first $5$ when we did $\left\lfloor\frac{n}{5}\right\rfloor$).  So you just need to see how big you need to go.
Do we need $5^3$?  Well $\frac{125}{5} + \frac{125}{25} = 25 + 5 = 30$ (not enough).  But we don't need $5^4$, since that would give: $125 + 25 + 5 = 155$ (way too big).  So I would start with @ $125$.  Right there you have $25 + 5 + 1 = 31$ fives.  We only need $6$ more.  Since we can get $6$ more by simply adding $6*5 = 30$, that's a good guess, but since that also adds another $25$, it's going to be one too many, we only need $5$ more fives:
$125 + 25 = 150$: $\frac{150}{5} + \frac{150}{25} + \left\lfloor\frac{150}{125}\right\rfloor = 30 + 6 + 1 = 37$.  So the smallest factorial is $150!$ and that should work until we get to the next five (then there will be one more zero (and note that in between we added at least $3$ more two's).  So $150!$, $151!$, $152!$, $153!$, and $154!$ will all have $37$ trailing zeros and $155!$ will have $38$ trailing zeros.