Zeros in factorial calculator
Working (Legendre's formula)
| Term | Value |
|---|---|
| Sum |
Every trailing zero in n! comes from a factor of 10, which comes from a pairing of 2 and 5 - and since factors of 2 always outnumber factors of 5, counting factors of 5 is enough. This never computes n! itself, which for anything past about 20! already has more digits than a calculator can hold exactly.
The shortcut, not the brute force
100! has 158 digits. Computing it outright just to count how many of those digits happen to be trailing zeros works, but it is solving a much harder problem than the one being asked. This uses the standard number-theory shortcut instead: every trailing zero in n! comes from a factor of 10, which comes from a pairing of 2 and 5 in n!'s prime factorisation - and since factors of 2 are always far more plentiful than factors of 5 in any factorial, the count of trailing zeros is exactly the count of factors of 5, found with Legendre's formula: floor(n/5) + floor(n/25) + floor(n/125) + ... until the terms reach zero.
Why this works
Every second number contributes a factor of 2, but only every fifth number contributes a factor of 5 - so 5s are always the bottleneck. Counting them directly, rather than multiplying out the whole factorial, turns an operation on a number with hundreds of digits into a handful of divisions.
What it shows
The trailing zero count, plus each term of the sum (floor(n/5), floor(n/25), ...) so the working is visible and checkable rather than just the final number.
Related tools
See the nth root calculator for another calculator built around a case most tools handle by brute force.
Rate this tool
Was this tool useful? Your feedback helps us improve it.