Given an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Input: n = 0
Output: 0
Follow up: Could you write a solution that works in logarithmic time complexity?
The basic idea is to count how many times 5 is a factor of numbers from 1 to n. Each multiple of 5 contributes at least one 5 to the factorization. For example, numbers like 25, 125, etc., which throw an additional power of 5, need to be considered as well.
n, stepping by 5s.n by powers of 5.The count of trailing zeroes is determined by the number of times 5 is a factor in the numbers from 1 to n. The mathematical insight is that we only count multiples of 5, 25, 125, etc., because they contribute one or more extra 5s.
n.n by 5, 25, 125... (i.e., continuously dividing by 5) and summing the quotient.n by a factor of 5.