N! Number of digits, N! Number of digits
1) Description:
N! (The factorial of N) is a very large number. The formula is: N! = N * (N-1) * (N-2) *... * 2*1 ). Now you need to know N! The number of digits in decimal format.
Input: Enter 1 positive integer N in each row. 0 <N <1000000
Output: For each N, output N! In decimal format.
Input: 320001000000
Output: 130271 5565709
2) algorithm analysis:
For any given positive integer a, assuming 10 ^ (x-1) <= a <10 ^ x, then it is clear that the number of digits of a is x, because log10 (10 ^ (x-1) <= log10 (a) <(log10 (10 ^ x) is the X-1 <= log10 (a) <x then (int) log10 (a) = X-1, that is, (int) log10 (a) + 1 = x that is, the number of digits of a is (int) log10 (a) + 1, we know that the number of digits of a positive integer a is equal to (int) log10 (A) + 1. Now we want to calculate the number of digits of the factorial of n: Suppose a = n! = 1*2*3 *...... * n, so what we need is (int) log10 (A) + 1, while log10 (A) = log10 (1*2*3 *...... n) (according to log10 (a * B) = log10 (a) + log10 (B) = log10 (1) + log10 (2) + log10 (3) + ...... + log10 (n) so the number of digits of the n factorial is equal to (int) (log10 (1) + log10 (2) + log10 (3) + ...... + log10 (n) + 13) source code: # include <stdio. h> # include <math. h>
Int main ()
{
Int n, I;
Double;
While (scanf ("% d", & n )! = EOF)
{
A = 0;
For (I = 1; I <= n; I ++)
{
A = a + (log10 (I ));
}
Printf ("% d \ n", (int) a + 1 );
}
}