HDU 1018 to find the number of digits of factorial of large numbers

Question meaning: give you n (1 ≤ n ≤ 10^7 ), find n! The median
method: a factorial method begins with a large number of requirements, the result of a timeout, this method is not seeking so much 10 ^ 7 factorial, Baidu a moment later found to have a direct demand equation

int result[4000];
int h;//最高位
int factorial(int num){
    
    
	memset(result,0,sizeof(result));
	h = 1;
	result[0] = 1;
	for(int i=1;i<=num;i++){
    
    
		int res = 0;
		for(int j=0;j<h;j++){
    
    
			int b = result[j]*i + res;
			result[j] = b%10;
			res = b/10;
		}
		while(res){
    
    
			result[h++] = res%10;
			res /= 10;
		}
	}
	return h;
}

Method 2: Stirling formula
Insert picture description here
res = (int)(log10(sqrt(2 PI n))+n*log10(n/e))+1

int Stirling(int n){
    
    //斯特拉公式 
	double PI = acos(-1);
	double e = exp(1);
	return (int)(log10(sqrt(2*PI*n))+n*log10(n/e))+1;
}

Method three: res = log10(N!)+1

int digit(int n){
    
    
	double sum = 0;
	for(int i=1;i<=n;i++){
    
    
		sum += log10(i);
	}
	return (int)(sum+1);
}

Guess you like

Origin blog.csdn.net/qq_45880043/article/details/112689540