Intellia Academy

Video lesson

Hilbert’s Hotel Infinite Buses: 3 Ways to Fit Infinite Passengers

The short answer

Hilbert's hotel infinite buses problem asks how to assign rooms when infinitely many buses, each carrying infinitely many passengers, arrive at a full hotel. Three methods work: prime exponents (injective but wasteful), digit interleaving (surjective but base-10 dependent), and Cantor's diagonal pairing function (a perfect bijection).

Watch the video

Hilbert’s hotel infinite buses is one of the most striking thought experiments in mathematics. The hotel is already full. An endless line of buses has arrived. Each bus carries an endless number of new passengers. How do you fit everyone in without turning anyone away?

This puzzle, rooted in Georg Cantor’s work on infinite sets, exposes the difference between three types of mathematical functions: injective, surjective, and bijective. Each method below teaches you something different about how infinity behaves — and one method alone achieves a perfect solution.

What Is Hilbert’s Hotel and Why Do Infinite Buses Make It Hard?

Hilbert’s hotel is a thought experiment where a hotel with infinitely many rooms (one for each positive integer) is completely full, yet can always accommodate more guests through clever reassignment. The infinite buses version is harder than the standard problem because it requires mapping a two-dimensional infinite grid — every possible (bus, seat) pair — onto a single infinite line of room numbers.

In the classic single-bus case, you shift every current guest from room n to room 2n, freeing up all odd-numbered rooms for the new arrivals. That trick works for one countably infinite group. It breaks down when you have infinitely many buses, each carrying infinitely many people, because shifting alone cannot compress a two-dimensional infinity into one dimension without a deliberate mapping strategy.

Formally, you need a function f(bus, seat) → room number that is:

  • Injective (one-to-one): no two passengers share a room.
  • Surjective (onto): every room is occupied.
  • Bijective: both at once — a perfect pairing with no gaps and no overlaps.

Does the Prime Exponent Method Solve Hilbert’s Hotel Infinite Buses?

The prime exponent method assigns each passenger a room using the formula 2seat × 3bus, and while it guarantees no two passengers ever share a room, it is not a complete solution because it leaves most rooms permanently empty. It is injective but not surjective.

Here is how it works. Treat current guests as bus 0. A guest in seat s on bus b gets room number 2s × 3b. Original guests (bus 0) land in rooms 2, 4, 8, 16, … — pure powers of 2. A passenger in seat 3 on bus 2 gets room 23 × 32 = 8 × 9 = 72.

The logic is airtight: the Fundamental Theorem of Arithmetic guarantees every positive integer has exactly one prime factorisation. So no two different (bus, seat) pairs can ever produce the same product. Collisions are impossible.

But check room 15. Its prime factorisation is 3 × 5. The formula only ever produces numbers whose prime factors are 2 and 3. The prime 5 never appears as a base, so room 15 can never be assigned. The same is true for every room number containing 5, 7, 11, or any other prime. In practice, a negligible fraction of rooms are used, and the hotel’s capacity is wasted on an astronomical scale.

Key insight: An injective mapping avoids collisions. It does not guarantee full occupancy. Injection and surjection are independent properties.

How Does Digit Interleaving Fill Every Room Sequentially?

Digit interleaving — sometimes called a digit-interleaving cipher — fills every room by weaving the digits of a passenger’s seat number and bus number into a single integer, producing a bijection in base 10 that assigns every positive integer to exactly one passenger and vice versa.

Take seat 1234 on bus 0789. Pad both to the same length and alternate their digits:

Position 1 2 3 4
Bus digit 0 7 8 9
Seat digit 1 2 3 4
Interleaved 01 72 83 94

The combined room number is 01728394. The process reverses cleanly: extract every other digit to recover the original bus and seat numbers. No information is lost, and every integer in the sequence corresponds to a unique passenger.

This solves the immediate problem. Every room gets a guest. Every guest gets a room. But there is a hidden limitation: the method is tied to base-10 notation. Change the number system and the interleaving scheme must be redesigned. It is a clever computational trick, not a universal mathematical truth. For a method that works independently of any number system, you need geometry.

In our experience teaching this material at Intellia Academy, students who understand why digit interleaving is base-dependent tend to grasp the significance of Cantor’s geometric solution much faster — because they already feel the need for something more fundamental.

What Is Cantor’s Diagonal Pairing Function and How Does It Achieve a Perfect Bijection?

Cantor’s diagonal pairing function is a geometric mapping that sweeps diagonally across an infinite two-dimensional grid of (bus, seat) coordinates, assigning each point a unique positive integer in a fixed sequence, producing a perfect bijection between the grid and the natural numbers without relying on any number base.

Visualise every possible passenger as a point on a grid. The horizontal axis is the seat number; the vertical axis is the bus number. Every passenger has a coordinate (bus, seat). The challenge is to trace a path through every point on this infinite grid exactly once.

Cantor’s path moves diagonally. It starts at (0,0), then sweeps along anti-diagonals:

  1. Diagonal 0: (0,0) → room 1
  2. Diagonal 1: (0,1), (1,0) → rooms 2, 3
  3. Diagonal 2: (0,2), (1,1), (2,0) → rooms 4, 5, 6
  4. Diagonal 3: (0,3), (1,2), (2,1), (3,0) → rooms 7, 8, 9, 10

The explicit formula for the room number of passenger at bus b, seat s is:

room = ½(b + s)(b + s + 1) + b + 1

Notice the term ½(b + s)(b + s + 1). That is a triangular number — the sum of the first (b + s) positive integers. In the geometric picture, each completed diagonal corresponds to one triangular number. The formula adds b + 1 to locate the exact position within that diagonal.

Try the worked example from the video: bus 2, seat 3.

  • b + s = 5
  • Triangular part: ½ × 5 × 6 = 15
  • Add b + 1 = 3
  • Room number: 15 + 3 = 18

Every positive integer appears exactly once in this sequence. Every (bus, seat) pair maps to exactly one room. That is a bijection — the mathematical gold standard for pairing two infinite sets of equal size.

What Is the Difference Between Injective, Surjective, and Bijective Mappings?

An injective mapping is one where no two inputs share an output; a surjective mapping is one where every output is used by at least one input; a bijective mapping satisfies both conditions simultaneously and proves two infinite sets have the same cardinality.

Property Prime exponents Digit interleaving Cantor diagonal
Injective (no collisions) ✓ ✓ ✓
Surjective (no empty rooms) ✗ ✓ ✓
Bijective (perfect pairing) ✗ ✓ (base-10) ✓ (universal)
Base-independent ✓ ✗ ✓

The Hilbert hotel puzzle makes these abstract definitions concrete. When your mapping is only injective, vast stretches of the hotel sit empty. When it is bijective, every integer is matched to every passenger in both directions. This is also how mathematicians prove that the set of all rational numbers is the same size as the set of all natural numbers — by exhibiting a bijection between them using exactly this diagonal argument.

If you enjoy these kinds of logical proof structures, you might also appreciate how necessary vs sufficient conditions appear in chessboard tiling proofs — another area where a condition can look convincing but still fall short of a complete solution.

How Does This Topic Appear in Math Competitions?

Hilbert’s hotel infinite buses connects directly to competition topics in combinatorics, set theory, and discrete mathematics. Contest problems rarely ask you to recite the Cantor formula, but they regularly test whether you can construct explicit bijections, prove that a mapping is or is not surjective, and reason about countable infinity.

Common competition appearances include:

  • Proving that a function between two infinite sets is a bijection.
  • Showing that ℕ × ℕ (all pairs of natural numbers) is countable.
  • Constructing an explicit one-to-one correspondence between the rationals and the integers.
  • Problems that ask why a clever-looking scheme fails to be surjective (a variant of the prime exponent trap above).

The birthday paradox is another probability result where intuition misleads you until you build the exact combinatorial count — the same discipline needed to verify whether a mapping truly covers all cases.

At the olympiad level, Ramsey theory and combinatorial reasoning share a deep connection with the ideas here: both fields ask when structure must emerge, and both demand careful proofs that a construction is complete rather than merely suggestive.

Students preparing for olympiads can explore Intellia Academy’s full range of foundational economics and mathematics courses to build the proof-writing fluency that competition judges look for.

Why Is Cantor’s Result About Countable Infinity So Surprising?

Cantor’s diagonal pairing function proves that the set of all pairs of natural numbers is exactly the same size as the set of natural numbers alone, which feels impossible but is a rigorous mathematical fact — because two infinite sets have the same cardinality if and only if a bijection exists between them.

This result shocked nineteenth-century mathematicians. Adding a second dimension of infinity to a first dimension of infinity still gives you the same infinity — what Cantor called ℵ₀ (aleph-null), the cardinality of the natural numbers. The hotel never runs out of room, no matter how many infinite buses arrive, because the two-dimensional grid of passengers is provably no larger than the one-dimensional line of room numbers.

What does produce a strictly larger infinity? Cantor showed separately that the real numbers cannot be put in bijection with the natural numbers — a different diagonal argument proves it. The real numbers form an uncountable infinity, ℵ₁, and no hotel trick can accommodate them. Understanding this boundary between countable and uncountable infinity is one of the deepest ideas in all of mathematics and appears in advanced olympiad problems and university entrance assessments alike.

For students interested in how these ideas connect to broader competition mathematics, the Ramsey number R(3,3) discussion at Intellia Academy shows another area where combinatorial thinking about structure leads to surprising, provable results.


Frequently Asked Questions

What exactly is the Hilbert’s hotel infinite buses problem?

It asks how to assign natural-number room numbers to every passenger across infinitely many buses, each carrying infinitely many people, when the hotel already has infinite guests. The solution requires a function that maps every (bus, seat) pair to a unique room number with no gaps — a bijection from ℕ × ℕ to ℕ. Cantor’s diagonal pairing function provides the universal answer.

Why does the prime exponent method fail to solve the problem completely?

Assigning room 2seat × 3bus is injective — no two passengers share a room — but it is not surjective. Room numbers that contain any prime factor other than 2 or 3 (for example, 5, 7, 15, 35) can never be assigned. The vast majority of rooms stay empty, so the hotel’s capacity is not truly used.

How do I calculate a room number using Cantor’s pairing function?

Use the formula: room = ½(b + s)(b + s + 1) + b + 1, where b is the bus number and s is the seat number. For bus 2, seat 3: b + s = 5, triangular part = ½ × 5 × 6 = 15, add b + 1 = 3, giving room 18. The formula works for any non-negative integer values of b and s.


Sources

Key takeaways
  • The prime exponent method 2^seat × 3^bus is injective — no collisions — but leaves most rooms empty because numbers with prime factors other than 2 and 3 are unreachable.
  • An injective mapping and a surjective mapping are independent properties; a function can satisfy one without satisfying the other.
  • Digit interleaving achieves a bijection by weaving seat and bus digits into one integer, but the method depends on base-10 notation and is not a universal mathematical truth.
  • Cantor's diagonal pairing function — room = ½(b+s)(b+s+1) + b + 1 — is a base-independent geometric bijection that maps every (bus, seat) pair to a unique room number with no gaps.
  • The fact that ℕ × ℕ bijects with ℕ proves that adding a second dimension of infinity does not produce a larger infinity; both sets share the same cardinality ℵ₀.
Full video transcript

Hilbert's hotel is already full, yet an infinite line of antique buses has just arrived, each carrying an endless number of new passengers. To fit everyone in, we can try an algebraic solution using prime numbers. Each passenger is assigned a room by calculating 2 to the power of their seat times 3 to the power of their bus. In this model, we treat original guests as bus 0, which maps them to simple powers of two. New arrivals use both the seat and bus values as exponents to find their specific room number. Because every integer has a unique prime factorization, no two combinations of seat and bus will ever produce the same result. The room assignments will never overlap. This collision-free state is what mathematicians call an injective mapping.

While the algebra works, this method creates a massive, invisible problem. By relying solely on primes 2 and 3, we are leaving the vast majority of the hotel empty. If we check the registry for room 15, we find it has no occupant. The number 15 is the product of 3 and 5. Because our formula only uses powers of 2 and 3, the prime factor of 5 is mathematically unreachable. This gap isn't an isolated case. Every room number that includes any other prime — like 5, 7, or 11 — remains a void. We've avoided collisions, but we've wasted millions of rooms. This proves that an injective mapping is not necessarily surjective. We aren't actually using the hotel's full capacity.

To truly solve infinity, we must find a method that leaves no gaps. We need to move beyond prime exponents. Our second attempt uses an algorithmic approach. We designate the current guests as bus 0 to ensure they are integrated into the new numbering sequence from the start. We use a digit interleaving cipher. It takes a passenger's seat and bus coordinates and physically weaves them into a single integer. If we take seat 1234 on bus 0789, we stack the numbers and alternate their digits. The result is a single zipped room number, 01728394. We can reverse the process just as easily. Extracting the alternating digits makes the original seat and bus numbers reappear. This method fills every room sequentially. Every integer corresponds to a passenger, and every passenger has an integer.

But this trick is tied to the rules of our base-10 number system. To find a universal truth, we need a solution based on pure geometry. We can visualize infinity not as a string of digits, but as a structural map. First, we place original guests into rooms that correspond to triangular numbers. This fills rooms 1, 3, 6, and 10, creating a geometric skeleton for our hotel. The infinite buses and seats are then plotted on a two-dimensional grid. Every possible passenger now has a specific coordinate. To assign the remaining rooms, we use Cantor's diagonal pairing function. This method sweeps across the coordinate grid, assigning a unique number to every point in a precise sequence. This geometric path targets the spaces left empty by our triangular numbers. It maps every bus and seat combination to a vacant room, without skipping a single integer.

By utilizing spatial geometry instead of arithmetic tricks, we create a blueprint that accounts for every dimension of the problem. The two-dimensional grid of buses and seats begins to fold — it is physically collapsing into a single infinite line. The Cantor formula is designed to map passengers to the exact room numbers left vacant by the triangular sequence. As these two sets of values combine, they fill every consecutive integer from one to infinity without exception. This state, where every room is filled and no guest is duplicated, is a perfect bijection. This geometric arrangement ensures that every passenger, no matter how deep their position in the grid, has a predictable, balanced location. Within this structure, the overwhelming scale of the buses and seats is resolved into a single, calculable address for every individual. Through this lens, mathematics maps an entire two-dimensional field into a single, manageable sequence.

Now, try to apply this geometry yourself. If you use the pairing function for bus 2, seat 3, what is their exact room number? Calculate the answer and leave it in the comments.

MA

Mathematics Team

Mathematics Team · Intellia Academy

Mathematics Team at Intellia Academy — instructors and former olympiad medalists who teach this track and mark real contest papers. All articles

Concepts and lessons, wherever you learn

Deep dives on YouTube, quick concepts on Instagram and X, longer conversations on the podcast.

Subscribe on YouTube

Try a free lesson

No credit card required.

Start free lesson
Scroll to Top