# Leetcode 172. Factorial Trailing Zeroes

## 2. Solution

• Version 1
```class Solution:
def trailingZeroes(self, n):
result = 1
count = 0
for i in range(1, n + 1):
result *= i
s = str(result)
i = len(s) - 1
while s[i] == '0':
i -= 1
count += 1
return count```
• Version 2
```class Solution:
def trailingZeroes(self, n):
if n < 5:
return 0
result = 1
count = 0
exp = 16
for i in range(5, n + 1, 5):
result = result * i * exp
while result % 10 == 0:
count += 1
result //= 10
return count```
• Version 3
```class Solution:
def trailingZeroes(self, n):
count = 0
for i in range(5, n + 1, 5):
temp = i
while temp % 5 == 0:
count += 1
temp //= 5
return count```
• Version 4
```class Solution:
def trailingZeroes(self, n):
count = 0
while n:
n //= 5
count += n
return count```

## Reference

1. https://leetcode.com/problems/factorial-trailing-zeroes/

0 条评论

• ### Leetcode 172 Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your sol...

• ### leetcode 172 Factorial Trailing Zeroes

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

• ### LeetCode 172. Factorial Trailing Zeroes

题解：一个数的阶乘结果的末尾的0，根据分解质因数，只能是25得到的，所以把这个数的阶乘分解质因数，看有多少个25，2显然是比5多的，所以数一数有多少个5就可以了...

• ### 关关的刷题日记86 – Leetcode 172. Factorial Trailing Zeroes

关关的刷题日记86 – Leetcode 172. Factorial Trailing Zeroes 题目 Given an integer n, retur...

• ### 【leetcode系列】172. 阶乘后的零

我们需要求解这n个数字相乘的结果末尾有多少个0，由于题目要求log的复杂度，因此暴力求解是不行的。

• ### Leetcode: Factorial Trailing Zeroes

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

• ### 【Leetcode】【python】Factorial Trailing Zeroes

给定一个整数n，返回n!（n的阶乘）数字中的后缀0的个数。 注意：你的解法应该满足多项式时间复杂度。

• ### Q172 Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solut...