1350. How Many Digits In a Factorial
Print how many digits n! has when written in base 10. With n = 0 the answer
is 1, since 0! is 1.
n reaches 100000, where n! has hundreds of thousands of digits, so it cannot
be built. Even big-integer arithmetic would be needlessly slow at that size.
The digit count of a positive integer x is floor(log10(x)) + 1. Logarithms
turn products into sums, so:
log10(n!) = log10(1) + log10(2) + ... + log10(n)
and the answer is that sum, floored, plus one. A single pass, no large numbers anywhere.
Precision is the real subject here. Accumulating 100000 doubles loses low-order
bits, but the error stays far below 1 while the sum runs to about 456574, so the
floor is safe at this size. It would NOT be safe at n = 10^9, and the bound is
set where it is for that reason rather than arbitrarily.
Add the terms in increasing order, smallest first. Summing largest first loses more precision, which is a general rule for floating point worth carrying elsewhere.
Constraints - `0 ≤ n ≤ 100000`
Input
One line containing the integer n.
Output
Print how many digits n! has in base 10.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.