Recently I solved a programming question in a contest asking to calculate the number of zeros at the end of factorial of a Number. Remember it asked the number of zeros in the end not the total number of zeros. There could be two methods first calculating the factorial then calculate the no of zeros second one a mathod that I am going to tell U. U should not even dare to use calculating factorial first then counting for zeros, because factrials have larger values even for sammller one. Range of unsigrned int is about 65536, do you think that you can store factorial of 100 or 1000 in this.  So, I am here to provide U an alternate way. Remember calculating factorial of large numbers is a different problem.

Mathematics has its deep usage in computers field like in every other field. Here too, it involves a mathematical formula.

Number of zeros can be calculated by the formula:

let the number be x and y be the number of zeros the end.

y=[a/5]+[a/(5^2)]+[a/(5^3)]+[a/(5^4)]+……………………

where [ ] is the greatest integer function.

Now, the problem comes how to deal with the greatest integer function here.

Integers divison is works like greatest integer function for example, what will be the output of fallowing code

int a;

a=5/2;

Output will be 2 not 2.5, so clearly U can see that integer divison works like greatest integer function. It means U are rquired to two numbers that are of integer type.

U are required to calculate sum of the seris until one of the terms gets zero as all other after this would be 0.

Advertisements