Factorial Calculator
Calculate the factorial (n!) of any non-negative integer with step-by-step breakdown. Factorials grow extremely fast — maximum input is 170.
Quick Examples
Introduction to Factorials
When calculating how many ways you can arrange objects — books on a shelf, digits in a password, teams in a tournament bracket, cards in a shuffled deck — you quickly encounter factorials. A deck of 52 cards can be arranged in 52! different ways, a number so large it exceeds the atoms in the observable universe. This is the power of factorial growth.
A factorial calculator instantly computes these enormous values without manual multiplication. Beyond basic arithmetic, factorials underpin permutation counting, combination formulas, probability theory, statistical distributions, and computer science algorithms. Every time you need to count arrangements or selections, factorials appear.
Students learning combinatorics, programmers implementing sorting algorithms, statisticians calculating probability distributions, scientists analyzing molecular arrangements, and game designers building randomization systems all rely on factorial calculations. This page covers: what factorials mean, their formula and notation, multiple calculation methods (direct, recursive, iterative, approximation), real-world applications, and programming implementations.
What is Factorial?
A factorial is the product of all positive integers from 1 up to a given number n. It answers the question: if you multiply every whole number from 1 to n together, what do you get?
Formally: n! = n x (n-1) x (n-2) x ... x 2 x 1
The exclamation mark symbol is called factorial notation. It represents repeated multiplication of consecutive integers, not exponentiation.
Basic Examples
- 5! = 5 x 4 x 3 x 2 x 1 = 120
- 4! = 4 x 3 x 2 x 1 = 24
- 3! = 3 x 2 x 1 = 6
- 2! = 2 x 1 = 2
- 1! = 1
- 0! = 1 (special case)
Why 0! = 1
This is not arbitrary — there is exactly one way to arrange zero objects: the empty arrangement. In combinatorics, if you have nothing to arrange, there is precisely one possibility (do nothing). This definition makes formulas consistent. For combinations to work when choosing all items from a set, we need 0! = 1.
Key Properties
| Property | Formula | Example |
|---|---|---|
| Recursive definition | n! = n x (n-1)! | 5! = 5 x 4! = 5 x 24 = 120 |
| Base cases | 0! = 1, 1! = 1 | Foundation of all factorial calculation |
| Only non-negative integers | Factorial undefined for negatives | (-1)! has no meaning in standard math |
| Extremely fast growth | n! grows faster than n^k for any k | 10! = 3.6 million, 15! = 1.3 trillion |
Factorial Values Reference Table
| n | n! | Digits |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 6 | 1 |
| 4 | 24 | 2 |
| 5 | 120 | 3 |
| 6 | 720 | 3 |
| 7 | 5,040 | 4 |
| 8 | 40,320 | 5 |
| 9 | 362,880 | 6 |
| 10 | 3,628,800 | 7 |
| 12 | 479,001,600 | 9 |
| 15 | 1.307 x 10^12 | 13 |
| 20 | 2.43 x 10^18 | 19 |
| 50 | 3.04 x 10^64 | 65 |
| 100 | 9.33 x 10^157 | 158 |
How Does the Factorial Calculator Work?
This calculator accepts a single non-negative integer and computes the product of all integers from 1 to n using efficient algorithms.
Input Requirements
- Input: A non-negative integer n (0, 1, 2, 3, ...)
- Maximum: 170 for floating-point display (larger values overflow)
- Negative numbers: Undefined — calculator rejects negative inputs
Internal Computation
- n = 0: Returns 1 immediately (base case)
- n > 0: Iteratively multiplies numbers from 1 to n
- Large n: Uses floating-point for display (scientific notation above 15!)
Output
- Value: The factorial n!
- Formula display: Shows the multiplication expression
- Step-by-step: Optional breakdown of intermediate values
Factorial Formula and Notation
Standard Notation
The exclamation mark follows the number and indicates factorial operation.
Expanded Formula
Recursive Formula
With base cases: 0! = 1 and 1! = 1
Closed Form (Small Values)
- 2! = 2
- 3! = 6
- 4! = 24
- 5! = 120
- 6! = 720
Growth Behavior
Factorial grows faster than any exponential n^k for fixed k. This makes it essential for complexity analysis in computer science. Algorithms with factorial complexity are impractical beyond very small inputs.
Method 1: Direct Multiplication Method
Definition
Multiply all integers from 1 to n sequentially. This is the most intuitive method for beginners and works well for small values.
Step-by-Step Process
- Start with the number 1.
- Multiply by 2, store the result.
- Multiply by 3, store the result.
- Continue multiplying by each successive integer up to n.
- The final product is n!
Worked Examples
Calculate 5!
- Start: 1
- 1 x 2 = 2
- 2 x 3 = 6
- 6 x 4 = 24
- 24 x 5 = 120
- Result: 5! = 120
Calculate 7!
- 1 x 2 = 2
- 2 x 3 = 6
- 6 x 4 = 24
- 24 x 5 = 120
- 120 x 6 = 720
- 720 x 7 = 5040
- Result: 7! = 5040
Strengths and Limitations
Strength: Simplest and most intuitive. Perfect for mental math with small numbers.
Limitation: Slow for large numbers. Each multiplication adds a step, making it O(n) time.
Method 2: Recursive Method
Definition
Factorial is defined in terms of itself: n! = n x (n-1)! This recursive definition is foundational for programming and mathematical proof.
Step-by-Step Process
- Express n! as n x (n-1)!
- Express (n-1)! as (n-1) x (n-2)!
- Continue until reaching 1! or 0!
- Multiply all values back up the chain.
Worked Example: Calculate 5! Using Recursion
5! = 5 x 4! 4! = 4 x 3! 3! = 3 x 2! 2! = 2 x 1! 1! = 1 (base case) Now multiply back: 2! = 2 x 1 = 2 3! = 3 x 2 = 6 4! = 4 x 6 = 24 5! = 5 x 24 = 120
Recursive Tree Visualization
Each factorial expands until hitting the base case, then values propagate back up. This teaches the concept of recursion before learning programming.
Strengths
Foundation for programming. Natural mathematical definition. Elegant and easy to understand conceptually.
Limitations
Recursion depth limits practical use. Each recursive call uses memory. In programming, deep recursion can cause stack overflow.
Method 3: Iterative Loop Method
Definition
Use a loop structure to multiply consecutive integers. This is the standard implementation in calculators and most software.
Step-by-Step Process
- Initialize result = 1.
- For each integer i from 1 to n:
- Update result = result x i.
- Return final result.
Worked Example: Calculate 6!
| Step | i | Calculation | Result |
|---|---|---|---|
| Initialize | — | result = 1 | 1 |
| 1 | 1 | 1 x 1 | 1 |
| 2 | 2 | 1 x 2 | 2 |
| 3 | 3 | 2 x 3 | 6 |
| 4 | 4 | 6 x 4 | 24 |
| 5 | 5 | 24 x 5 | 120 |
| 6 | 6 | 120 x 6 | 720 |
Result: 6! = 720
Strengths
Efficient and practical. O(n) time complexity. Constant memory usage. No recursion depth limitations.
Method 4: Stirling's Approximation (Advanced)
Definition
For very large values where exact computation is impractical, Stirling's formula provides an approximation.
Formula
Purpose
Estimates factorial when exact values are too large to compute or store. Used in probability theory, statistical mechanics, and algorithm complexity analysis.
Example: Estimate 10!
10! ~ sqrt(2pi x 10) x (10/e)^10 ~ sqrt(62.83) x (3.679)^10 ~ 7.93 x 4541 ~ 3.60 x 10^6
Actual: 10! = 3,628,800. Approximation is within 1%.
Use Cases
- Probability bounds on large sample sizes
- Complexity analysis of algorithms
- Statistical mechanics approximations
- Quick mental estimates for large factorials
Comparison of All Four Methods
| Method | Best For | Speed | Accuracy | Complexity |
|---|---|---|---|---|
| Direct Multiplication | Beginners, small numbers | Slow | Exact | Low |
| Recursive | Learning, proofs | Medium | Exact | Medium |
| Iterative Loop | Programming, standard use | Fast | Exact | Low |
| Stirling's Approximation | Large numbers, estimates | Very Fast | Approximate | High |
Decision guide: Learning basics? Direct multiplication. Programming? Iterative loop. Studying algorithms? Recursive concept. Estimating large values? Stirling's approximation.
Factorial Growth Pattern
Factorial grows faster than any polynomial or exponential function with fixed base. Understanding this growth explains why factorial complexity is considered impractical.
Growth Comparison
| n | n! | 2^n | n^2 |
|---|---|---|---|
| 5 | 120 | 32 | 25 |
| 10 | 3,628,800 | 1,024 | 100 |
| 15 | 1.3 x 10^12 | 32,768 | 225 |
| 20 | 2.4 x 10^18 | 1,048,576 | 400 |
While 2^20 is about 1 million, 20! is about 2.4 quintillion. Factorial outpaces exponential dramatically.
Why This Matters
Computing all permutations of 20 items requires 20! operations — impossible for any computer. This is why brute-force permutation algorithms are limited to small inputs.
Factorial in Permutations
The most direct application of factorial: counting arrangements.
Core Relationship
Examples
Books on a shelf: 5 books can be arranged in 5! = 120 different orders.
Password characters: A 4-character password using digits 1-4 without repetition has 4! = 24 possibilities.
Marathon finish order: 10 runners finishing in all possible orders = 10! = 3,628,800 outcomes.
Partial Permutations
Choosing r items from n and arranging them: P(n,r) = n!/(n-r)!
Example: Arranging 3 runners from 10 in order: P(10,3) = 10!/7! = 10 x 9 x 8 = 720 ways.
Factorial in Probability
Combinations Formula
Counts ways to choose r items from n without regard to order.
Example: Lottery Probability
Lottery drawing 6 numbers from 49:
- Total combinations: C(49,6) = 49!/(6! x 43!)
- C(49,6) = 13,983,816 possible tickets
- Odds of winning: 1 in 13,983,816
Binomial Distribution
The binomial coefficient appears in probability mass functions:
Factorial in Programming
| Language | Function | Notes |
|---|---|---|
| Python | math.factorial(n) | Built-in, handles up to large values |
| JavaScript | Custom function | function f(n){return n<=1?1:n*f(n-1)} |
| Java | BigInteger | Use BigInteger for large values |
| C++ | Custom or boost | std::int64_t overflows at 21! |
| Excel | =FACT(n) | Built-in worksheet function |
| R | factorial(n) | Built-in, returns double |
Overflow Warning
32-bit integers overflow at 12! (479 million). 64-bit integers overflow at 20! (about 2.4 quintillion, but overflow actually occurs at 21! due to signed integer limits). For larger values, use arbitrary precision libraries:
- Python: Built-in arbitrary precision
- Java: java.math.BigInteger
- JavaScript: BigInt type (ES2020+)
- C++: Boost.Multiprecision library
Implementation Example (JavaScript)
// Iterative (recommended)
function factorial(n) {
if (n < 0) return undefined;
if (n === 0 || n === 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
// Recursive (for learning)
function factorialRecursive(n) {
if (n <= 1) return 1;
return n * factorialRecursive(n - 1);
}
Special Cases
- 0! = 1: The empty arrangement convention.
- 1! = 1: The product of just 1.
- Negative factorial: Undefined in standard mathematics. Gamma function extends to non-integers, but (-1)!, (-2)!, etc. remain undefined.
- Fractional factorial: Not defined for non-integers. Gamma(n+1) extends the concept.
- Large factorials: Require arbitrary precision arithmetic. 100! has 158 digits.
- Double factorial n!!: Multiply by steps of 2. 6!! = 6 x 4 x 2 = 48. Different operation entirely.
Common Mistakes to Avoid
- Thinking 0! = 0: It equals 1 by definition. Memorize this.
- Skipping numbers: 5! = 5 x 4 x 3 x 2 x 1, not 5 x 1. Every integer must be multiplied.
- Confusing with exponentiation: 5! = 120 but 5^2 = 25. Different operations entirely.
- Integer overflow: In programming, 21! exceeds signed 64-bit integer range. Use BigInt.
- Wrong base case: Recursive implementations must return 1 for n=0 or n=1.
- Ignoring order: Factorial counts ordered arrangements. For unordered selections, use combinations.
Frequently Asked Questions
Historical Context
Indian mathematics developed early combinatorial concepts. The Jaina mathematician Mahavira (9th century) described formulas for permutations and combinations that implicitly used factorials.
European mathematicians formalized factorial notation. The exclamation mark was introduced by Christian Kramp in 1808, though the concept was used earlier by others including Stirling.
Leonhard Euler contributed significantly to factorial theory, including the Gamma function extension and Stirling's approximation development.
Probability theory expanded factorial applications. Pascal's triangle, binomial coefficients, and probability distributions all depend on factorial calculations.
Modern computing optimized factorial algorithms. Fast multiplication algorithms compute large factorials, and arbitrary precision arithmetic enables calculations far beyond traditional limits.
Related Calculators
These tools extend factorial concepts:
- Permutation Calculator: Calculate P(n,r) using factorials.
- Combination Calculator: Calculate C(n,r) for selection problems.
- Probability Calculator: Use factorial-based probability formulas.
- Exponent Calculator: Compare growth rates with factorials.
- GCD Calculator: Related number theory operations.
- Scientific Calculator: General mathematical computations.
Conclusion
Factorials are fundamental mathematical operations that count arrangements and form the basis of combinatorics, probability theory, and algorithm analysis. This page covered four calculation methods: direct multiplication for beginners, recursive definition for conceptual understanding, iterative loops for programming implementation, and Stirling's approximation for large-value estimation.
The extraordinary growth rate of factorials explains why brute-force permutation algorithms are limited to small inputs. Understanding factorial behavior helps programmers recognize when algorithms are impractical and when alternative approaches are necessary.
Use the factorial calculator above for instant, accurate results. Whether solving permutation problems, calculating probabilities, analyzing algorithms, or learning mathematical foundations, this tool computes values that would be tedious or impossible by hand.