If you’ve ever dealt with factorials, you may have felt like yelling, and not because of the exclamation point. Evaluating something like 20! by hand will feel like forever, and you might make a mistake along the way.
You might ask if there a nice formula to evaluate all this multiplication quickly, since there is a nice formula for the sum of all these numbers.
Or more generally:
The reason why this nice addition formula works can be demonstrated with a little geometry. A sum from 1 to N can be arranged like stacked blocks in a triangle (here we’ll go up to 5).
Then the area of the triangle can be found by taking a twin triangle and fitting it together into a rectangle, then divide by 2 to get the original triangle area (to arrive at 15).
(These sums are called triangular numbers.)
So what about a nice formula for factorials? Well, one was discovered by a happy fellow named Adrien-Marie Legendre (pictured below). It’s called Legendre’s Formula.
This formula allows you to express factorials as a product of prime numbers. For example:
This may seem ugly at first, but it is better than chugging through the multiplication to arrive at the long, boring answer of 3628800. Legendre’s formula is faster, and it gives you more information about the number. In any case, you can always evaluate to get 3628800 later. The only caveat to finding N! is knowing all the prime numbers less than or equal to N.
The reason we need the primes is because we want to find the prime factors of N!
Integers are either prime or composite. Composite integers can be broken up into prime factors. For example:
Since a factorial number is definitely composite, it can be broken down into prime factors as well.
Let’s use 10! as our example:
We want to find all the prime factors of 10! First, we need to know which prime numbers are factors of 10! To do that is easy, find all the primes less than 10, which are:
No other primes are needed, since we see all the numbers being multiplied in 10! are 10 and lower. So that is pretty convenient. We know from this that . So now we want to find and .
We will start with 2. How many factors of 2 are there in 10! We can start by underlining the multiples of 2 in the factorial:
There are 5 multiples of 2. So make a list of 1 to 5 and find the multiples of 2 in that list:
There are 2 multiples of 2 in this list. So make a list of 1 to 2 and find multiples of 2 in that list:
There is 1 multiple of 2 in this list, and 1 is less than 2, so we can stop.
We can then sum these numbers from each list:
There are 8 factors of 2 in 10!
You might have noticed that this sort of recurring list pattern works because some of the numbers between 1 and 10 have more than 1 factor of 2. For example: 4 has 2, and 8 has 3.
So you could have counted factors of 2 more directly:
Both methods are equivalent, but the previous one requires less thinking in my opinion.
The same is done with 3, 5 and 7. For 3:
Count the underlines to see that there are 4 factors of 3 in 10!
There are 2 factors of 5 in 10!
Finally, 1 factor of 7 in 10!
So we can then write:
So if this is Legendre’s Formula, there should be a formula, right? What I showed above seems more like an algorithm, but it is equivalent to:
Where the brackets are the floor function, which rounds a number downward to the next integer. For example: .
The resulting is the exponent for the prime of interest .
So going back to the 10! example, for :
Then do the same for and , to arrive at once again.
As a side note: even though this sum technically goes to infinity, any term where will be pushed to zero by the floor function.
Now that you know how to quickly evaluate a factorial, try evaluating 20!