Intermediate Level

Implement the countTrailingZerosFactorial method that counts trailing zeroes in the factorial of n.

The input contains an integer n. Your task is to return how many trailing zeroes are present in n!, the factorial of n.

A trailing zero is produced by a factor pair of 2 and 5. Factorials contain more factors of 2 than 5, so the answer depends on how many factors of 5 are present.

Example 1
Input:
n (int) = 25
Return:
(int) 6
Example 2
Input:
n (int) = 5
Return:
(int) 1
Example 3
Input:
n (int) = 10
Return:
(int) 2

Count how many times 5 appears as a factor in the numbers from 1 to n.

Every multiple of 5 contributes at least one factor of 5. Multiples of 25 contribute one extra factor, multiples of 125 contribute another extra factor, and so on.

Add n / 5, n / 25, n / 125, until the divisor becomes larger than n.

Pseudocode:

function countTrailingZerosFactorial(n):
    count = 0
    divisor = 5
    while divisor <= n:
        count = count + n / divisor
        divisor = divisor * 5
    return count
Run your code to see the result.