Why This Matters
Every time you compute the number of possible passwords, API keys, or database query results, you are using counting principles. A 4-digit PIN has 10 x 10 x 10 x 10 = 10,000 possibilities because the multiplication principle tells us to multiply when choices are made in sequence. If a menu offers 3 soups OR 5 salads, the addition principle says you have 3 + 5 = 8 options because you choose one or the other, not both.
These two rules are the foundation of all combinatorics. Whether you are analyzing algorithm complexity, estimating hash collision probabilities, or designing security systems, counting principles are the starting point. Master these and every advanced counting technique — permutations, combinations, inclusion-exclusion — becomes a natural extension.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
The two fundamental counting rules: multiply for sequential choices, add for disjoint cases.
Code Example
// Multiplication Principle: sequential independent choices
// How many 4-digit PINs exist? (digits 0-9)
const digitsPerSlot = 10;
const pinSlots = 4;
const totalPins = Math.pow(digitsPerSlot, pinSlots);
console.log("4-digit PINs:", totalPins); // 10000
// Addition Principle: disjoint alternatives
// Menu: 3 soups OR 5 salads
const soups = 3;
const salads = 5;
console.log("Starter options:", soups + salads); // 8
// Combined: license plates = 3 letters + 4 digits
const letterChoices = 26;
const digitChoices = 10;
const plates = Math.pow(letterChoices, 3) * Math.pow(digitChoices, 4);
console.log("License plates:", plates); // 175760000
// Counting outcomes programmatically
function countOutcomes(choicesPerStep) {
return choicesPerStep.reduce((total, c) => total * c, 1);
}
console.log("Coin, die, card:", countOutcomes([2, 6, 52])); // 624
// Addition: bit strings of length <= 3
// Length 1: 2, Length 2: 4, Length 3: 8
const bitStrings = Math.pow(2,1) + Math.pow(2,2) + Math.pow(2,3);
console.log("Bit strings of length 1-3:", bitStrings); // 14
// Enumerate to verify
function enumeratePins(digits, length) {
if (length === 0) return [""];
const shorter = enumeratePins(digits, length - 1);
const result = [];
for (const prefix of shorter) {
for (let d = 0; d < digits; d++) {
result.push(prefix + d);
}
}
return result;
}
console.log("2-digit PINs (base 3):", enumeratePins(3, 2).length); // 9Interactive Experiment
Try these exercises:
- How many binary strings of length 8 exist? Verify that it equals 2 to the power of 8.
- A restaurant has 4 appetizers, 6 mains, and 3 desserts. If you pick one of each, how many full meals can you make?
- How many 3-character passwords can you make using lowercase letters (26) and digits (10)? What if each character can be either a letter OR a digit?
- Roll a die and flip a coin. List all outcomes by hand, then verify the count matches the multiplication principle.
- How many even 3-digit numbers exist? (Hint: the last digit must be 0, 2, 4, 6, or 8.)
Quick Quiz
Coding Challenge
Write a function called `countOutcomes` that takes two arguments: `numChoicesPerStep` (an array of integers representing the number of choices at each step) and `disjointGroups` (an array of integers representing disjoint group sizes to add). Return the total outcomes by first computing the product of all values in `numChoicesPerStep` (multiplication principle), then adding each value in `disjointGroups` to that product (addition principle). If `numChoicesPerStep` is empty, treat the product as 0. If `disjointGroups` is empty, just return the product.
Real-World Usage
Counting principles appear everywhere in computing and daily life:
- Password strength: An 8-character password with 62 possible characters (a-z, A-Z, 0-9) has 62^8 = 218 trillion possibilities. This is the multiplication principle applied to security.
- Database query results: A JOIN of a 1,000-row table with a 500-row table can produce up to 1,000 * 500 = 500,000 rows. Understanding this helps predict query performance.
- Test case generation: If a function has 3 parameters, each with 5 possible values, you need 5 * 5 * 5 = 125 test cases for exhaustive coverage.
- Probability foundations: The probability of an event is (favorable outcomes) / (total outcomes). You need counting principles to compute both numbers.
- Network routing: If a packet can take 3 paths from A to B and 4 paths from B to C, there are 3 * 4 = 12 paths from A to C.