A trailing zero is made of factor 2 and factor 5 n! = 2 ^ m * 5 ^ n * (other factors) the count of trailing zero is min(m, n). because a 2 and a 5 generates a zero. the work is just counting 2 and 5 ...