Problem Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.
Output
The output contains the number of digits in the factorial of the integers appearing in the input.
Sample Input
2
10
20
Sample Output
7
19
题目大意:求一个整数的阶乘位数;
思路一 : N!=C(常数)*10^m (假如说5!=120=1.2*10^2)我们就是要把上面那个公式的 m求出来,前面那个常数C我们可以记为1,求出来m之后再加1就是阶乘的位数。
N!=C*10^m
两边同时取对数 lg N!=C*m=m(把C看成1);
所以 digit(阶乘位数)=(lg1+lg2+…+lgN)+1;
思路二:这个思路就是要运用到一个公式—-斯特林公式。
N!的位数就是[lg(N!)]+1=[lg(1)+lg(2)+…+lg(N)]+1
—=(int)ceil[(n*ln(n)-n+0.5*ln(2*n*π))/ln(10)]
/ceil是向上取整,[]符号为取整/
斯特林公式:
代码如下:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int t,m;
double k;
long long int n;
scanf("%d",&t);
getchar();
while(t--){
double k=0;
scanf("%lld",&n);
for(int i=1;i<=n;i++){
k+=log10(i);
}
m=(int)k;
printf("%d\n",m+1);
}
return 0;
}