Skip to main content
Back to problems
#172
Medium Algorithms

Factorial trailing zeroes

Math
46.2% acceptance
Jan 12, 2026
3510
1991
Given an integer n, return the number of trailing zeroes in n!. Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn trailing_zeroes(n: i32) -> i32 {
    let mut count = 0;
    let mut power_of_five = 5;
    
    while power_of_five <= n {
      count += n / power_of_five;
      power_of_five *= 5;
    }
    
    count
  }
}