← Math

Trailing Zeroes

Easy
Python
def trailing_zeroes(n):
    count=0
    while n>=5: n//=5; count+=n
    return count
Java
public int trailingZeroes(int n){
    int count=0;
    while(n>=5){n/=5;count+=n;}
    return count;
}

Key Insight

Count factors of 5. Nearly identical. //= vs /=.

Python → Java Differences

  • //= vs /=
  • Nearly identical solutions
  • Count factors of 5
Python
def trailing_zeroes(n):
    count=0
    while n>=5: n//=5; count+=n
    return count
Java
public int trailingZeroes(int n){
    int count=0;
    while(n>=5){n/=5;count+=n;}
    return count;
}

Algorithm Steps

1. Count factors of 5
2. Each 25 contributes twice