LeetCode #172 Medium

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.

  • n is 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.
Constraints
  • 0 <= n <= 10⁴
math
Open on LeetCode ↗
02

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 // 5 of them.
  • Every multiple of 25 gives a second 5: n // 25 more.
  • Every multiple of 125 gives a third, and so on until the power of 5 passes n.
How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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!.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def trailingZeroes(self, n: int) -> int:
▶3 count = 0
▶4 while n >= 5:
▶5 n //= 5
▶6 count += n
▶7 return count
numbers multiplied into 25!, one dot per 5 found1234567891011121314151617181920212223242525n0countcount the 5s hidden in 1 to 25
n25
count0
Each trailing zero needs a 10 = 2 × 5. The faded numbers have no factor of 5, so they can only supply 2s. The others are where the 5s, and so the zeros, come from.
numbers multiplied into 25!, one dot per 5 found1234567891011121314151617181920212223242525n÷5=5multiples of 525 ÷ 5 = 5 → 5 multiples of 5 up to 25
n5
After one division, n is the number of multiples of 5 from 1 to 25. Each of them holds at least one 5.
numbers multiplied into 25!, one dot per 5 found123456789101112131415161718192021222324250count+5new 5s=5countadd 5 → count 5
count5
Each multiple of 5 gets one dot. That is 5 fives so far, but numbers like 25 hold more than one.
numbers multiplied into 25!, one dot per 5 found123456789101112131415161718192021222324255n÷5=1multiples of 255 ÷ 5 = 1 → 1 multiple of 25 up to 25
n1
Dividing again gives 25 ÷ 25, rounded down: the numbers that hold a second factor of 5. Here that is just 25.
numbers multiplied into 25!, one dot per 5 found123456789101112131415161718192021222324255count+1new 5s=6countadd 1 → count 6
count6
Each highlighted number gets one more dot for its extra 5. 25 = 5 × 5, so it supplies two 5s in total.
numbers multiplied into 25!, one dot per 5 found123456789101112131415161718192021222324251n<5n is 1, below 5 → no bigger power of 5 fits
n1
The next power, 125, is larger than 25, so no number up to 25 holds another 5. The loop stops.
numbers multiplied into 25!, one dot per 5 found12345678910111213141516171819202122232425222s in 25!≥65s in 25!→6zeros25! ends with 6 zeros
answer6
25! has 22 factors of 2 but only 6 of 5, so only 6 pairs make a 10. The count of 5s is the answer.
05

Common pitfalls

Counting each multiple of 5 only once

✗ Wrong
return n // 5
✓ Right
while n >= 5:
    n //= 5
    count += n

25 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

✗ Wrong
f = 1
for i in range(2, n + 1):
    f *= i
# then count zeros of f
✓ Right
count += 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.

06

Edge cases

n = 0

0! is 1, which has no trailing zeros. The loop never runs and 0 is returned.

07

Complexity

Time
O(log n)
Space
O(1)
Each round divides n by 5, so there are about log₅ n rounds: 5 rounds for n = 10⁴. Only the count and n are stored.
08

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.