Skip to content
BytePatterns

Trailing Zeros Of A Factorial

EasyMath & Number Theory#factor-counting#math~20m

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

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