给定一个整数n,我们必须编写一个函数,该函数返回n!中的尾随零。
例如-
trailingZeroes(4) = 0 trailingZeroes(5) = 1 because 5! = 120 trailingZeroes(6) = 1
const num = 17; const findTrailingZeroes = num => { let cur = 5, total = 0; while (cur <= num) { total += Math.floor(num / cur); cur *= 5; }; return total; }; console.log(findTrailingZeroes(num)); console.log(findTrailingZeroes(5)); console.log(findTrailingZeroes(1));
输出结果
控制台中的输出将是-
3 1 0