I am trying to solve the following problem:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
I submitted the following code which was rejected as too slow:
public int trailingZeroes(int n) {
int result = 0;
int k = 5;
while (k <= n){
result += n / k;
k *= 5;
}
return result;
}
But when I changed the variable k to long, it was fast enough/
Why is it faster when I declare k as long instead of int? (I read this question which, if I understand correctly, says the opposite should happen)