Factorial Trailing Zeroes
Factorial Trailing Zeroes is LeetCode 172 (Medium). Given an integer n, return how many zeros n! ends with. For example, 5! = 120 ends with one zero.
nis between 0 and 10⁴. Even 21! is too big for a 64-bit integer, so the factorial cannot simply be computed and inspected.- The follow-up asks for a solution in logarithmic time.
- 0 <= n <= 10⁴
Intuition
Every trailing zero is a factor of 10, and 10 = 2 × 5. So the number of trailing zeros in n! is the number of (2, 5) pairs among its prime factors. Even numbers supply a 2 at every second step, while a 5 only comes every fifth step, so 2s always outnumber 5s. The answer is the number of factors of 5 in n!.
Count them by multiples:
- Every multiple of 5 gives one 5. There are
n // 5of them. - Every multiple of 25 gives a second 5:
n // 25more. - Every multiple of 125 gives a third, and so on until the power of 5 passes
n.
A question about the zeros at the end of a huge product. Zeros at the end come from 10 = 2 × 5, so count the scarcer prime factor instead of building the product.
Approach
Before reading on, count the zeros of 25! by hand, without computing it. Why is the answer not 5? Aim for O(log n) time.
Divide n by 5
Set n //= 5. The new n is the number of multiples of 5 up to the original n, which is how many numbers give at least one factor of 5.
Add it to the count
Add the new n to count. Each of those multiples has now been counted once, for its first factor of 5.
Repeat while n is at least 5
Dividing again gives n // 25 from the original input, the numbers with a second 5, then n // 125, and so on. Dividing n in place means the code never builds a power of 5, so nothing can overflow.
Return the count
Once n drops below 5, no larger power of 5 fits in the original input, and count is the total number of 5s in n!.
Code in Python, C++ and Java
Common pitfalls
Counting each multiple of 5 only once
return n // 5
while n >= 5:
n //= 5
count += n25 is 5 × 5, so it brings two 5s, and 125 brings three. For n = 25 the one-pass version returns 5, but 25! ends with 6 zeros. Every n from 25 up comes out short.
Computing the factorial first
f = 1
for i in range(2, n + 1):
f *= i
# then count zeros of fcount += n # after n //= 5
In C++ and Java a 64-bit integer overflows at 21!, so from n = 21 on the product is garbage and so is its zero count. Python's big integers do not overflow, but 10⁴! has over 35,000 digits, and building it takes far longer than the O(log n) count.
Edge cases
0! is 1, which has no trailing zeros. The loop never runs and 0 is returned.
Complexity
Factorial Trailing Zeroes FAQ
How many trailing zeros does 100! have?
24. There are 100 / 5 = 20 multiples of 5 and 100 / 25 = 4 multiples of 25, and 125 is already past 100. For 1000! the sum is 200 + 40 + 8 + 1 = 249.
Can it be written recursively?
Yes: trailingZeroes(n) = n // 5 + trailingZeroes(n // 5), with 0 returned when n is below 5. It is the same sum written as recursion, and its depth is only about log₅ n.
Can every number of zeros happen?
No. The count jumps by 2 or more at each multiple of 25: 24! ends with 4 zeros and 25! with 6, so no factorial ends with exactly 5. Problem 793, Preimage Size of Factorial Zeroes, asks how many n give exactly k zeros, and it is solved by binary search on this count, since the count never decreases as n grows.