Sterling [striling] formula (factorial (N !) Number of digits)

Source: Internet
Author: User
Tags natural logarithm

 /*  For example, 1000 factorial digits:
Log10 (1) + log10 (2) + · + log10 (1000) rounded up and added 1
*/
# Include <stdio. h>
# Include <math. h>
Int Main ()
{
Int N, I, T;
Double D;
Scanf ( " % D " , & T );
While (T --)
{
While (Scanf ( " % D " , & N ))
{
D = 0 ;
For (I = 1 ; I <= N; I ++)
D + = log10 (I );
Printf ( " % D \ n " ,( Int ) D + 1 );
}
}
Return 0 ;
}
/*
Or

# Include <stdio. h>
# Include <math. h>
# Define PI 3.14159265
Int main ()

{
Int Len, N;
While (scanf ("% d", & N )! = EOF)
{
If (n = 1)
Len = 1;
Else
Len = (INT) Ceil (n * log (N)-N + Log (2 * n * PI)/2)/log (10 )); /// calculate the upper bound of Ceil, that is, the minimum integer not smaller than a value.


// String formula lnn! = Nlnn-N + 0.5 * ln (2 * n * PI)
// While the number of digits of the n factorial is equal:
// Log10 (N !) Add 1 after rounding
// Log10 (N !) = Lnn! /Ln (10)

// Ceil is the upper bound, that is, the smallest integer of N.
// Log is the natural logarithm.
Printf ("% d \ n", Len );
}
Return 0;
}

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.