Problem 80
Factorial Trailing Zeroes
Medium · 20 pts · Math
Problem
You are given an integer N. Count how many zeros N! (N factorial, the product 1 × 2 × ... × N) has at the end of its decimal form. N! is far too large to compute directly. Each trailing zero comes from a factor 10 = 2 × 5, and factors of 2 are always more plentiful than factors of 5, so count the factors of 5: N / 5 + N / 25 + N / 125 + ... using integer division. Multiples of 25 contribute an extra 5, multiples of 125 another, and so on. The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
A single integer N.
Output
Print the number of trailing zeros of N!.
Constraints
0 <= N <= 10^18
Example input
5
Example output
1
Explanation
5! = 1 × 2 × 3 × 4 × 5 = 120, which ends in one zero. With the formula: 5 / 5 = 1 and 5 / 25 = 0, so the count is 1.