Back to all solutions

#172 - Factorial Trailing Zeroes

Problem Description

Given an integer n, return the number of trailing zeroes in n!.

Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.

Solution

/**
 * @param {number} n
 * @return {number}
 */
var trailingZeroes = function(n) {
  return n < 5 ? 0 : Math.floor(n / 5) + trailingZeroes(n / 5);
};