I'm writing a function that returns the number of trailing zeros of a factorial. It seems that the amount of zeros increases from 0! for every 5 positive increments, but also increases 1 zero for every 25 increments. The problem is that when I pass in a number like 1000, it should return 249, but it returns 240 instead. Am I missing something?
I'm almost positive there is a better way to write this, but hopefully, someone can tell me what I'm missing in my current function?
Note: This function must not calculate the actual factorial to determine the number of trailing zeros.
function zeros(n) {
const extraZeros = n/25
const zeros = n/5
if(extraZeros >= 1) {
return zeros + Math.floor(extraZeros)
}else{
return Math.floor(zeros)
}
}
console.log(zeros(0), 0);
console.log(zeros(5), 1);
console.log(zeros(6), 1);
console.log(zeros(30), 7);
console.log(zeros(60), 14);
console.log(zeros(1000), 249); // missing 9 zeros?
Any help would be greatly appreciated, Thank you!