Ctrl K

Zeros at the End of n!

Medium
You are given an integer n. How many zeros does the decimal form of n! = 1 · 2 · … · n end with? By convention, 0! = 1. The factorial itself is enormous for large n, so try to get the answer without computing it, in time logarithmic in n.
Example 1
n=7 → 1

7! = 5040, which ends in a single zero.

Example 2
n=26 → 6

Each zero comes from a pair of factors 2 and 5. Numbers up to 26 contribute six 5s: one each from 5, 10, 15, 20 and two from 25. There are more 2s than that, so there are 6 zeros.

Example 3
n=1 → 0
Constraints
  • 0 ≤ n ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumBackwards Digits in 32 Bits MediumNumber in Roman Numerals MediumDivision Without Dividing MediumMultiplying Digit Strings Medium
loading Python…
Input
n=7
Expected
1

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