BRAGA

02 Writing02-02 Notes02-02-010

Factorial

Published 16 Apr 20266 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
n!=1⋅2⋅3…nn! = 1 \cdot 2 \cdot 3 \dots n
(1)

Deep Breakdown

What it tells me (Situation Awareness): n!n! (read "n factorial") is the product of all positive integers from 1 up to nn. It counts the number of ways to arrange nn distinct items in a sequence. Factorials appear constantly in combinatorics (which underpins probability and statistics), in the formulas for combinations and permutations, in Taylor series expansions of exe^x, sin⁡\sin, and cos⁡\cos, and in the denominator of many probability mass functions (Poisson, Binomial). If you have seen (nk)\binom{n}{k} (n-choose-k), its denominator is k!(n−k)!k!(n-k)!.

Flowchart

Yes (base case)NoInput: n(non-negativeinteger)n == 0 or n ==1?Return 1Multiply: n *(n-1) * (n-2) * ...* 2 * 1Result: n!Key: 0! = 1 byconvention (nota coincidence)Result growsextremely fast:10! = 3628800,20! approx2.4e18

Real-world use

Where it is used

Counting arrangements and selections in probability, computing combination and permutation formulas, appearing in Taylor series (used in deriving logistic regression and probability distributions), and in the denominator of Poisson PMF (P(X=k)=e−λλk/k!P(X=k) = e^{-\lambda} \lambda^k / k!).

Problem it solves

How many ways can you arrange 5 different products on a shelf? The answer is 5!=1205! = 120. How many ways can a recommendation engine rank 10 items? 10!=3,628,80010! = 3{,}628{,}800. Factorial gives you the exact count of ordered arrangements, which is the foundation of probability calculations involving sequences.

How it solves the problem

Each position in a sequence reduces your remaining choices by one. For nn items: first position has nn options, second has n−1n-1, ..., last has 1. Multiplying these choices gives n×(n−1)×⋯×1=n!n \times (n-1) \times \cdots \times 1 = n!.

Example in a business

Computing the probability of a specific sequence of events in a Poisson process. The Poisson PMF is P(X=k)=e−λλk/k!P(X=k) = e^{-\lambda} \lambda^k / k!. The k!k! in the denominator prevents overcounting all the orderings of kk identical events. Without it, you would be treating "3 events in order ABC" as different from "3 events in order BAC" when in reality they are the same count.

The formula, piece by piece

Breaking the notation:

  • nn: any non-negative integer.
  • !!: the factorial operator. Postfix notation (written after the number).
  • ⋅\cdot: multiplication.
  • …\dots: "continuing the pattern." Full recursive definition:
n!={1if n=0 or n=1n×(n−1)!if n≥2n! = \begin{cases} 1 & \text{if } n = 0 \text{ or } n = 1 \\ n \times (n-1)! & \text{if } n \geq 2 \end{cases}
(2)

Expanded (product notation): n!=∏k=1nk=1×2×3×⋯×nn! = \prod_{k=1}^{n} k = 1 \times 2 \times 3 \times \cdots \times n Key values to memorize:

  • 0!=10! = 1 (by convention and by the empty product rule)
  • 1!=11! = 1
  • 2!=22! = 2
  • 3!=63! = 6
  • 4!=244! = 24
  • 5!=1205! = 120
  • 10!=3,628,80010! = 3{,}628{,}800
  • 20!≈2.43×101820! \approx 2.43 \times 10^{18} (grows explosively)

The formula in words

"n!n! means: start at nn, multiply by every integer below it down to 1. It counts how many distinct orderings of nn unique objects exist." The recursive view: "n!n! equals nn times the factorial of (n−1)(n-1)." This is because: if you have nn items, fix the first one (nn choices) and then arrange the remaining n−1n-1 in (n−1)!(n-1)! ways. Why is 0!=10! = 1? There is exactly one way to arrange zero objects: do nothing. The "empty arrangement" is a valid, unique arrangement. Also: it makes the combination formula (n0)=1\binom{n}{0} = 1 work cleanly.

Critical Concepts

  1. Explosive growth: factorials grow faster than any polynomial or exponential. 13!>613! > 6 billion. Be careful of overflow when computing them directly for large nn.
  2. 0!=10! = 1: not zero. This is a definition that ensures consistency in combination formulas and the empty product convention.
  3. Stirling's approximation: n!≈2πn(n/e)nn! \approx \sqrt{2\pi n} (n/e)^n. Used to estimate factorials of large numbers without computing them exactly.
  4. Log factorial: for large nn, computing ln⁡(n!)\ln(n!) using the log-sum-of-logs is numerically safer than computing n!n! directly (avoids overflow). Many statistical distributions work in log-space for this reason.
  5. Combinations use factorial: (nk)=n!k!(n−k)!\binom{n}{k} = \frac{n!}{k!(n-k)!}.

The binomial coefficient counts how many ways to choose kk items from nn without regard to order.

Step-by-step calculation (tiny example)

Input: compute 5!5!.

Step 1: Write out the product 5!=5×4×3×2×15! = 5 \times 4 \times 3 \times 2 \times 1

Step 2: Multiply left to right 5×4=205 \times 4 = 20 20×3=6020 \times 3 = 60 60×2=12060 \times 2 = 120 120×1=120120 \times 1 = 120

Step 3: Verify the interpretation There are exactly 120120 ways to arrange 5 distinct items (say, books on a shelf) in a row. List them mentally: with 5 choices for the first position, 4 for the second, etc.: 5×4×3×2×1=1205 \times 4 \times 3 \times 2 \times 1 = 120.

Code examples

Beginner example (Python)

python
import math

# Python's math.factorial()
print(math.factorial(0))   # 1 (0! = 1)
print(math.factorial(1))   # 1
print(math.factorial(5))   # 120
print(math.factorial(10))  # 3628800

# Manual implementation to understand the recursion
def factorial(n: int) -> int:
    """n! = n * (n-1) * ... * 1, with 0! = 1."""
    if n == 0 or n == 1:
        return 1
    return n * factorial(n - 1)

print(factorial(5))   # 120

# Log factorial (numerically safe for large n)
log_factorial_10 = sum(math.log(k) for k in range(1, 11))
print(f"ln(10!) = {log_factorial_10:.4f}")
print(f"ln(10!) = {math.log(math.factorial(10)):.4f}")   # Same

Beginner example (R)

r
# R's factorial() function
factorial(0)  # 1
factorial(1)  # 1
factorial(5)  # 120
factorial(10)  # 3628800

# Manual recursive version
my_factorial <- function(n) {
  if (n == 0 || n == 1) return(1)
  n * my_factorial(n - 1)
}
my_factorial(5)  # 120

# Log factorial (safer for large n)
lfactorial(10)  # log factorial: log(10!) in R

Professional example (Python)

python
import math
import numpy as np

# Scenario: computing Poisson PMF from scratch to understand where factorial is used
# P(X = k) = e^(-lambda) * lambda^k / k!
def poisson_pmf(k: int, lam: float) -> float:
    """
    Poisson probability mass function.
    P(X=k) = e^(-lambda) * lambda^k / k!
    Uses log-space arithmetic to avoid overflow for large k.
    """
    log_prob = -lam + k * math.log(lam) - math.lgamma(k + 1)
    # math.lgamma(k+1) = ln(k!) via the Gamma function: Gamma(k+1) = k!
    return math.exp(log_prob)

# Lambda = 3 (average 3 events per interval)
lam = 3.0
print(f"Poisson PMF with lambda={lam}:")
for k in range(0, 10):
    prob = poisson_pmf(k, lam)
    bar = "#" * int(prob * 100)
    print(f" P(X={k:2d}) = {prob:.4f} {bar}")

# Verify: probabilities sum to ~1
total = sum(poisson_pmf(k, lam) for k in range(0, 50))
print(f"\nSum of first 50 terms: {total:.6f}")   # Should be ~1.0

Professional example (R)

r
# Scenario: Poisson PMF from scratch; P(X=k) = e^(-lambda) * lambda^k / k!
# Uses log-space: lgamma(k+1) = ln(k!)
poisson_pmf <- function(k, lam) {
  log_prob <- -lam + k * log(lam) - lgamma(k + 1)
  exp(log_prob)
}
lam <- 3.0
cat("Poisson PMF with lambda=", lam, ":\n", sep = "")
for (k in 0:9) {
  prob <- poisson_pmf(k, lam)
  bar <- strrep("#", as.integer(prob * 100))
  cat(sprintf(" P(X=%2d) = %.4f %s\n", k, prob, bar))
}
total <- sum(sapply(0:49, function(k) poisson_pmf(k, lam)))
cat(sprintf("\nSum of first 50 terms: %.6f\n", total))

Source: 1300 p. 326

#foundations#maths-symbols#algebra

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close