Factorial Calculator
Enter a non-negative integer to calculate its factorial (n!), double factorial (n!!), and subfactorial (derangement number !n). Factorials are essential in permutations, combinations, and probability.
A factorial, written n!, is the product of all positive integers from 1 to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. The factorial is fundamental in combinatorics — counting arrangements, permutations, combinations, and probability. By convention, 0! = 1 (an "empty product"), which makes formulas work consistently.
Factorials grow incredibly fast. 10! = 3,628,800. 20! ≈ 2.4 × 10^18 (more than a quintillion). 70! ≈ 1.2 × 10^100 — more than a googol! Computers struggle with factorials above 170 because 171! exceeds the largest representable double-precision number (~1.8 × 10^308).
Factorials count permutations: the number of ways to arrange n distinct objects in order. With 5 books on a shelf, you can arrange them in 5! = 120 different orders. With 10 students lining up: 10! = 3,628,800. With 52 playing cards: 52! ≈ 8 × 10^67 — more than the number of atoms in our galaxy.
Factorials also appear in: - **Combinations**: C(n,k) = n! / (k! × (n-k)!) — number of ways to choose k from n. - **Probability**: many discrete probabilities involve factorials. - **Series expansions**: Taylor series include 1/n! terms. - **Statistics**: gamma function generalizes factorial to non-integers.
This calculator also handles two variants: - **Double factorial** (n!!): product of integers of same parity (5!! = 5 × 3 × 1). - **Subfactorial** (!n): counts "derangements" — permutations where no element is in its original position.
Common applications: combinatorics, probability theory, statistics, computer science (algorithm analysis), physics (statistical mechanics), and any counting problem involving arrangements or selections.
Inputs
Max 170 due to floating-point limits
Results
n!
720
6 x 5 x 4 x 3 x 2 x 1 = 720
n!!
48
6 x 4 x 2 = 48
!n (Subfactorial)
265
Number of derangements
Digits in n!
3
Formula
How to use this calculator
- Enter a non-negative integer (0 to 170).
- Calculator returns n!, n!! (double factorial), and !n (subfactorial).
- For very large n: numbers display in scientific notation.
- Above n = 170: floating-point overflow (use arbitrary-precision tools for larger).
- 0! = 1 by convention.
- For arbitrary real numbers: use gamma function Γ(n+1) = n!.
Worked examples
Arranging books on a shelf
**Scenario:** You have 7 different books. How many ways to arrange them on a shelf? **Calculation:** 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5,040. **Result:** 5,040 different orderings. Each unique arrangement is one of these 5,040 permutations. With 10 books: 10! = 3,628,800 arrangements. Permutations grow factorially with item count.
Lottery probability
**Scenario:** Lottery picks 6 numbers from 1 to 49. Total combinations? **Calculation:** C(49, 6) = 49! / (6! × 43!) = (49×48×47×46×45×44) / (6×5×4×3×2×1) = 10,068,347,520 / 720 = 13,983,816. **Result:** ~14 million possible combinations. Probability of matching all 6: 1 in 14 million. To put in perspective: more likely to be struck by lightning twice in a lifetime than win the jackpot. Lottery is essentially a tax on poor math intuition.
Card shuffling
**Scenario:** Standard 52-card deck. How many possible shuffles? **Calculation:** 52! ≈ 8.066 × 10^67. **Result:** ~8 × 10^67 distinct orderings — vastly more than atoms in our galaxy (~10^67). Every time anyone shuffles a deck thoroughly, the resulting order has almost certainly never existed before in human history. Mathematics of "uniqueness" via factorials.
When to use this calculator
**Use factorials for:**
- **Counting arrangements**: permutations of n objects (n!). - **Combinations**: ways to choose k from n. - **Probability**: many discrete probability calculations. - **Statistics**: combinations, binomial coefficients. - **Computer science**: algorithm complexity analysis. - **Card games**: hand probabilities. - **Lottery analysis**: combination counting. - **Anagrams and word puzzles**: counting letter arrangements.
**Permutations vs combinations:**
Order matters? - **Yes**: permutation P(n,r) = n!/(n-r)! - **No**: combination C(n,k) = n!/(k!(n-k)!)
Examples: - **Permutation**: assigning roles (president, VP, secretary): P(10,3) = 720 ways from 10 candidates. - **Combination**: choosing 3 friends to invite: C(10,3) = 120 ways from 10.
**Common combination uses:**
- **Lottery**: C(N, k) for picking k from N. - **Poker**: C(52, 5) for 5-card hands = 2.6M. - **Voting systems**: counting possible outcomes. - **Sports brackets**: matchup possibilities.
**Combinations to memorize:**
- C(n, 0) = 1 (one way to choose none). - C(n, n) = 1 (one way to choose all). - C(n, 1) = n. - C(n, 2) = n(n-1)/2. - C(n, k) = C(n, n-k) (symmetry).
**Stirling's approximation:**
For large n: n! ≈ √(2πn) × (n/e)^n
Useful when actual factorial overflows. Accurate within ~1% for n ≥ 10.
Logarithmic form: ln(n!) ≈ n × ln(n) − n + 0.5 × ln(2πn)
**Common applications:**
- **Probability**: binomial, hypergeometric, Poisson distributions. - **Statistics**: t-distribution, F-distribution use gamma function. - **Genetics**: counting allele combinations. - **Cryptography**: birthday attacks use 23-people probability. - **Algorithms**: traveling salesman O(n!), permutation generation. - **Quantum mechanics**: state counting. - **Card magic**: depending on deck order.
**Edge cases:**
- 0! = 1 (convention, makes formulas work). - 1! = 1. - (-n)! undefined for positive n (negative integers). - (1/2)! = √π/2 ≈ 0.886 (via gamma function).
**Big factorials in practice:**
- 13!: ~6 billion (deck of cards exceeds this). - 21!: about 5 × 10^19 (about US national debt in dollars). - 25!: about 10^25 (atoms in 1 mole of substance). - 70!: about 10^100 (a googol). - 170!: maximum in IEEE double-precision.
**Software for large factorials:**
- **Python**: arbitrary precision integers natively (math.factorial). - **Java**: BigInteger class. - **Mathematica**: native arbitrary precision. - **Specialized**: number theory libraries.
For 1000!: ~2.6 × 10^2567 (over 2,500 digits long).
**Pitfalls:**
- **Overflow**: n > 170 overflows standard floats. - **Computational time**: large factorials slow even in arbitrary precision. - **Wrong formula**: confusing P(n,r) with C(n,r). - **Direct computation**: avoid for large n in combinations; use cancellation. - **Negative numbers**: factorial undefined. - **0!**: must define as 1; common error to assume 0.
**Combinations in practice:**
For C(52, 5) = 2,598,960: - 4 of a kind: 624. - Full house: 3,744. - Flush: 5,108. - Royal flush: 4.
Probabilities follow from these counts.
**Probability formula:**
P(event) = (favorable outcomes) / (total outcomes)
Both numerator and denominator often involve combinations.
P(flush in 5-card hand) = C(13, 5) × 4 / C(52, 5) = 1287 × 4 / 2598960 ≈ 0.00198 = 0.198%.
(Excluding straight flush which counts separately.)
**Factorial in series:**
e^x = Σ x^n / n! sin(x) = Σ (-1)^n × x^(2n+1) / (2n+1)! cos(x) = Σ (-1)^n × x^(2n) / (2n)!
Factorials in denominators make series converge.
Common mistakes to avoid
- Forgetting 0! = 1 (by convention).
- Confusing factorial with exponent (5! = 120, not 5^5 = 3,125).
- Computing large factorials directly (overflow); use Stirling or simplification.
- Confusing P(n,r) and C(n,r).
- Using factorial for negative integers (undefined).
- Not simplifying combinations (computing n! and (n-k)! separately when they share factors).
- Confusing double factorial 5!! with (5!)!.
- Treating subfactorial as a real factorial operation.