I. Problem
Count how many trailing ‘0’ for the result of n!
II. Solution
The 0
are only contributed by both factor 2
and 5
, which means the number of trailing 0
equals the number of pairs of 2
& 5
Thus, we only need to find out how many times of the factor 2
and factor 5
in this n! is good enogh; and then min(num_2, num_5)
1 | // // Test |
III. Complexity
- Time: O(n)
- Space: O(1)