首页 > 编程语言 > 详细

LeetCode Factorial Trailing Zeroes Python

时间:2015-03-27 16:56:55      阅读:201      评论:0      收藏:0      [点我收藏+]

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) + ....

 

LeetCode Factorial Trailing Zeroes Python

原文:http://www.cnblogs.com/fyymonica/p/4372021.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!