LeetCode in Python after 172. Factorial Trailing Zeroes zero factorial

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

Example 1:

Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example 2:

Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.

Is looking for n! Expression after decomposition number 5, because 5 * 2 = 10, and 2 before May, more than a certain number of more than five. 5 th and the like as 25,125,625 5 comprising a plurality of consecutive 5 divided by 5 to calculate the excess. Is n / 5 + n / 25 + n / 125 ... i.e. n / 5 + n / 5/5 + n / 5/5/5, while loop as follows:

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

Guess you like

Origin www.cnblogs.com/lowkeysingsing/p/11284242.html