斯特灵公式:斯特灵是一条用来取n阶乘近似值的数学公式
斯特灵公式介绍:
斯特灵公式是一条用来取n阶乘近似值的数学公式。
公式为:
或更精确的
斯特林公式可以用来估算某数的大小,结合lg可以估算某数的位数,或者可以估算某数的阶乘是另一个数的倍数。
下面来讲一个例题应用。
Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 40438 Accepted Submission(s): 19746
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 ≤ 10
7 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
Source
题意:
给你一个整数n,求n!的位数。利用斯特灵公式求解n!的位数:易知整数n的位数为[lgn]+1。.利用Stirling公式计算n!结果的 位数时,可以两边取对数,得:
log10(n!)= log10(2*n*Pi)/2+n*log10(n/e)
则答案为:
ans= log10(2*n*Pi)/2+n*log10(n/e) + 1
代码实现如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define pi acos(-1.0)
#define e 2.7182818285
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int ans;
ans=log10(2.0*pi*n)/2.0+n*log10(n/e)+1;
cout<<ans<<endl;
}
return 0;
}