High precision factorial and number of bits

High precision factorial

What is high precision factorial?

We know that the factorial changes are very large,
even the factorial of 20 is very large, which is 2432902008176640000
long long;
how to calculate it?

Then we can do it with the help of an array

#include<bits/stdc++.h>
using namespace std;
#define IN -1e6
#define INT 1e6
const int maxn=1e6;
int res=1;
typedef long long ll;
int a[maxn];

int main()
{
    
    
	a[0]=1;
  int n,jin=0,w=1,s; ///jin表示进位 , w代表位数
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    
    
  	jin=0;
  	   for(int j=0;j<w;j++)
  	   {
    
    
  	   	   s=a[j]*i+jin;
  	   	   jin=s/10;
  	   	   a[j]=s%10;
	   }
	   
	   while(jin)
	   {
    
    
	   	 a[w++]=jin%10;
	   	 jin/=10;
	   }
  }
  
  for(int i=w-1;i>=0;i--)
  {
    
    
  	cout<<a[i];
  }
   return 0;
}

In this case, a relatively large factorial can be obtained, but if it exceeds 10,000, it may be t;

What about the number of digits of factorial after the ball is 10,000?

We have a Stirling formula that can find a very large factorial digit, only o(1);

Insert picture description here
Insert picture description here
Then

const double e = 2.7182818284590452353602875;

const double PI = 3.1415926535897932384626434;//越精确越好
int main() {
    
    
	int n, N;
	cin >> n;
	while (n--) {
    
    
		cin >> N;
		cout << int(0.5 * log10(2 * PI * N) + N * log10(N / e)) + 1 << endl;
	}

From this we can get a large number of factorial bits;

Guess you like

Origin blog.csdn.net/qq_52172364/article/details/112133131