Skip to content

Trailing Zeros Of A Factorial

Easy#factor-counting#math~20 min

The mission

Problem

Given a non-negative whole number n, report how many zeros sit at the end of n factorial. The factorial itself is astronomically large for even modest n, so computing it and then counting characters is not an option.

Examples

Input:  n = 5
Output: 1
Why:    120 ends in a single zero
Input:  n = 30
Output: 7
Input:  n = 0
Output: 0
Why:    edge case, 0! is 1 and has no trailing zero

Hints

0 / 3

Target time
O(log n)
Target space
O(1)

Constraints

  • n ≥ 0

Stuck on the idea rather than the code? Sieve of Eratosthenes covers it.

Python 3Ctrl+Enter runs
Custom test case