Factorial Trailing Zeroes

Problem: Factorial Trailing Zeroes

The zeroes come from 5 * 2, so what we want are numbers of factor 5 since we always have plenty of 2. We first need numbers of factor 5, then numbers of factor 25, 125, .... Although 25 can produce 2 zeroes, one of them is already calculated while calculating numbers of factor 5.

Code in Python:

class Solution(object):
    def trailingZeroes(self, n):
        """
        :type n: int
        :rtype: int
        """
        res = 0
        while n > 0:
            n = n/5
            res += n
        return res

results matching ""

    No results matching ""