Factorial Trailing Zeroes (LeetCode 172): Solution and Explanation
Count the zeros at the end of n! without computing it. Every zero is a factor of 10 = 2 x 5, factors of 2 are plentiful, so the answer is the number of 5s in n!, which is n//5 + n//25 + n//125 + ....
Intuition
n! grows past 64 bits at n = 21, so computing it and counting zeros does not scale. A trailing zero means the number is divisible by 10, and each 10 needs one factor of 2 and one factor of 5.
Among 1..n every second number brings a 2 but only every fifth brings a 5, so 5s are the scarce half of each pair. The number of trailing zeros is exactly the number of factors of 5 in n!.
Multiples of 5 bring one 5 each, but multiples of 25 bring a second, multiples of 125 a third, and so on. So count n//5 numbers, then add n//25 for the second 5s, then n//125, until the power of 5 exceeds n.
Approach: Sum of n // 5^k (Legendre's formula)
- Start with count = 0.
- While n > 0, divide n by 5 with integer division. The new n equals original // 5^k for round k: the multiples of 5^k up to the original n.
- Add the new n to count. Each of those multiples contributes its kth factor of 5.
- Stop when n reaches 0 and return count. Dividing n in place avoids computing 5^k, which could overflow.
Step-by-step example
n = 30
- Round 1: 30 // 5 = 6. The numbers 5, 10, 15, 20, 25, 30 each contribute a 5. count = 6.
- Round 2: 6 // 5 = 1. Only 25 is a multiple of 25, and it contributes a second 5. count = 7.
- Round 3: 1 // 5 = 0. The loop stops.
- 30! = 265252859812191058636308480000000 ends in 7 zeros.
Code: Python, Java, C++
1class Solution:
2 def trailingZeroes(self, n: int) -> int:
3 count = 0
4 while n > 0:
5 n //= 5
6 count += n
7 return count1class Solution {
2 public int trailingZeroes(int n) {
3 int count = 0;
4 while (n > 0) {
5 n /= 5;
6 count += n;
7 }
8 return count;
9 }
10}1class Solution {
2public:
3 int trailingZeroes(int n) {
4 int count = 0;
5 while (n > 0) {
6 n /= 5;
7 count += n;
8 }
9 return count;
10 }
11};Time and space complexity
O(log n)
n shrinks by a factor of 5 each iteration, so the loop runs about log base 5 of n times, 6 times for n = 10^4.
O(1)
Only n and count are stored.
Other approaches
Count 5s in every multiple of 5 (time O(n), space O(1))
For each multiple of 5 up to n, divide by 5 while possible and add one per division. It is direct and correct but runs in O(n) instead of O(log n).
Compute n! with big integers (time O(n^2) digit work, space O(n log n) bits)
Build the exact factorial and strip zeros from the end. It works for small n but the number has thousands of digits for n = 10^4, so it is far slower and does not meet the follow-up.
Common mistakes
- Counting only n // 5. Numbers like 25, 50 and 125 contribute more than one 5.
- Computing n! directly, which overflows 64-bit integers at n = 21.
- Multiplying a power of 5 up instead of dividing n down; in other inputs the power can overflow before the loop ends.
- Counting factors of 2 as well. They are never the limit, so they do not change the answer.
Frequently asked questions
Why are factors of 2 never the bottleneck?
The number of 2s in n! is n//2 + n//4 + n//8 + ..., and each term is at least the matching term n//5, n//25, ... for the 5s. So there are always at least as many 2s as 5s.
Why does dividing n in place give n // 25 on the second round?
Integer division composes: (n // 5) // 5 = n // 25. So after k rounds, n holds the original n // 5^k.
Is there a recursive form?
Yes: zeros(n) = 0 if n = 0, else n // 5 + zeros(n // 5). It is the same sum written recursively.