We need to count how many trailing zeroes appear at the end of n!. A trailing zero is a zero at the rightmost end of a number. For example, 120 has one trailing zero, and 1000 has three.
One option is to compute n! and then count the zeroes at the end. But factorials grow fast. 20! is already over 2 billion, and 100! has 158 digits, far beyond what a 64-bit integer holds. Computing the full factorial is impractical for large n, so we need a mathematical shortcut.
A trailing zero is produced by a factor of 10, and 10 = 2 x 5. The number of trailing zeroes equals the number of times 10 divides into n!, which equals the number of (2, 5) pairs in the prime factorization of n!. There are always more factors of 2 than 5 in a factorial (every even number contributes a 2, but only every fifth number contributes a 5), so the number of (2, 5) pairs is limited by the number of factors of 5. Counting trailing zeroes reduces to counting factors of 5 in n!.
0 <= n <= 10^4. With n up to 10,000, an O(n) scan over every number is fast enough. The follow-up asks for O(log n), which a direct formula achieves.n can be 0. This is the edge case where 0! = 1, which has zero trailing zeroes. Both approaches below handle it without special casing, since their loops never execute when n is 0.The answer equals the total number of factors of 5 across the numbers 1 through n. We can find it directly: iterate through every number from 1 to n, count how many times 5 divides it, and sum those counts.
A number contributes more than one factor of 5 when it is divisible by a higher power of 5. In 10!, the numbers 5 and 10 each contribute one factor of 5, so the total is 2. In 25!, the number 25 contributes two factors (25 = 5 x 5), while 5, 10, 15, and 20 each contribute one, giving 6 total.
count = 0.i from 1 to n:i is divisible by 5, divide i by 5 and increment count.count.This scans every number from 1 to n, even though four out of five are not divisible by 5 at all. The next approach counts the factors of 5 across the whole range with a few divisions, giving O(log n) time.
Instead of inspecting each number, we count all factors of 5 in n! at once by grouping numbers according to which power of 5 divides them.
floor(n/5). Each contributes at least one factor of 5.floor(n/25). Each of these contributes a second factor of 5, which the first term has not yet counted.floor(n/125). Each contributes a third factor.A number like 25 is counted once by the floor(n/5) term and once more by the floor(n/25) term, so its two factors of 5 are both accounted for. This layering is why each higher-power term adds exactly the extra factors the lower terms missed. The total is:
floor(n/5) + floor(n/25) + floor(n/125) + floor(n/625) + ...
This is Legendre's formula for the power of a prime p in n!, specialized to p = 5.
Every second number is even, so factors of 2 are always more abundant than factors of 5. The count of (2, 5) pairs is limited by the scarcer factor, which is always 5.
count = 0.n >= 5:count.count.