Friday, September 28, 2012

Trailing zeros in 100!

Question: How many zeros are there in 100! (100 factorial)?

Strategy:
A trailing zero is formed when a multiple of 5 is multiplied with a multiple of 2. Now all we have to do is count the number of 5′s and 2′s in the multiplication.
Let’s count the 5′s first. 5, 10, 15, 20, 25 and so on making a total of 20. However there is more to this. Since 25, 50, 75 and 100 have two 5′s in each of them (25 = 5 * 5, 50 = 2 * 5 * 5, …), you have to count them twice. This makes the grand total 24. For people who like to look at it from a formula point of view
Number of 5′s = 100/5 + 100/25 + 100/125 + … = 24 (Integer values only)
Moving on to count the number of 2′s. 2, 4, 6, 8, 10 and so on. Total of 50 multiples of 2′s, 25 multiples of 4′s (count these once more), 12 multiples of 8′s (count these once more) and so on… The grand total comes out to
Number of 2′s = 100/2 + 100/4 + 100/8 + 100/16 + 100/32 + 100/64 + 100/128 + … = 97 (Integer values only)
Each pair of 2 and 5 will cause a trailing zero. Since we have only 24 5′s, we can only make 24 pairs of 2′s and 5′s thus the number of trailing zeros in 100 factorial is 24.

1 comment:

  1. #include
    #include
    int fun(int n,int i)
    {
    static int sum=0;
    if(!(n/(pow(5,i))))
    {
    return 0;
    }
    else
    sum+=n/(pow(5,i));
    fun(n,i+1);
    return sum;


    }
    int main(void) {
    printf("%d",fun(100,1));
    return 0;
    }

    ReplyDelete