KatInterview

172. Factorial Trailing Zeroes

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

A trailing zero in a number is formed by a factor of 10. Since 10 = 2 * 5, each pair of prime factors 2 and 5 in the prime factorization of n! will produce one trailing zero.

Solution

Factorial Trailing Zeroes in Python

class Solution:
    def trailingZeroes(self, n: int) -> int:
        # The number of trailing zeroes is determined by the number of factors of 5
        # in the prime factorization of n!.
        # For example, 10! has factors of 5 from 5 and 10, so two 5s.
        # 25! has factors of 5 from 5, 10, 15, 20, 25. Note that 25 = 5 * 5, so it contributes two 5s.
        # The total number of 5s is floor(n/5) + floor(n/25) + floor(n/125) + ...

        count = 0
        
        # A simple way to compute the sum is to repeatedly divide n by 5.
        # In the first iteration, n // 5 counts all multiples of 5.
        # In the second iteration, (n // 5) // 5 = n // 25 counts multiples of 25 (which contribute an extra 5).
        # And so on.
        while n > 0:
            n //= 5
            count += n
        
        return count

Factorial Trailing Zeroes in Java

class Solution {
    public int trailingZeroes(int n) {
        // The number of trailing zeroes is determined by the number of factors of 5
        // in the prime factorization of n!.
        // For example, 10! has factors of 5 from 5 and 10, so two 5s.
        // 25! has factors of 5 from 5, 10, 15, 20, 25. Note that 25 = 5 * 5, so it contributes two 5s.
        // The total number of 5s is floor(n/5) + floor(n/25) + floor(n/125) + ...

        int count = 0;
        
        // A simple way to compute the sum is to repeatedly divide n by 5.
        // In the first iteration, n/5 counts all multiples of 5.
        // In the second iteration, n/25 counts multiples of 25 (which contribute an extra 5).
        // And so on.
        while (n > 0) {
            n /= 5;
            count += n;
        }
        
        return count;
    }
}

Factorial Trailing Zeroes in C++

class Solution {
public:
    int trailingZeroes(int n) {
        // The number of trailing zeroes is determined by the number of factors of 5
        // in the prime factorization of n!.
        // For example, 10! has factors of 5 from 5 and 10, so two 5s.
        // 25! has factors of 5 from 5, 10, 15, 20, 25. Note that 25 = 5 * 5, so it contributes two 5s.
        // The total number of 5s is floor(n/5) + floor(n/25) + floor(n/125) + ...

        int count = 0;
        
        // A simple way to compute the sum is to repeatedly divide n by 5.
        // In the first iteration, n/5 counts all multiples of 5.
        // In the second iteration, n/25 counts multiples of 25 (which contribute an extra 5).
        // And so on.
        while (n > 0) {
            n /= 5;
            count += n;
        }
        
        return count;
    }
};

Factorial Trailing Zeroes in TypeScript

function trailingZeroes(n: number): number {
    // The number of trailing zeroes is determined by the number of factors of 5
    // in the prime factorization of n!.
    // For example, 10! has factors of 5 from 5 and 10, so two 5s.
    // 25! has factors of 5 from 5, 10, 15, 20, 25. Note that 25 = 5 * 5, so it contributes two 5s.
    // The total number of 5s is floor(n/5) + floor(n/25) + floor(n/125) + ...

    let count = 0;

    // A simple way to compute the sum is to repeatedly divide n by 5.
    // In the first iteration, Math.floor(n/5) counts all multiples of 5.
    // In the second iteration, Math.floor((n/5)/5) counts multiples of 25 (which contribute an extra 5).
    // And so on.
    while (n > 0) {
        n = Math.floor(n / 5);
        count += n;
    }

    return count;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.