Saturday, May 13, 2006

000000000000000...............

How many trailing 0's does 100! have? Generalize it for any N! after you have computed for 100!


N! = Factorial N = N * N-1 * N-2 * ......2 * 1

Level: Medium to Hard

2 Comments:

Blogger Sanjay said...

Lets consider the first occurrence of a trailing 0 in n! which happens at 5! (5!=120) and from then on until 9! every n! has one trailing zero. Later on a 10! produces an additional 0, so does 15! So every multiple of 5 produces an additional 0. The trouble is that 25!=25*24*23! has two zeros more than 23! or 24!.
So, we need to count the number of times each number is completely divided by 5 and keep incrementing that.
So 100! has 20 zeros (20 multiples of 5) + 4 zeros (4 multiples of 25) i.e. a total of 24 zeros.
For any n: it can be generalized as ceil(n/5)+ceil(n/5^2)+.... until n>=5^k

10:43 AM, May 16, 2006  
Blogger Karthik Nagaraj said...

Sanjay,

Interesting solution. I think you cracked it right.

Excellent! Think about a scenario if the number of 0's in the result is asked instead of the trailing 0's. Hmm, that would be toasty! But for now This puzzle is SOLVED!

11:51 AM, May 16, 2006  

Subscribe to Post Comments [Atom]

<< Home