Combinatorics
Permutations & Combinations
Counting arrangements and selections — the foundation of combinatorics
A permutation is an arrangement of objects in order; a combination is a selection where order doesn't matter. P(n, k) = n!/(n−k)! counts ordered selections; C(n, k) = n!/(k!(n−k)!) counts unordered. Used in probability (lottery odds, card games), cryptography (key spaces), genetics (gene combinations), and any "how many ways..." question.
- PermutationsP(n, k) = n! / (n − k)! — order matters
- CombinationsC(n, k) = n! / (k!(n − k)!) — order doesn't matter
- RelationshipP(n, k) = C(n, k) · k!
- Total subsets2ⁿ — sum of all C(n, k)
- Total permutations of n itemsn!
- Real applicationsLottery odds, card hands, password spaces, race finishing orders
Interactive visualization
Press play, or step through manually. The visualization is yours to drive — try it before reading on.
Watch the 60-second explainer
A condensed visual walkthrough — narrated, captioned, under a minute.
Permutations — when order matters
The number of ways to arrange k items chosen from n distinct items, where order matters:
P(n, k) = n · (n−1) · (n−2) · ... · (n−k+1) = n! / (n−k)!
For all of n items arranged: P(n, n) = n!.
Example — first, second, third place in a race with 8 runners. P(8, 3) = 8 · 7 · 6 = 336 ways. (Order matters because gold, silver, and bronze are different positions.)
Combinations — when order doesn't matter
The number of ways to choose k items from n distinct items, where order doesn't matter:
C(n, k) = n! / (k! · (n−k)!) = P(n, k) / k!
The "n choose k" notation. Read as "n choose k." Also written ⁿCₖ or (n, k) in superscript form.
Example — choose 5 cards from a 52-card deck. C(52, 5) = 52! / (5! · 47!) = 2,598,960 hands.
Permutation vs Combination — when to use which
| Question | Order matters? | Use |
|---|---|---|
| "How many 4-digit PINs?" | Yes (1234 ≠ 4321) | 10⁴ (with repetition) or P(10, 4) (without) |
| "How many ways to seat 8 people?" | Yes | P(8, 8) = 8! = 40,320 |
| "How many 5-card poker hands?" | No (hand is a set) | C(52, 5) = 2,598,960 |
| "How many committees of 3 from 12 people?" | No | C(12, 3) = 220 |
| "First, second, third place from 10 racers?" | Yes | P(10, 3) = 720 |
| "How many ways to pick 2 colors from 8?" | No | C(8, 2) = 28 |
Rule of thumb — if rearranging a chosen set gives a "different" outcome, use permutation; if it's the "same," use combination.
Worked examples
Example 1 — Powerball jackpot odds
5 numbers chosen from 69 (no repetition, no order) + 1 number from 26.
Main: C(69, 5) = 69!/(5! · 64!) = 11,238,513
Powerball: 26
Total: 11,238,513 × 26 = 292,201,338
Buying one ticket gives 1/292,201,338 chance of jackpot — about one in 290 million. You're more likely to be struck by lightning twice than win.
Example 2 — Birthday problem
23 people in a room. Probability that at least two share a birthday?
It's easier to compute the complement — probability all 23 birthdays are distinct.
P(all distinct) = (365/365) · (364/365) · (363/365) · ... · (343/365)
= P(365, 23) / 365²³
≈ 0.4927
P(at least two shared) ≈ 1 − 0.4927 = 0.5073 ≈ 50.7%. Counterintuitive — only 23 people for >50% chance of a shared birthday.
Example 3 — Number of poker hands
5 cards from 52. C(52, 5) = 2,598,960. Probability of a specific hand type:
| Hand | Number of hands | Probability |
|---|---|---|
| Royal flush | 4 | 0.000154% |
| Straight flush | 36 | 0.00139% |
| Four of a kind | 624 | 0.024% |
| Full house | 3,744 | 0.144% |
| Flush | 5,108 | 0.197% |
| Straight | 10,200 | 0.392% |
| Three of a kind | 54,912 | 2.11% |
| Two pair | 123,552 | 4.75% |
| One pair | 1,098,240 | 42.3% |
| High card | 1,302,540 | 50.1% |
JavaScript — counting
// Factorial
function factorial(n) {
let result = 1;
for (let i = 2; i <= n; i++) result *= i;
return result;
}
// Permutations P(n, k)
function permutations(n, k) {
if (k > n) return 0;
let result = 1;
for (let i = 0; i < k; i++) result *= (n - i);
return result;
}
// Combinations C(n, k)
function combinations(n, k) {
if (k > n) return 0;
if (k > n - k) k = n - k; // symmetry: C(n, k) = C(n, n-k)
let result = 1;
for (let i = 0; i < k; i++) {
result = result * (n - i) / (i + 1);
}
return Math.round(result);
}
// Examples
console.log(permutations(8, 3)); // 336
console.log(combinations(52, 5)); // 2598960
console.log(combinations(69, 5)); // 11238513
// Birthday problem
function birthdayProblem(people) {
let pAllDifferent = 1;
for (let i = 0; i < people; i++) {
pAllDifferent *= (365 - i) / 365;
}
return 1 - pAllDifferent;
}
console.log(birthdayProblem(23).toFixed(3)); // 0.507
console.log(birthdayProblem(50).toFixed(3)); // 0.970
Variations and special cases
| Problem | Formula |
|---|---|
| Permutations of n distinct items | n! |
| Permutations of k from n (no rep) | n! / (n − k)! |
| Permutations with repetition (k slots, n choices) | n^k |
| Permutations with identical groups | n! / (k₁! · k₂! · ... · kₘ!) |
| Combinations of k from n (no rep) | C(n, k) = n! / (k!(n−k)!) |
| Combinations with repetition | C(n+k−1, k) |
| Subsets (any size) | 2ⁿ |
| Pairs of subsets | 3ⁿ |
Where this matters
- Probability. Lottery odds, card games, dice combinations, the birthday problem — all use combinations and permutations.
- Cryptography. Password space size — n^k for character × position. Key strength is measured in number of possible keys.
- Genetics. Number of possible genotypes from given alleles, distribution of traits in offspring.
- Statistics. Sampling without replacement, hypergeometric distribution, multinomial expansions.
- Computer science. Counting algorithm complexity (number of permutations to try, number of subsets to consider). Brute-force enumeration uses these counts directly.
- Operations research. Scheduling, assignment problems, traveling salesman (n! tours). Combinatorial optimization is built on these counts.
- Combinatorial design. Latin squares, balanced incomplete block designs, error-correcting codes.
Common mistakes
- Confusing permutations with combinations. The single most common error. "Order matters" is the test. If swapping two items gives a "different" answer, use permutation. If "same," use combination.
- Forgetting whether repetition is allowed. "Choose 4 letters from 26" — without repetition (P(26, 4) or C(26, 4)) or with (26⁴)? The two answers differ by orders of magnitude.
- Computing factorials directly for large n. 100! is too big to fit in any standard integer. Use the iterative product formula or BigInt; cancel before multiplying when possible.
- Forgetting 0! = 1. Both C(n, 0) = 1 and C(n, n) = 1 by convention. The empty arrangement counts as one valid arrangement.
- Mixing up identical objects. Permutations of MISSISSIPPI (1 M, 4 I, 4 S, 2 P) is 11! / (1!·4!·4!·2!) = 34,650, not 11!. Identical letters can't be told apart.
- Confusing P(n, k) with permutation P(X) of X. The function name is overloaded; check context.
Frequently asked questions
What's the difference between permutation and combination?
Order. Permutations count arrangements where order matters — (A, B, C) and (B, A, C) are different. Combinations count selections where order doesn't — picking A, B, C is the same as picking B, A, C. P(n, k) = n!/(n-k)! = number of ways to arrange k items chosen from n. C(n, k) = P(n, k)/k! = number of ways to choose k items from n (ignoring order).
Why is C(n, k) = n! / (k!(n−k)!)?
To pick k items from n — first arrange them in order (P(n, k) = n!/(n-k)! ways), then divide by the k! orderings that produce the same combination. So C(n, k) = P(n, k)/k! = n!/(k!(n-k)!). The k! divisor "removes" the ordering distinction.
What does 0! equal?
By convention, 0! = 1. The reason — there's exactly one way to arrange zero objects (the empty arrangement). It also makes formulas like C(n, 0) = 1 (one way to choose nothing) work without special cases. The convention is universal in modern math.
How is this used in lottery probability?
A lottery picks k numbers from 1 to N without order. Number of possible tickets = C(N, k). For Powerball — 5 main numbers from 69 plus 1 Powerball from 26. C(69, 5) · 26 = 11,238,513 · 26 = 292.2 million combinations. Buying one ticket gives you 1 / 292,201,338 chance of winning the jackpot.
How many ways can you arrange a deck of 52 cards?
52! ≈ 8 × 10⁶⁷. More than the number of atoms on Earth. So a typical shuffle produces a sequence that has likely never been produced before in human history (and never will be again). 52! is one of the canonical "astronomically large" numbers.
What about permutations with repetition allowed?
For n distinguishable items, k slots with repetition allowed — n^k. For 4-digit PINs — 10⁴ = 10,000 possibilities. Without repetition (distinct digits) — P(10, 4) = 5040.
How does this relate to Pascal's triangle?
The (n, k)-th entry of Pascal's triangle is C(n, k). Each row's entries sum to 2ⁿ — the total number of subsets of an n-element set. The triangle visualizes the combinatorial structure of binomial coefficients.