Why This Matters
How many different passwords can you make with 8 characters? How many ways can a team of 5 be chosen from 20 people? These questions sit at the heart of combinatorics, the mathematics of counting. In computer science, combinatorics tells you the size of your search space, which directly determines how long a brute-force algorithm will take. If you do not understand counting, you cannot reason about algorithmic complexity or probability.
A permutation counts arrangements where order matters — like ranking contestants or assigning seats. A combination counts selections where order does not matter — like choosing team members or card hands. The factorial function connects both: permutations use n!, while combinations divide out the redundant orderings. Mastering these two ideas unlocks all of probability theory and a large chunk of algorithm analysis.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Permutations count ordered arrangements. Combinations count unordered selections. The only difference is dividing by r! to remove duplicate orderings.
Code Example
// Factorial function
function factorial(n) {
if (n <= 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
console.log("5! =", factorial(5)); // 120
console.log("0! =", factorial(0)); // 1
// Permutation: P(n, r) = n! / (n - r)!
function permutation(n, r) {
return factorial(n) / factorial(n - r);
}
console.log("P(5,3) =", permutation(5, 3)); // 60
console.log("P(10,2) =", permutation(10, 2)); // 90
// Combination: C(n, r) = n! / (r! * (n - r)!)
function combination(n, r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
console.log("C(5,3) =", combination(5, 3)); // 10
console.log("C(10,2) =", combination(10, 2)); // 45
// Relationship: C(n,r) = P(n,r) / r!
console.log("P(5,3)/3! =", permutation(5, 3) / factorial(3)); // 10
// Practical: how many 4-digit PINs?
console.log("4-digit PINs:", Math.pow(10, 4)); // 10000
// How many ways to choose 5 cards from 52?
console.log("Poker hands:", combination(52, 5)); // 2598960Interactive Experiment
Try these exercises:
- Compute C(6,2) and P(6,2) by hand. Verify that C(6,2) = P(6,2) / 2!.
- How many 3-letter arrangements can you make from the letters A, B, C, D, E? Check with P(5,3).
- Compute C(n, 0) and C(n, n) for any n. Why are both always 1?
- Verify that C(n, r) = C(n, n-r). Try it with C(10, 3) and C(10, 7).
- How many different 5-card poker hands exist? Use C(52, 5) and verify with code.
Quick Quiz
Coding Challenge
Write two functions: `nPr(n, k)` that returns the number of permutations of k items from n, and `nCr(n, k)` that returns the number of combinations of k items from n. Use the factorial-based formulas. Both n and k are non-negative integers with k at most n.
Real-World Usage
Combinatorics is everywhere in computing and daily life:
- Password security: An 8-character alphanumeric password has 62^8 (about 218 trillion) possibilities. Combinatorics tells you how strong your passwords are against brute force.
- Algorithm analysis: The number of subsets of n items is 2^n. This is why brute-force subset problems are exponential and why we need dynamic programming.
- Machine learning: Feature selection involves choosing k features from n candidates. The number of possible feature sets is C(n, k), which grows rapidly.
- Networking: The number of unique connections in a network of n nodes is C(n, 2) = n(n-1)/2. This is why fully connected networks do not scale.
- Card games and simulations: Poker odds, lottery probabilities, and Monte Carlo simulations all rely on combinatorial counting.