#172

Factorial Trailing Zeroes

Medium Counting factors

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return how many zeros n! ends with.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: how many zeros are at the end of n!?

Why this works: each zero is a 10 = 2 × 5, and 2s are always plentiful, so count the 5s. Numbers like 25 give two 5s.

Example: n = 30

30 / 5 = 6   (5,10,15,20,25,30)30 / 25 = 1  (the extra 5 in 25)total 7 zeros

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Computing n! directly — it overflows almost immediately.