查找阶乘 JavaScript 的尾随零
javascriptweb developmentfront end technologyobject oriented programming
给定一个整数 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