The code seems to work, but, I always get the "Time Limit Exceeded" error (the time limit defined is 6 seconds). I even tried reducing the number of iterations but am still running short on time. I am basically trying to find the number of 5's and 2's, to obtain 10.
#include <iostream>
int number_counter(long int x, int base)
{
long int val = x;
unsigned int counter = 0;
for(long int i = x; i >= 1;(!(i%base)?(i=i-base):i--))
{
val =i;
while((val % base) == 0)
{
counter++;
val = val/base;
}
}
return counter;
}
int Zfact(long int num)
{
unsigned int fives, twos;
fives = number_counter(num, 5);
twos = number_counter(num, 2);
return (fives>twos) ? twos : fives;
}
int main() {
int T; //T = Number of test cases.
cin>>T;
unsigned long int n;
for(int i=0;i < T;i++)
{
cin>>n;
int zeroes = Zfact(n);
cout<<zeroes<<endl;
}
return 0;
}