Factorial Trailing Zeroes - Problem
Given an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Follow up: Could you write a solution that works in logarithmic time complexity?
Input & Output
Example 1 — Small Factorial
$
Input:
n = 3
›
Output:
0
💡 Note:
3! = 6, which has no trailing zeros
Example 2 — One Trailing Zero
$
Input:
n = 5
›
Output:
1
💡 Note:
5! = 120, which has 1 trailing zero (from 2×5=10)
Example 3 — Multiple Zeros
$
Input:
n = 25
›
Output:
6
💡 Note:
25! has 6 trailing zeros from factors: 5 multiples of 5 plus 1 extra from 25=5×5
Constraints
- 0 ≤ n ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Input
Integer n representing factorial
2
Process
Count factors of 5 in n!
3
Output
Number of trailing zeros
Key Takeaway
🎯 Key Insight: Trailing zeros come from pairs of factors 2 and 5, with 5s being the limiting factor
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code