website/content.en/ChapterFour/0100~0199/0172.Factorial-Trailing-Zeroes.md
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example 2:
Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
Given an integer n, return the number of zeroes at the end of the result of n!. Note: The time complexity of your algorithm should be O(log n).
min(number of 5s in the factorial and number of 2s).res = N/5 + N/(5^2) + N/(5^3) + ... = ((N / 5) / 5) / 5 /... . The final algorithm complexity is O(logN).
package leetcode
func trailingZeroes(n int) int {
if n/5 == 0 {
return 0
}
return n/5 + trailingZeroes(n/5)
}