Factorial calculator
Welcome to the factorial calculator: a tool that calculates the factorial of any number from 0 to 170. On top of calculating, e.g., the 0-factorial or 5-factorial, we will also show you how to use the exclamation point in maths, provide information about the n-factorial formula, and the applications it can have. By the end of this, we will have an answer to "what is a factorial?" using the factorial definition. And finally, we will dive into the maths behind it to find how we can include more than just positive integers by using the gamma function.
Exclamation point in maths: what is a factorial?
When you saw an exclamation point in maths for the first time, you probably got shocked or even thought that there was some kind of mistake or typo. But the reality is different: this exclamation point in maths is called the factorial or n-factorial. The factorial is a reasonably unknown operator that can, in fact, be viewed more as an abbreviation than an actual operator, at least at the beginning.
It is important not to confuse the factorial with the prime factorization of a number, which is a way of obtaining the prime numbers that, when multiplied together, give your number. Prime factorization has its uses in maths and is arguably more well-known than the n-factorial — part of the reason for its popularity is its usefulness when calculating the Greatest Common Factor (GCF) and the Least Common Multiple (LCM).
To understand what the factorial does or means, we should start with an example. We could choose any number n and calculate its n-factorial value, but it's best to choose a relatively small number, so let's use 5-factorial:
5! = 5 × 4 × 3 × 2 × 1 = 120
From this example, you can see that it is not rocket science, and you might even take a guess at the factorial formula. You can also understand why this exclamation point in maths can be regarded as an abbreviation, since it is not a new operation but rather a collection of multiplications. In short, and somewhat informally, we can define the factorial as the multiplication of all the positive integers smaller than and equal to the given number.
Getting mathematical: factorial definition and factorial formula
Now that we have an intuitive understanding, we can write the factorial formula in a compact way. For any non-negative integer n:
n! = n × (n − 1) × (n − 2) × … × 3 × 2 × 1
The factorial also has a neat recursive definition, which is exactly how a computer or our calculator builds up the answer step by step:
n! = n × (n − 1)! with 0! = 1
In words: the factorial of n equals n times the factorial of the previous number.
Basic values of factorials
Here is a quick reference table of the first factorials so you can see how astonishingly fast they grow:
| n | n! |
|---|---|
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 24 |
| 5 | 120 |
| 6 | 720 |
| 7 | 5,040 |
| 10 | 3,628,800 |
| 15 | 1,307,674,368,000 |
| 20 | 2,432,902,008,176,640,000 |
Because factorials grow so quickly, 170! is roughly 7.26 × 10306 — close to the largest number an ordinary computer can store as a floating-point value, which is exactly why our factorial calculator stops at 170.
0-factorial and why it is so special
One of the most common questions is: why does 0! = 1? At first glance, multiplying "no numbers together" giving 1 feels strange. There are two good reasons:
- The recursive rule. We know that 1! = 1 × 0!. For this to equal 1, we must have 0! = 1.
- The empty product. By convention, the product of no factors at all is defined to be 1 (just as the sum of no numbers is 0). This keeps the counting formulas of combinatorics — permutations and combinations — working smoothly.
So 0! = 1 is not an arbitrary choice: it is the only value that keeps all the maths consistent.
Gamma function: factorials of non-integer values
The plain factorial is only defined for non-negative integers. But mathematicians love to extend definitions, and the factorial is no exception. The gamma function, written Γ, is a smooth function that agrees with the factorial on the integers and fills in every value in between:
x! = Γ(x + 1)
For example, using the gamma function you can find that (1/2)! = √π / 2 ≈ 0.8862. Switch the calculator above to "Factorial of a real number (gamma function)" mode to compute the factorial of decimals such as 4.5 or 0.5. The gamma function is defined for all real (and even complex) numbers except the negative integers, where it shoots off to infinity.
Real-world applications
Factorials appear far more often than you might expect:
- Combinatorics. The number of ways to arrange n distinct objects in a row (permutations) is exactly n!. Combinations and permutations formulas are built entirely from factorials.
- Probability. Card shuffles, lottery odds, and the binomial distribution all rely on factorials.
- Calculus and series. The Taylor series of functions like ex, sin(x), and cos(x) divide each term by a factorial.
- Computer science. Analysing the complexity of algorithms that examine every possible ordering (like brute-force solutions to the travelling salesman problem) leads straight to factorials.
How to calculate factorial in programming languages?
Factorials are a classic first exercise in programming because they can be written either with a loop or recursively. Here are a few short examples:
Python:
import math
math.factorial(5) # -> 120
# or manually:
def factorial(n):
result = 1
for i in range(2, n + 1):
result *= i
return result
JavaScript:
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
factorial(5); // 120
C / C++:
long long factorial(int n) {
long long result = 1;
for (int i = 2; i <= n; ++i) result *= i;
return result;
}
Note that in most languages the built-in integer types overflow very quickly — a 64-bit integer can only hold up to 20!. To display the exact digits of larger factorials (like 100! with its 158 digits), our calculator uses arbitrary-precision arithmetic, so every result is shown in full.
A note on units, systems of measurement, and currencies
The factorial is a pure counting operation: it acts on a plain integer and returns a plain integer. Because of this, the result does not depend on any system of measurement — it is identical whether you work in the US customary / imperial system or the metric (SI) system, and there is no monetary quantity involved (so no conversion between the US dollar, the Russian ruble, the euro, or any other currency is required). Whenever factorials feed into a real-world calculation involving money or physical units, those units are attached at that later stage, not to the factorial itself.