ให้จำนวนเต็ม 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