Factorial Trailing Zeros Without Building the Factorial
Math & Bit ManipulationAuto-graded · 20 test casesLast updated
Count the base-10 trailing zeros of n! by tallying factors of 5, never constructing the enormous factorial itself.
Implement a function trailing_zeros(n) that returns how many zeros sit at the end of the base-10 representation of n! (n factorial).
You must not build n! explicitly — for large n it has far too many digits. Instead, reason about how many times 10 divides the product.
Rules:
- Each trailing zero comes from teaming up one factor of 2 with one factor of 5. In
n!, factors of 2 are plentiful, so the answer equals the number of times 5 dividesn!. - For
n = 0orn = 1, return0(both0!and1!equal1). - For any
n < 0, return0as well — the factorial is undefined, but do not raise an exception. - The answer must be exact, including values like
25,125, and625, which supply more than one factor of 5 each.
Constraints
nis an integer.-10^18 <= n <= 10^18(values may be very large; never constructn!).- The function should run in time proportional to
log₅(max(n, 1)). - Return an
int.
Examples
Example 1
Input: trailing_zeros(0)
Output: 0
Example 2
Input: trailing_zeros(5)
Output: 1
Example 3
Input: trailing_zeros(10)
Output: 2
Example 4
Input: trailing_zeros(25)
Output: 6
Hints
Hint 1
A trailing zero needs one 2 and one 5 — which of the two runs out first inside n!?
Hint 2
Add up the count of multiples of 5, then multiples of 25, then 125, and so on.
Hint 3
Repeatedly divide n by 5 and add each quotient to your total; stop once the quotient becomes 0.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each trailing zero in n! pairs a factor of 2 with a factor of 5 to form 10. In the range 1..n, even numbers are far more common than multiples of 5, so factors of 5 are always the limiting ingredient. The answer is therefore the exponent of 5 in the prime factorization of n!.
That exponent is given by summing floor(n/5) + floor(n/25) + floor(n/125) + ....
Instead of generating successive powers of 5, we shrink n with integer division each round: after one step n becomes floor(n/5), after two it becomes floor(n/25), and so on. Every quotient is added to a running total, and the loop halts once the quotient drops to 0.
Why it is correct
- The k-th quotient equals
floor(n / 5^k), which counts the multiples of5^kup ton. Each such multiple contributes one extra factor of 5 at that level. - Summing across all k tallies every factor of 5 in
n!exactly once per level — precisely the exponent we want. - The loop ends when
5^k > n, where every remaining term is0, so nothing is missed or double counted. - For
n <= 0the loop never runs and0is returned, matching the specified edge-case behavior.
Time and space complexity
- Time:
O(log₅ n)— one division per power of 5 up ton(only about 26 iterations even forn = 10^18). - Space:
O(1)extra.
def trailing_zeros(n: int) -> int:
zeros = 0
while n > 0:
n //= 5
zeros += n
return zeros