Cho số n, tìm số chữ số 0 ở cuối của n! (số lần 10 chia hết, tương đương số cặp 2*5, chỉ cần đếm thừa số 5).
Ví dụ
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));
Kết quả
3 1 0