DevPath

← All problems

Trailing Zeroes in Factorial

Medium math

Description

Return the number of trailing zeroes in n factorial.

Constraints

0 <= n <= 10^9

Examples

Input: n = 25

Output: 6

Hints (1)
  • Count factors of 5, then 25, then 125 and so on.

Solutions

Count factors of five. · Time O(log n) · Space O(1)

func trailingZeroes(n int) int {
    count := 0
    for n >= 5 {
        n /= 5
        count += n
    }
    return count
}

Create a free account to submit solutions, track progress and save problems.

Create account