star

搜索

RSS

RSS Link

计数器

143415

对于给定的N,求N!结果的位数

2010年8月01日 05:02 | Comments(606) | Category:C语言 | Tags:

根据斯特林公式:$$n!$$~$$\sqrt{2\pi n}(\frac{n}{e})^n$$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*calculate the number of digits in the factorial of the integer N.date:2010-07-31*/
#include <stdio.h>
#include <math.h>
#include <assert.h>
int main(void)
{
    const double PI = acos(-1.0);
    int T;
    while(1)
    {
        scanf("%d", &T);
        assert(T > 0 && T < (1<<30));
        printf("%d\n", (int)((T*log(T) - T + 0.5 * log(2*T*PI) ) / log(10)) + 1);
    }
    return 0;
}