The length of the factorial of 51nod 1130 N (approximate sterling), 51nod1130

Source: Internet
Author: User

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 }

 

 

 

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.