Trailing Zeros of n Factorial
Problem statement
Given an integer n, return how many zeros n! ends with, where n! = 1 * 2 * ... * n and 0! = 1.
For example, 10! = 3628800 ends with 2 zeros. Do not compute n! itself: for large n it has billions of digits.
Function
factorialTrailingZeros(n: int) → intExamples
Example 1
n = 25return = 6Among 1 to 25, the multiples of 5 are 5, 10, 15, 20 and 25, and 25 is 5 times 5, so there are 6 fives in all. There are plenty of twos to pair with them, so 25! ends in 6 zeros.
Example 2
n = 4return = 04! = 24 has no factor 5, so it ends in no zeros.
Example 3
n = 100return = 24There are 20 multiples of 5 up to 100 and 4 multiples of 25, each giving one extra five: 20 + 4 = 24 zeros.
Constraints
0 <= n <= 10^9