Ctrl K

Factorials With Exactly K Trailing Zeros

Hard
The factorial is x! = 1 · 2 · … · x, with 0! = 1. For instance, 10! = 3628800 ends in two zeros. Given zeros, count how many non-negative integers x have x! ending in exactly zeros zeros in decimal. For large zeros you cannot compute the factorials themselves, so look for the pattern.
Example 1
zeros=1 → 5

Exactly one trailing zero appears in 5!, 6!, 7!, 8! and 9! (5! = 120, while 10! already ends in 00). The answer is 5.

Example 2
zeros=5 → 0

24! ends in 4 zeros and 25! jumps straight to 6, because the factor 25 adds two at once. Exactly five never happens, so the answer is 0.

Example 3
zeros=0 → 5
Constraints
  • 0 ≤ zeros ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Base of All Ones HardK-th Value in a Times Table HardN-th Multiple of Either Divisor HardPrototype Drop Tests HardNumbers From Working Keys Hard
loading Python…
Input
zeros=1
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 105 tests on the server, hidden ones included.