3

To find the factorial of a number, $n$, you need to multiply $n$ by every number that comes before it.

For example, if $n = 4$, then $n! = 24$ since $4 \cdot 3 \cdot 2 \cdot 1 = 24$.

However, this method is very time consuming and, as $n$ gets larger, this method also become more difficult, so is there an easier method that I can use to find the factorial of any number?

anipalur
  • 540
CCS
  • 143
  • 4
    Stirling's approximation provides a very good approximation of $n!$ for large $n$, but I don't know of any method that gives you a precise answer that doesn't boil down to repeated multiplication. – Joe Jun 04 '21 at 22:24
  • 3
    You can use the recurrence relation $n!=n (n-1)!$. So $2!=2$, $3!=3(2!)=3(2)=6$, $4!=4(3!)=4(6)=24$, $5!=5(4!)=5(24)=120$ and so on. – Math101 Jun 04 '21 at 22:29
  • No one has found a significantly easier method for finding an exact value, no one has proved there is no significantly easier method. If there were an easier method, it would give an fast factorization algorithm, which I don't think anyone expects. Here's a good summary of algorithms for calculating factorials: http://www.luschny.de/math/factorial/FastFactorialFunctions.htm – Gerry Myerson Jun 05 '21 at 05:12
  • 1
    Take $2^{[\frac n2] + [\frac n4] + ..... + [\frac n{2^k}]}$ where $2^k \le n < 2^{k+1}$. Then multiple by $3^{[\frac n3]+ [\frac n9] + .... +[\frac n{3^j}]}$ where $3^j\le n < 3^{j+1}$ and so on for ever prime $\le p$. So for example $39! = 2^{19+9+4+2+1}\cdot 3^{13+4+1}\cdot 5^{7+1}\cdot 7^{5}\cdot 11^3\cdot 13^3 \cdot 17^2\cdot 19^2\cdot 23\cdot 29\cdot 31\cdot 37$. Okay, that actually makes things a lot worse but... it gives you ideas how to think about it. Works pretty good for small factorials. $11! = 2^{5+2+1}\cdot 5^{2}\cdot 3^{3+1}\cdot 7\cdot 11 = 6400\times 81\times 77$ – fleablood Jun 05 '21 at 05:14
  • See also https://cs.stackexchange.com/questions/14456/factorial-algorithm-more-efficient-than-naive-multiplication The question has also come up several times on StackOverflow. – Gerry Myerson Jun 05 '21 at 05:18
  • @GerryMyerson, can you please explain how an easier (or faster) method to calculate factorials would lead to a fast factorization algorithm? – user25406 Jun 01 '23 at 13:24
  • 1
    @user, given $n$, you could find $\gcd(n,m!)$ where $m$ is the integer part of $\sqrt n$. If you get $1$, then you know $n$ is prime. If you get some number $r$ with $1<r<n$, then you know $r$ is a proper factor of $n$, and you can apply the procedure to $n/r$. If you get $n$, then all the factors of $n$ are less than $\sqrt n$, and you can find $\gcd(n,s!)$ with $s$ near $\root4\of n$. You may have to do a few iterations, but it won't be long before you have the factorization of $n$. – Gerry Myerson Jun 02 '23 at 00:44

1 Answers1

3

To answer your question, there are several methods that can help calculate factorials faster, though most are either approximations or shortcuts for calculation. I will be detailing some of these methods below:

Method 1

As mentioned by Joe in the comments, Stirling's approximation is a good method to approximate the value of a large factorial, and by rewriting the factorial as a Gamma function, the following formula is obtained:

$$n! \sim \sqrt{2\pi n} \cdot \biggl(\frac{n}{e} \biggr)^n$$

$$\\$$

Method 2

Again, as mentioned by Math101 in the comments, we can make use of the property $n! = n(n - 1)!$ to quickly calculate a factorial given the value of a previous factorial.

For example, given that $5! = 120$ (or by calculating $5!$ by using the same method):

$$6! = 6 \cdot 5! = 6 \cdot 120 = 720$$

This method does not work as efficiently as Method 1, especially at larger values of $n$, but it may be useful in some ways.

$$\\$$

Method 3

By pairing up the numbers needed to calculate a factorial, a shortcut can be used to quickly calculate the factorial by using the commutative property ($xy = yx$).

For example:

$$8! = 8 \cdot 7 \cdot 6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 = (8 \cdot 1) \cdot (7 \cdot 2) \cdot (6 \cdot 3) \cdot (5 \cdot 4) = 8 \cdot 14 \cdot 18 \cdot 20 = 40320$$

The largest value is always paired up with the smallest value, the second-largest with the second-smallest, and so on.

Please have a read through the source for this method for more detail on how to use this method more efficiently.

Please note that there is no clear method to easily calculate large factorials; approximations can only bring you fairly close to the true value.


I hope this helps! I'm still new to MSE, so I would really appreciate any and all feedback. Thank you!

anipalur
  • 540
  • 1
    +1.....Let $[x]$ denote the largest integer not exceeding $x.$ If $p$ is prime and $p\le n$ then the largest power of $p$ to divide $n!$ is $p^B$ where $B= \sum_{j\in \Bbb N}[n/p^j].$ (Only finitely many terms in the summation are non-$0$.) E.g. $20!=(2^{18})(3^8)(5^4)(7^2)(11)(13)(17)(19).$ – DanielWainfleet Jun 05 '21 at 05:14