Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
题目意思:
n求阶乘以后,其中有多少个数字是以0结尾的。
方法一:
class Solution: # @return an integer def trailingZeroes(self, n): res = 0 if n < 5: return 0 else: return n/5+ self.trailingZeroes(n/5)
方法二:
class Solution: # @return an integer def trailingZeroes(self, n): res = 0 x = 5 while( n >= x): res += n/x x *= 5 return res
参考博文:http://www.tuicool.com/articles/RZZnQf
n!后缀0的个数 = n!质因子中5的个数 = floor(n/5) + floor(n/25) + floor(n/125) + ....