CalcMountain

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

Last updated:

Formula

**Factorial (n!):** n! = n × (n-1) × (n-2) × ... × 2 × 1 By convention: 0! = 1. **Worked examples:** 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5,040 8! = 40,320 9! = 362,880 10! = 3,628,800 **Recursive definition:** n! = n × (n-1)! Each factorial is n times the previous factorial. **Growth rate:** | n | n! | ~10^? | |---|---|---| | 1 | 1 | — | | 5 | 120 | ~10² | | 10 | 3,628,800 | ~10⁷ | | 15 | 1.3 × 10^12 | 10^12 | | 20 | 2.4 × 10^18 | 10^18 | | 25 | 1.6 × 10^25 | 10^25 | | 50 | 3.0 × 10^64 | 10^64 | | 70 | ~10^100 (googol) | 10^100 | | 100 | ~9.3 × 10^157 | 10^157 | | 170 | ~7.3 × 10^306 | 10^307 | | 171 | overflow in double-precision | — | **Double factorial (n!!):** For odd n: n!! = n × (n-2) × ... × 3 × 1 For even n: n!! = n × (n-2) × ... × 4 × 2 Examples: - 5!! = 5 × 3 × 1 = 15 - 7!! = 7 × 5 × 3 × 1 = 105 - 6!! = 6 × 4 × 2 = 48 - 8!! = 8 × 6 × 4 × 2 = 384 **Subfactorial (!n):** Number of derangements — permutations where no element stays in its original position. Examples: - !0 = 1 - !1 = 0 (can't be deranged) - !2 = 1 - !3 = 2 - !4 = 9 - !5 = 44 - !6 = 265 - !10 = 1,334,961 Approximation: !n ≈ n! / e (about 1/e ≈ 36.8% of permutations are derangements). **Stirling's approximation:** For large n: n! ≈ √(2πn) × (n/e)^n For n = 100: Actual: 9.3326 × 10^157. Stirling: 9.3248 × 10^157. (Within 0.1%) Useful for analysis and avoiding overflow. **Factorial in combinations:** Number of ways to choose k from n: C(n, k) = n! / (k! × (n-k)!) C(5, 2) = 5! / (2! × 3!) = 120 / (2 × 6) = 10. Number of ways to pick 2 from 5 (e.g., 2 lottery numbers from 5). **Factorial in permutations:** Number of ways to arrange r objects from n: P(n, r) = n! / (n-r)! P(5, 2) = 5! / 3! = 120/6 = 20. Number of ways to arrange 2 from 5 in order. **Common counting problems:** - **Lottery (pick 6 from 49)**: C(49, 6) = 13,983,816 combinations. - **Poker hand (5 from 52)**: C(52, 5) = 2,598,960 hands. - **Birthday paradox**: with 23 people, ~50% chance of shared birthday. - **Anagrams**: arrangements of letters in a word. **Combinations vs permutations:** | Problem | Use | |---|---| | Order matters | Permutation P(n,r) | | Order doesn't matter | Combination C(n,k) | Pick 3 from 10: - P(10, 3) = 720 (e.g., elections — order matters). - C(10, 3) = 120 (e.g., lottery — order doesn't matter). **Gamma function (generalization):** For non-integers: Γ(n+1) = n! Γ(0.5) = √π Γ(1.5) = 0.5 × √π Γ(n+1) = n × Γ(n) (recursive) Used in statistics (chi-square, gamma distribution), physics, advanced math. **Famous facts:** - 70! is approximately 1 googol (10^100). - 52! ≈ 8 × 10^67 — more than atoms in galaxy. Every shuffle of cards likely produces a unique order never seen before. - 13! ≈ 6 × 10^9 — about Earth's population. - 25! ≈ 10^25 — comparable to atoms on Earth. **Series with factorials:** - **e**: e = Σ 1/n! = 1 + 1 + 1/2 + 1/6 + 1/24 + ... ≈ 2.71828. - **sin(x), cos(x), e^x**: Taylor series have factorials in denominators. - **Bessel functions**: complex series with factorials. **Programming:** | Language | Syntax | |---|---| | Python | math.factorial(n) | | JavaScript | reduce loop | | C/C++ | recursive function | | Excel | =FACT(n) | | R | factorial(n) | | MATLAB | factorial(n) | **Common pitfalls:** - **Overflow**: factorials grow fast; n > 170 overflows double-precision. - **Big integer types**: Python handles arbitrary precision; C/C++ need libraries. - **Confusing factorial with exponent**: 5! ≠ 5^5. - **0! = 1 is convention**: makes formulas consistent. - **Negative factorials**: undefined for negative integers (gamma function has poles). **Combinations from large n:** For C(n, k) when n is large: avoid computing n! directly (overflows). Instead: C(n, k) = (n × (n-1) × ... × (n-k+1)) / k! For C(100, 5): 100 × 99 × 98 × 97 × 96 / 120 = 75,287,520. Much safer than computing 100! (which overflows). **Permutations within constraints:** For repeated elements: divide by factorials of counts. Permutations of "MISSISSIPPI" (11 letters: M×1, I×4, S×4, P×2): 11! / (1! × 4! × 4! × 2!) = 39,916,800 / 1,152 = 34,650. **Common applications:** - **Lottery and probability**: counting outcomes. - **Statistics**: chi-square, F-distribution involve factorials. - **Computer science**: O(n!) algorithm complexity (worst case for traveling salesman, etc.). - **Cryptography**: large-number combinatorics. - **Physics**: statistical mechanics (Boltzmann distribution). - **Genetics**: counting allele combinations. - **Card games**: shuffling, hand probabilities. - **Combinatorial design**: scheduling, tournaments.

How to use this calculator

  1. Enter a non-negative integer (0 to 170).
  2. Calculator returns n!, n!! (double factorial), and !n (subfactorial).
  3. For very large n: numbers display in scientific notation.
  4. Above n = 170: floating-point overflow (use arbitrary-precision tools for larger).
  5. 0! = 1 by convention.
  6. 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.

Frequently Asked Questions

Sources & further reading

SponsoredShop Top Deals on AmazonSupport CalcMountain — browse top-rated products at no extra cost to you.

Related Calculators