Factorial Calculator

Calculate the factorial (n!) of any non-negative integer with step-by-step breakdown. Factorials grow extremely fast — maximum input is 170.

Factorial (n!)
Enter a number and click calculate

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

PropertyFormulaExample
Recursive definitionn! = n x (n-1)!5! = 5 x 4! = 5 x 24 = 120
Base cases0! = 1, 1! = 1Foundation of all factorial calculation
Only non-negative integersFactorial undefined for negatives(-1)! has no meaning in standard math
Extremely fast growthn! grows faster than n^k for any k10! = 3.6 million, 15! = 1.3 trillion

Factorial Values Reference Table

nn!Digits
011
111
221
361
4242
51203
67203
75,0404
840,3205
9362,8806
103,628,8007
12479,001,6009
151.307 x 10^1213
202.43 x 10^1819
503.04 x 10^6465
1009.33 x 10^157158

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

n!

The exclamation mark follows the number and indicates factorial operation.

Expanded Formula

n! = n x (n-1) x (n-2) x (n-3) x ... x 3 x 2 x 1

Recursive Formula

n! = n x (n-1)!

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

  1. Start with the number 1.
  2. Multiply by 2, store the result.
  3. Multiply by 3, store the result.
  4. Continue multiplying by each successive integer up to n.
  5. 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

  1. Express n! as n x (n-1)!
  2. Express (n-1)! as (n-1) x (n-2)!
  3. Continue until reaching 1! or 0!
  4. 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

  1. Initialize result = 1.
  2. For each integer i from 1 to n:
  3. Update result = result x i.
  4. Return final result.

Worked Example: Calculate 6!

StepiCalculationResult
Initializeresult = 11
111 x 11
221 x 22
332 x 36
446 x 424
5524 x 5120
66120 x 6720

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

n! ~ sqrt(2pi x n) x (n/e)^n

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

MethodBest ForSpeedAccuracyComplexity
Direct MultiplicationBeginners, small numbersSlowExactLow
RecursiveLearning, proofsMediumExactMedium
Iterative LoopProgramming, standard useFastExactLow
Stirling's ApproximationLarge numbers, estimatesVery FastApproximateHigh

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

nn!2^nn^2
51203225
103,628,8001,024100
151.3 x 10^1232,768225
202.4 x 10^181,048,576400

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

n objects can be arranged in n! different ways

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

C(n,r) = n! / (r! x (n-r)!)

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:

P(k successes in n trials) = C(n,k) x p^k x (1-p)^(n-k)

Factorial in Programming

LanguageFunctionNotes
Pythonmath.factorial(n)Built-in, handles up to large values
JavaScriptCustom functionfunction f(n){return n<=1?1:n*f(n-1)}
JavaBigIntegerUse BigInteger for large values
C++Custom or booststd::int64_t overflows at 21!
Excel=FACT(n)Built-in worksheet function
Rfactorial(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

What is factorial?
A factorial is the product of all positive integers from 1 to n. Written as n!, it represents multiplication of consecutive integers. For example, 5! = 5 x 4 x 3 x 2 x 1 = 120. Factorials count the number of ways to arrange n distinct objects in order.
How do you calculate factorial?
Multiply all positive integers from 1 to n together. Start with 1, multiply by 2, then by 3, continuing until n. For 6!: 1 x 2 = 2, x 3 = 6, x 4 = 24, x 5 = 120, x 6 = 720. Use a calculator for values above 10 due to rapid growth.
Why is 0! equal to 1?
0! = 1 because there is exactly one way to arrange zero objects: the empty arrangement. This convention makes formulas like combinations work correctly. Without 0! = 1, C(n,n) = n!/(n! x 0!) would be undefined rather than 1.
What is the difference between factorial and exponent?
Factorial multiplies consecutive integers (n! = n x (n-1) x ... x 1) while exponent multiplies the same base repeatedly (n^k = n x n x ... x n, k times). Factorial grows faster for large n. For example, 10! = 3,628,800 but 10^3 = 1000.
How is factorial used in programming?
Python has math.factorial(n) built-in. JavaScript requires custom functions. Java uses BigInteger for large values to prevent overflow. Excel has =FACT(n). C++ requires custom implementation or libraries. Most languages overflow around 20! with standard integers.
What is the largest factorial you can calculate?
In standard floating-point (64-bit), 170! is the maximum before overflow, approximately 7.3 x 10^306. With arbitrary precision, much larger values are possible. 1000! has over 2500 digits. Python handles arbitrarily large factorials by default.
How are factorials used in real life?
Factorials count arrangements: password possibilities, book shelf arrangements, tournament bracket orders, card shuffling outcomes, seating arrangements, DNA sequence permutations, lottery combinations, and probability calculations in statistics and research.
What is the factorial formula?
n! = n x (n-1) x (n-2) x ... x 2 x 1 for positive integers, with 0! = 1 as the base case. Recursively: n! = n x (n-1)!. For large n, Stirling's approximation estimates factorial: n! ~ sqrt(2pi*n) x (n/e)^n.
Can you calculate factorial of negative numbers?
No, factorial is undefined for negative integers in standard mathematics. The Gamma function extends factorial concepts to non-integers, but negative integers remain undefined because Gamma has poles at all non-positive integers.
How fast does factorial grow?
Factorial grows faster than any exponential with fixed base. 5! = 120, 10! = 3.6 million, 15! = 1.3 trillion, 20! = 2.4 quintillion. This is why factorial-time algorithms are impractical beyond very small inputs.
What is the relation between factorial and permutations?
n! equals the number of permutations of n distinct objects. If you have 5 different books, there are exactly 5! = 120 ways to arrange them in order. Permutations of r items from n total equals n!/(n-r)!. Factorial is foundational to all arrangement counting.
How do you calculate factorial without a calculator?
Multiply consecutive integers starting from 1. For 6!: 1 x 2 = 2, x 3 = 6, x 4 = 24, x 5 = 120, x 6 = 720. For mental math, group products: 5! = (2x3) x (4x5) x 1 = 6 x 20 = 120. Memorize small values (1-10) for efficiency.
What is the value of 1 factorial?
1! = 1. The product of all positive integers from 1 to 1 is just 1 itself. This follows the definition and makes recursive formulas work: since 2! = 2 x 1!, we need 1! = 1 for 2! = 2 x 1 = 2.
How is factorial used in combinations?
The combinations formula C(n,r) = n!/(r! x (n-r)!) uses factorial to count ways to select r items from n without considering order. For example, C(5,2) = 5!/(2! x 3!) = 120/12 = 10 ways to choose 2 items from 5.

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.