### Factorial Trailing Zeroes

Total Accepted: 42370 Total Submissions: 139281 Difficulty: Easy

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.

Subscribe to see which companies asked this question

1.求n!中后缀0的个数。

2.n!后缀0的个数由(2^i)*(5^j)得来。

3.因此，要求min(i,j)。

4.j<=i，直观的来看i是逢2进1，j是逢5进1。因此，min(i,j)==j。

5.即求n!能被5整除的个数，即k=n/5 + n/25 + n/125 + ... + n/(2^j)，其中j<=n。

class Solution {
public:
int trailingZeroes(int n) {
int x = 5;
int ret = 0;
while(x <= n) {
ret += n/x;
x *= 5;
}
return ret;
}
};

class Solution {
public:
int trailingZeroes(int n) {
int ret = 0;
while(n) {
ret += n/5;
n /= 5;
}
return ret;
}
};

#### leetcode 172: Factorial Trailing Zeroes

2014-12-30 09:54:52

#### 172. Factorial Trailing Zeroes [easy] (Python)

2016-06-05 17:40:21

#### LeetCode(172) Factorial Trailing Zeroes

2015-01-02 03:46:08

#### 【LeetCode-面试算法经典-Java实现】【172-Factorial Trailing Zeroes（阶乘尾后0的数目）】

2015-08-23 06:43:47

#### Leetcode: Factorial Trailing Zeroes

2015-01-02 21:23:32

#### 172 Factorial Trailing Zeroes 过了。英文真不好读啊。

2014-12-30 18:52:33

#### 【LeetCode】Factorial Trailing Zeroes 阶乘尾部0的个数

2015-04-23 15:53:00

#### LeetCode：求阶乘后面的0的个数（Factorial Trailing Zeroes）

2016-04-03 15:54:48

#### LeetCode Factorial Trailing Zeroes数学方法详解

2016-08-05 15:14:24

#### LeetCode -- Factorial Trailing Zeroes

2015-10-04 16:19:37

## 不良信息举报

LeetCode:Factorial Trailing Zeroes