您的位置:首页 > 大数据 > 人工智能

Factorial Trailing Zeroes

2015-08-14 13:44 323 查看
Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

分解因子, 因子中出现 一对 (2,5)时, 最后结果会增加一个0.

25可以分解出5*5,此时(2,5),(4,5)都不会得到0。125会得到三个0.

所以要求的和是 sum(N/5^1, N/5^2, N/5^3...)。

public int trailingZeroes(int n) {
if (n < 0)
return -1;
int count = 0;
for(long i=5;n/i>=1;i*=5)
count+=n/i;
return count;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: