loading

Factorial Trailing Zeroes — Step-by-Step Visualization

mediumLeetCode #172Math

Return the number of trailing zeroes in n!.

Algorithm Pattern

Count Factors of 5

Key Idea

Trailing zeros = count of 5s in n! = ⌊n/5⌋ + ⌊n/25⌋ + ...

Step-by-Step Approach

  1. While n >= 5:
  2. n = n // 5
  3. count += n
  4. Return count

Related Problems