Amazon Interview Question
Software Engineer / DevelopersI don't think we have to use any while loop, simply divide the number by 5 and quotient is the number of zero's at end.
So,
int numZero( int n){
return n/5;
}
sum=n/power(5,0)+n/power(5,1)+n/power(5,2) and so on .untill ou get 0 so sum will be no of zero
unsigned long CountZerosOfFactorial(unsigned long n)
- fiddler.g June 28, 2010{
unsigned long x2 = n, n2 = 0;
while (x2)
n2 += x2 /= 2;
unsigned long x5 = n, n5 = 0;
while (x5)
n5 += x5 /= 5;
return min(n2, n5);
}