Leetcode: Factorial Trailing Zeroes

Question

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Show Tags
Show Similar Problems

Solution

Analysis

Count how many factor 5 in n!

Code

class Solution(object):
    def trailingZeroes(self, n):
        """ :type n: int :rtype: int """

        res = 0
        while n!=0:
            res = res + n/5
            n /= 5

        return res

你可能感兴趣的:(Leetcode: Factorial Trailing Zeroes)