Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
public class Solution {
public int trailingZeroes(int n) {
int count_5 = 0;
int tmp = 0;
for (int i = 5; i <= n; i++) {
if (i % 5 == 0) {
count_5++;
tmp = i / 5;
while (tmp >= 5 && tmp % 5 == 0) {
count_5++;
tmp = tmp / 5;
}
}else{
continue;
}
}
return count_5;
}
}
public class Solution {
public int trailingZeroes(int n) {
int count_5 = 0;
int tmp = 0;
for (int i = 5; i <= n; i++) {
if (i % 5 == 0) {
count_5++;
tmp = i / 5;
while (tmp >= 5 && tmp % 5 == 0) {
count_5++;
tmp = tmp / 5;
}
}else{
continue;
}
}
return count_5;
}
}