JavaScript:查找阶乘末尾的零
给定一个整数 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
广告