You are given a non-negative integer N. Your task is to return the number of trailing zeroes in N! (N factorial).
A trailing zero is formed by a factor of 10, which is the product of 2 and 5. Since factorials contain more factors of 2 than factors of 5, the number of trailing zeroes can be determined by counting the total number of factors of 5 in N!.
Input: N = 5
Output: 1
Explanation: 5! = 120 has exactly one trailing zero.
Input: N = 100
Output: 24
Explanation: The number of trailing zeroes is calculated by counting the multiples of powers of 5: 100 / 5 + 100 / 25 = 20 + 4 = 24.
Input: N = 3
Output: 0
Explanation: 3! = 6 has no trailing zeroes.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
5
1
10
2