Can you know how big a factorial is before calculating it?

I use GMP to calculate very large factorials (e.g. 234234!). Is there any way to find out before what calculation how many digits will be a long result (or maybe)?

+3
source share
6 answers

The factorial locarithm can be used to calculate the number of digits that the factorial will take:

logn!

This can be easily translated into an algorithmic form:

//Pseudo-code
function factorialDigits (n) 
  var result = 0;

  for(i = 1; i<=n; i++)
    result += log10(n);

  return result;
+7
source

You can convert the Stirling approximation formula using simple logarithmic mathematics to get the number of digits:

n!         ~ sqr(2*pi*n) * (n/e)^n
log10(n!)  ~ log10(2*pi*n)/2 + n*log10(n/e)

, .

+12

n!

alt text

. Wikipedia .

+3

nlog(n) - n + log(n(1 + 4n(1 + 2n)))/6 + log(pi)/2

. " " @http://en.wikipedia.org/wiki/Factorial

+2

, .

: n! ~ = sqrt (2 * Pin) (n/e) ^ n. , 1 + log (n!)/Log (10).

+1

, , ... - LUT, N . 4 4 , 1000 000 8 .

+1

Source: https://habr.com/ru/post/1712363/


All Articles