Given $n$, find smallest number $m$ such that $m!$ ends with $n$ zeros

421 Views Asked by At

I got this question as a programming exercise. I first thought it was rather trivial, and that $m = 5n$ because the number of trailing zeroes are given by the number of factors of 5 in $m!$ (and factors of 2, but there are always a lot more of those).

But it seems like this is not true, because I'm not getting the correct answers for certain $n$. Any hints?

1

There are 1 best solutions below

2
On

The answer lies in the very interesting observation made in this post: How come the number $N!$ can terminate in exactly $1,2,3,4,$ or $6$ zeroes but never $5$ zeroes?

Basically, for some n there's just no solution, unless you read "ends with n zeros" as "ends with at least n zeros", but computers don't do that.