191. Factorial Trailing Zeroes

Medium · Math

Given an integer n, return the number of trailing zeroes in n! (n factorial).

Trailing zeroes are produced by factors of 10, and 10 = 2 × 5. In any factorial, there are always more factors of 2 than factors of 5, so the number of trailing zeroes is determined by counting the factors of 5 in n!.

To count factors of 5 in n!, divide n by 5, then by 25, then by 125, and so on, summing the results.

Examples

Example 1
Input: n = 5
Output: 1
Explanation: 5! = 120, which has 1 trailing zero (from 5 × 2 = 10).
Example 2
Input: n = 25
Output: 6
Explanation: 25! has 6 trailing zeroes. We count: ⌊25/5⌋ = 5, ⌊25/25⌋ = 1, total = 5 + 1 = 6.

Constraints