Show problems as
Classic problem #172MediumMath

Factorial Trailing Zeroes

Real-world version: Zeros At The End Of The Run Figure
Tracked in this browser only
Read Factorial Trailing Zeroes on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

At the close of a production run the log posts one figure: the shift numbers 1, 2, up to n all multiplied together. A run of no shifts posts the figure 1.

Write the posted figure in decimal. Return how many zeros stand to the right of its last non-zero digit. Aim for a running time that grows only with the logarithm of n, since for n near the upper bound the figure itself runs to tens of thousands of digits.

Examples

Example 1

Input
n = 24
Output
4

The figure posted for 24 shifts is 620448401733239439360000, which ends in four zeros before its last non-zero digit 6.

Example 2

Input
n = 124
Output
28

The figure for 124 shifts is a 208-digit number ending with 28 zeros, so 28 is returned.

Example 3

Input
n = 1249
Output
308

The figure for 1249 shifts ends with 308 zeros.

Constraints

  • 0 <= n <= 10^4

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def trailing_zeroes(n: int) -> int:
Java
public int trailingZeroes(int n)