Algorithm
Problem Name: 172. Factorial Trailing Zeroes
Given an integer n
, return the number of trailing zeroes in n!
.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1
.
Example 1:
Input: n = 3 Output: 0 Explanation: 3! = 6, no trailing zero.
Example 2:
Input: n = 5 Output: 1 Explanation: 5! = 120, one trailing zero.
Example 3:
Input: n = 0 Output: 0
Constraints:
0 <= n <= 104
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int trailingZeroes(int n) {
int c = 0;
while (n) {
n /= 5;
c += n;
}
return c;
}
int main() {
printf("%d\n", trailingZeroes(1808548329));
return 0;
}
Copy The Code &
Try With Live Editor
Input
#2 Code Example with Javascript Programming
Code -
Javascript Programming
const trailingZeroes = function(n) {
let result = 0;
for(let i = 5; Math.floor(n/i) > 0; i *= 5){
result += (Math.floor(n/i));
}
return result;
};
Copy The Code &
Try With Live Editor
Input
#3 Code Example with Python Programming
Code -
Python Programming
class Solution:
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
return 0 if n == 0 else n // 5 + self.trailingZeroes(n // 5)
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with C# Programming
Code -
C# Programming
namespace LeetCode
{
public class _0172_FactorialTrailingZeroes
{
public int TrailingZeroes(int n)
{
var count = 0;
while (n > 0)
{
n /= 5;
count += n;
}
return count;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output