I have to calculate factorials of an arbitrarily large integers mod another arbitrarily large integer. i have considered Stirling's Approximation and Ramanujan’s factorial approximation. is it possible to get better estimates?
-
3If you are using modular arithmetic, analytic estimates might not be the most useful thing for you, depending on what exactly your problem is. Instead things like https://en.wikipedia.org/wiki/Wilson%27s_theorem might help. – Alex J Best Nov 22 '18 at 11:53
-
1There's the gamma function but I don't think it will help you much in contexts of modular arithmetic... – Prasun Biswas Nov 22 '18 at 11:57
-
lets say, for the time being i just need to estimate the factorial. what will the nearest approximation be? – Malik Hamza Murtaza Nov 22 '18 at 12:05
-
1Have a look at https://math.stackexchange.com/questions/676952/is-ramanujans-approximation-for-the-factorial-optimal-or-can-it-be-tweaked-a/677163#677163. For $n=20$, what I wrote gives $2432902008176665737$ to be compared to the exact $2432902008176640000$ – Claude Leibovici Nov 22 '18 at 12:32
-
Yes that's exactly what got me started on this path... that's a great answer. – Malik Hamza Murtaza Nov 22 '18 at 12:39
-
1I don't know if this could be of interest since the coefficients start to be huge. Using the same kind of expression with degree $2$ in numerator and degree $3$ in denominator, for $n=20$, I get $2432902008176639896$. If you want, I could post the numbers in an answer. – Claude Leibovici Nov 22 '18 at 13:05
-
that seems like a better approximation, please do. – Malik Hamza Murtaza Nov 22 '18 at 13:09
-
if i am not wrong totally, it is my understanding that as the factorial grows bigger and bigger, there are a certain number of trailing zeroes, if the percentage error can somehow be reduced to magnitude, wont we be able to just ignore the error? – Malik Hamza Murtaza Nov 22 '18 at 13:15
-
Also how did you come up with these numbers? could you please explain? – Malik Hamza Murtaza Nov 22 '18 at 13:27
-
1Comparing to high order expansion of Stirling formula. – Claude Leibovici Nov 22 '18 at 13:41
-
I wish i knew half of the math you know!!! – Malik Hamza Murtaza Nov 22 '18 at 13:43
-
Be sure I don't know much ! I am just old. Cheers. – Claude Leibovici Nov 22 '18 at 13:55
-
Let us continue this discussion in chat. – Malik Hamza Murtaza Nov 22 '18 at 14:00
2 Answers
Writing $$n!\approx\sqrt{\pi}\left(\frac{n}{e}\right)^n\root\LARGE{6}\of{8n^3+4n^2+n+\frac 1 {30}\color{red}{-}x(n)}$$ with $$x(n)=\frac{a_0+a_1n+a_2n^2}{b_0+b_1n+b_2n^2+b_3n^3}$$ the coefficients are $$\left( \begin{array}{cc} a_0 & 12521740840824081\\ a_1 & 132077016740516320 \\ a_2 & 261892615461486240 \\ b_0 &3339455095907419720 \\ b_1 & 7902477164268212400 \\ b_2 & 5812898776788230400 \end{array} \right)$$ A few values $$\left( \begin{array}{ccc} n & \text{approximation} & \text{exact} \\ 10 & 3628800 & 3628800 \\ 15 & 1307674368000 & 1307674368000 \\ 20 & 2432902008176639896 & 2432902008176640000 \\ 25 & 15511210043330985910414618 & 15511210043330985984000000 \\ 30 & 265252859812191058429178640362769 & 265252859812191058636308480000000 \end{array} \right)$$
Edit
Using ratios of polyomials (as done in this answer and the previous one) leads to incredibly huge coefficients. Thinking more about it, I thought that is would be better to just write $$x(n)=\sum_{k=1}^m \frac{a_k}{n^k}$$ and, when required, transform this expansion to the desired $[p,p+1]$ Padé approximant. The coefficients so obtained are listed below $$\left( \begin{array}{cc} k & a_k \\ 1 & \frac{11}{240} \\ 2 & -\frac{79}{3360} \\ 3 & -\frac{3539}{201600} \\ 4 & \frac{9511}{403200} \\ 5 & \frac{10051}{716800} \\ 6 & -\frac{233934691}{6386688000} \\ 7 & -\frac{3595113569}{178827264000} \\ 8 & \frac{403527851669}{4649508864000} \\ 9 & \frac{25622861661869}{557941063680000} \\ 10 & -\frac{30016604936501}{101443829760000} \\ 11 & -\frac{685661227463561}{4463528509440000} \\ 12 & \frac{109896661164737049961}{79673983893504000000} \end{array} \right)$$ For $n=25$, this would lead to $\color{blue}{1551121004333098598400000}5$.
For $n=30$ , this would lead to $\color{blue}{26525285981219105863630848}5359781$.
This seems to be significantly better.
- 289,558
Stirlings approximation is good ... especially if in the expression for $\ln(n!)$ you include the series in odd powers of 1/n. Are you aware of that series - Stirlings aporoximation is all-too-often given without it. The coefficients of it are not too compliated - Bernoulli numbers multiplied by simple factors. Ah yes! maybe it is troublesome for very large n then because of that - the way the Bernoulli numbers 'turn round'.
This might answer your question though.
- 1,001
- 6
- 12