一:log10(n!)=log10(1*2*3…*n)=log10(1)+log10(2)+…+log10(n)
二:n! = sqrt(2*π*n) * ((n/e)^n) * (1 +1/(12*n) + 1/(288*n*n) + O(1/n^3))
π = acos(-1)=3.1415927;
e = exp(1)=2.718281828459;
兩邊對10取對數
忽略log10(1 + 1/(12*n) +1/(288*n*n) + O(1/n^3)) ≈ log10(1) = 0
得到公式
log10(n!) = log10(sqrt(2 * pi * n)) + n* log10(n / e)。
一:
[cpp] #include<stdio.h>
#include<math.h>
int main()
{
int i,n,t;
double temp=0;
scanf("%d",&t);
while(t--)
{
temp=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
temp+=log10(i);
printf("%d\n",(int)temp+1);
}
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
int i,n,t;
double temp=0;
scanf("%d",&t);
while(t--)
{
temp=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
temp+=log10(i);
printf("%d\n",(int)temp+1);
}
return 0;
}
二:
[cpp] #include"stdio.h"
#include"math.h"
int main()
{
int n;
double temp;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
temp=0.5*log10(2*3.1415927*n)+n*log10(n/2.718281828459);
printf("%d\n",(int)temp+1);
}
return 0;
}
#include"stdio.h"
#include"math.h"
int main()
{
int n;
double temp;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
temp=0.5*log10(2*3.1415927*n)+n*log10(n/2.718281828459);
printf("%d\n",(int)temp+1);
}
return 0;
}