Combinatorics
Mathematics
Combinations
Probability
Permutations

Calculating the Amount of Combinations

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Calculating combinations means counting selections where order does not matter. This sounds simple, but many wrong answers come from mixing combinations with permutations or from using the right formula without first checking what kind of counting problem is actually being asked.

Start by Asking Whether Order Matters

This is the first and most important question.

If choosing A then B is the same result as choosing B then A, the problem is about combinations. If the order creates different outcomes, the problem is about permutations.

Examples:

  • choosing 3 committee members from 10 people is a combinations problem
  • awarding gold, silver, and bronze medals to 3 people from 10 is a permutations problem

The first case ignores order. The second one does not.

The Standard Formula

The number of ways to choose r items from n distinct items is written as:

C(n, r) or "n choose r"

The usual formula is:

C(n, r) = n! / (r! * (n - r)!)

This works because n! effectively counts ordered selections, and dividing by r! removes the duplicate orderings inside each chosen group.

For example:

C(5, 3) = 10

So there are 10 different 3-person groups you can form from 5 people.

Why the Formula Works Conceptually

A useful way to derive the formula is to start with ordered selection.

The number of ordered ways to choose r items from n is:

n * (n - 1) * ... * (n - r + 1)

But each unordered group is counted multiple times, once for every possible internal ordering of its r members. There are r! such orderings, so dividing by r! removes that duplication.

This explanation is better than memorizing the formula mechanically, because it tells you what the denominator is fixing.

Compute It Safely in Code

Mathematically, factorials are fine. In code, direct factorial computation can be wasteful or overflow-prone in some languages. A multiplicative implementation is often better.

python
1def combinations(n, r):
2    if r < 0 or r > n:
3        return 0
4
5    r = min(r, n - r)
6    result = 1
7
8    for i in range(1, r + 1):
9        result = result * (n - r + i) // i
10
11    return result
12
13
14print(combinations(5, 3))
15print(combinations(52, 5))

The line r = min(r, n - r) uses the symmetry identity:

C(n, r) = C(n, n - r)

That reduces the number of loop iterations and keeps the arithmetic smaller.

Use Built-Ins When Available

In Python, the built-in choice is math.comb:

python
1import math
2
3print(math.comb(5, 3))
4print(math.comb(100, 2))

This is usually the cleanest solution unless the point of the exercise is to implement the logic yourself.

Edge Cases Matter

A few values are worth understanding explicitly:

  • 'C(n, 0) = 1'
  • 'C(n, n) = 1'
  • 'C(n, r) = 0 when r > n'

These are not arbitrary programming conventions. They follow from the meaning of the problem.

There is exactly one way to choose nothing, exactly one way to choose everything, and no way to choose more items than exist.

Where Combination Counts Show Up

Combination formulas appear in many real problems:

  • counting lottery tickets
  • computing poker hand totals
  • choosing feature subsets
  • evaluating probabilities in binomial-style models

For example, the number of 5-card hands from a standard 52-card deck is:

C(52, 5)

That is a classic combinations problem because the hand does not change when you reorder the cards.

Common Pitfalls

The most common mistake is using permutations when the problem is really combinations. If order does not matter, do not count rearrangements separately.

Another issue is applying the factorial formula directly in code for large values when a multiplicative method or built-in function would be more robust.

People also sometimes forget edge cases such as r > n, which should produce zero rather than a broken factorial expression.

Finally, many wrong answers come from skipping the verbal analysis. Always decide what the objects are, what counts as the same outcome, and whether order matters before reaching for a formula.

Summary

  • Combinations count selections where order does not matter.
  • The standard formula is C(n, r) = n! / (r! * (n - r)!).
  • The symmetry C(n, r) = C(n, n - r) simplifies many calculations.
  • In code, a multiplicative implementation or a built-in helper is usually better than raw factorial arithmetic.
  • The first step in any counting problem is deciding whether the problem is combinations or permutations.

Course illustration
Course illustration

All Rights Reserved.