The length of the factorial of 51nod 1130 N (approximate sterling), 51nod1130
Input N to calculate the length of the factorial in the 10 hexadecimal notation of N. Example 6! = 720, with a length of 3. Input
Row 1st: A number T, indicating the number of subsequent input tests. (1 <= T <= 1000) 2nd-T + 1 row: 1 in each row N. (1 <= N <= 10 ^ 9)
Output
T rows in total, and the length of the corresponding factorial is output.
Input example
3456
Output example
233
The Sterling formula is used to obtainMathematical formula of n factorial Approximation. Generally, when n is large, the calculation of n factorial is large.
Therefore, the string formula is very useful. Even when n is very small, the value of the string formula is very accurate.
Formula:
That is to say, for integers that are big enoughNThe two numbers are approximate values. More precisely:
Or, take log () + 1 and get it.
Simply put
N! = N * (n-1) * (n-2) *... * 3*2*1
∴ Lg (n !) = Lg (n) + lg (n-1) + lg (n-2) +... + lg (3) + lg (2) + lg (1 );
The number of Stirling is N! Number of digits: log10 (n !) = 0.5 * log10 (2 * PI * n) + n * log10 (n/e );
1 #include <stdio.h> 2 #include <math.h> 3 #define e 2.718281828459 4 #define pi 3.1415926 5 int main(){ 6 int n, t; 7 long long sum; 8 scanf("%d",&t); 9 while(t--){10 scanf("%d",&n);11 sum=1+0.5*log10(2*pi*n)+n*log10(n/e);12 printf("%I64d\n",sum);13 }14 return 0;15 }