FCTRL: Why divide by 5

Can someone explain this problem to me? I’ve completed it, but I’m not sure why we divide by 5 to get the trailing zeros. Matter of fact I’m not even sure what the trailing zeros is even about.

See, Jeramy, when we compute factorial of a number n, we multiply each natural number starting from 1 upto n. But this problem does not demand value of factorial, but only the number of trailing zeroes in a factorial (zeroes at the end of a number, e.g. 12000 has 3 trailing zeroes).

So, we only need to find the trailing zeroes of a factorial.

Now, to your question, why divide by 5. I think you need to ask, why not 10?? So, let’s say you’re to find Z(n)(no. of trailing zeroes) for n! , so, you can just find 1.2.3…(n-1).n and then look into zeroes of it or you can see how many numbers from 1 to N possess factors which are 2 or 5(so as to make it 10 on multiplication)

e.g. 4! = 1*2*3*4 = 24 (Z(n) = 0)
and  5! = 1*2*3*4*5 = 120 (Z(n) = 1)

Here you can see that in 4!, 2,2,2 are the factors of 4! which are either 2 or 5(2 from 2 and 2 2’s from 4) but there isn’t any 5 present so, there is no possibility of a zero.

But in the case of 5, we get one 5 as a factor of 5!, so we’ve now got one 5 against 3 2’s, one 5 and one 2 can make a zero but other two 2’s will not be contributing for zeroes.

So, in every case, you can easily find that the number of 2’s are greater than that of 5’s.

So, the only thing determining the number of trailing zeroes in n! here is the number of 5’s dividing n!.

Hope this helps :slight_smile:


Very nice answer :wink:

thanks betlista… :slight_smile: