The Pascal Puzzles: The Champagne Tower

Contents
A champagne tower of 55 glasses needs 1,023 glasses of champagne before its last glass is full, and 968 of them end up on the table. Pour in exactly the 55 it holds, and only 37 glasses are full.
This is Part 1 of the Pascal puzzles, a series in the manner of the cleaning robot puzzle: one small object, and rules that change one at a time, each change turning the object into a different classic problem. The object here is Pascal’s triangle. This part starts from a puzzle many programmers meet in interviews, and changes one rule of the triangle: a glass can hold only one unit.
The new ideas:
- Pascal’s triangle and binomial coefficients, from nothing, as the flow through a tower of bottomless glasses;
- simulation row by row, the interview answer;
- bisection on a function that only ever rises, to turn “how full is this glass?” into “how much must I pour?”;
- a formula that subtracts what the glasses above keep, and the exact point where it stops being enough;
- an event-driven simulation in exact fractions, which finds the moment every glass fills;
- what doubles get wrong: a tie they break, and a near-tie that a careless tolerance merges.
| File | What it does |
|---|---|
champagne.py | towers of any shape, pouring a fixed amount, the formula, bisection and the fill events |
test_champagne.py | the checks and the report behind every number below |
check_figures.py | runs the figures’ own arithmetic in Node and compares it with champagne.py |
Everything is standard-library Python. The files are listed in the code’s README.
The puzzle
Glasses are stacked in a triangle: one glass on top, two below it, three below those, and so on. Each glass rests on the two glasses beneath it. Champagne is poured into the top glass. A glass holds one unit. When it is full, whatever more it receives runs over its rim and is shared equally by the two glasses it rests on. A glass in the bottom row has nothing beneath it, so what runs over it falls onto the table.
Rows and glasses are numbered from 0: glass is the glass in row , places from the left, so the top glass is and row holds glasses to . Glass rests on and .
The interview version, which LeetCode carries as problem 799, “Champagne Tower”, asks one question: after pouring a given amount, how full is a given glass? This part asks the questions that come after it. How much must be poured to fill a given glass? In what order do the glasses fill? And how much ends up on the table?
Try it first. Pour, step from one fill to the next, and select a glass to see where its champagne comes from. The last line under the tower will make sense a few sections from now.
Pour into the top glass
Each glass holds one; whatever runs over is shared equally by the two glasses below it, and the bottom row spills onto the table. Press Next glass fills to jump from one fill to the next, and select a glass to see where its champagne comes from.
Bottomless glasses: Pascal’s triangle
Start with the rule the puzzle changes. Suppose the glasses had no bottoms, so that every glass passed on everything it received, half to each glass below it. Pour 16 units into the top and follow them down:
row 0: 16
row 1: 8 8
row 2: 4 8 4
row 3: 2 6 6 2
row 4: 1 4 6 4 1
Each number is half of the one above it to the left, plus half of the one above it to the right. Multiply row by , which undoes the halving, and the rows are ; ; ; ; . That is Pascal’s triangle: every entry is the sum of the two above it, with a 1 at each end.
There is a second way to see the same numbers, which explains why they appear here. Every drop that reaches glass took a path down the tower, turning left or right at each of the rows above it, and it turned right exactly times. Each turn halves the amount, so each path carries of the pour. The number of such paths is the number of ways to choose which of the turns go right, which is written , read “ choose ”, and called a binomial coefficient. So with bottomless glasses
where is the amount poured. Pascal’s rule, that each entry is the sum of the two above it, is the same statement counted at the last turn: a path to arrives either from or from .
test_champagne.py checks it by pouring into glasses that hold nothing, for four amounts on 12 rows, and every glass receives exactly .
This is the whole of Pascal’s triangle that this part needs. The puzzle adds one thing to it: a bottom.
One unit per glass
With a bottom, a glass keeps up to one unit, and only the excess runs on. That changes a single line of the bottomless version: half of everything becomes half of max(0, amount - 1). Here is the interview answer, which computes what reaches each glass one row at a time:
def champagne_tower(poured, query_row, query_glass):
flow = [poured] # what reaches each glass of the current row
for _ in range(query_row):
below = [0.0] * (len(flow) + 1)
for c, amount in enumerate(flow):
extra = max(0.0, amount - 1) / 2
below[c] += extra
below[c + 1] += extra
flow = below
return min(1.0, flow[query_glass])
print(champagne_tower(2, 1, 1), champagne_tower(10, 3, 1), champagne_tower(10, 3, 0))
0.5 1.0 0.375
Pour 2, and the top glass keeps 1 and sends half a unit to each glass below it. Pour 10, and glass is full, while the edge glass holds three eighths: the top glass passes 4.5 down each side, the edge glass of row 1 keeps 1 and passes 1.75, the edge glass of row 2 keeps 1 and passes 0.375. The work is one pass over the rows down to the glass asked about, for row .
That max is the whole difference from Pascal’s triangle, and it is enough to break everything that made the triangle easy. Pascal’s flow is linear: pour twice as much and every glass receives twice as much, so one formula covers every pour. With a capacity, pouring 2 sends half a unit to glass and pouring 4 sends a unit and a half, not one. There is no longer a single formula, so answering for another pour means running the simulation again.
Turning the question round
The interview question goes from an amount poured to how full a glass is. The question a host would ask goes the other way: how much do I pour to fill that glass?
One fact makes it answerable. Pouring more never makes a glass emptier. What reaches row 0 is the pour itself, and what reaches each glass of the next row is a sum of halves of max(0, x - 1), where each reaches a glass in the row above. That function never decreases as grows, so if every inflow in one row rises with the pour, so does every inflow in the next. By induction down the rows, every glass’s level is a non-decreasing function of the pour.
A function that only rises can be inverted by bisection. Double the pour until the glass is full, so that the answer lies between the last two amounts, then repeatedly try the midpoint and keep whichever half still contains the answer:
def fill_pour(row, glass, halvings=60):
lo, hi = 0.0, 1.0
while champagne_tower(hi, row, glass) < 1: # double until the glass is full
hi *= 2
for _ in range(halvings): # then halve the gap
mid = (lo + hi) / 2
if champagne_tower(mid, row, glass) < 1:
lo = mid
else:
hi = mid
return hi
print(fill_pour(3, 1), fill_pour(9, 4), fill_pour(9, 0))
8.333333333333334 33.333333333333336 1023.0
Glass fills at eight and a third, the middle of row 9 at thirty-three and a third, and its edge at 1,023; bisection reports the top of its final interval, so the first two print as the double just above. Each answer costs about seventy simulations.
Bisection has two limits, and both matter here. The first is that it returns an interval, not a number. The answer for is exactly , and no number of halvings will ever land on a third, because halving only ever produces fractions whose denominator is a power of two. Every midpoint bisection tries is such a fraction, so it can land exactly on answers like 3 and 7, but even with exact arithmetic the best it can do with is trap it: in the tests, 50 halvings in Python’s Fraction bracket every fill pour of a six-row tower. The second limit is that bisection answers one glass at a time and says nothing about the order the glasses fill in, or which of them fill together. To get those, the rest of this part looks at what pouring actually does.
The edges
Some answers need no search at all. An edge glass rests under only one glass, the edge glass of the row above, so everything it receives comes down one side of the tower. For glass to be full, the edge glass above it must have received 1 for itself plus 2 to send down, half of which is the 1 needed: 3 in all. That one must in turn have received 1 plus twice 3, which is 7. Each step up doubles the need and adds one, so the need runs 1, 3, 7, 15, …, and
The tests check it for every row up to 29. For a tower of 10 rows, the corners of the bottom row are the last glasses to fill, after . The tower holds , so the other 968 are on the table.
The middle of the same row filled at . Why the edges need thirty times as much is the subject of the rest of this part.
Pascal minus what the glasses above keep
The capacity broke the formula, but not as badly as it seems. Look at any glass. What it passes down is what it received minus what it keeps:
This is true of every glass, full or not. A full glass keeps 1 and passes the rest; a glass that isn’t full keeps everything and passes nothing, and then in and level are equal. If we knew the levels, the tower would be linear again: the pour spreads down as in Pascal’s triangle, and each unit kept by a glass is a unit that would have spread down from that glass as in Pascal’s triangle and now doesn’t. So
where the sum runs over every glass above from which champagne can reach it, and is the share of a unit at that would reach : Pascal’s triangle again, started from . In words: each glass receives Pascal’s share of the pour, minus Pascal’s share of everything the glasses above it hold back. That is the decomposition the figure shows when you select a glass.
champagne.kept_formula computes the right-hand side from the levels, and the tests compare it with the simulation, exactly, for 40 random pours on 12 rows: equal every time.
The formula needs the levels, and the levels are what we are trying to find. But for one glass at the moment it fills, it is tempting to guess them: by the time a glass deep in the tower fills, surely every glass above it that can reach it is already full? Set all those levels to 1, set the inflow to 1, and solve for :
For glass the glasses above it are , , , and , and the sum is , divided by : , the number bisection could only approach. The same formula gives 1, 3, 5, 7 and the middle of row 4, 11, all exactly.
Then it gives 14 for glass , and the true answer is . The guess was wrong: when fills, the edge glass above it is not full yet. The edges fill late, at 15 for row 3, and by glass holds . It keeps less than the formula assumed, half of that comes down to , and since gets a quarter of each extra unit poured, that is worth of pour: . Over 30 rows, the formula is exact on the 66 glasses whose ancestors are all full when they fill, and too high on the other 399.
So the order in which glasses fill decides the answer, and finding the order means following the pour.
Pouring as a sequence of events
Pour steadily, one unit at a time, and watch the rates. The top glass receives 1 for each unit poured. A glass that isn’t full keeps everything it receives, so it passes nothing. A full glass passes everything, half to each glass below it. So the rate at which every glass fills depends only on which glasses are full, and that changes only at the moment another glass fills. Between two such moments every rate is constant, and every level rises in a straight line.
That makes the whole pour a short list of events. At each one:
- work out every glass’s rate from the set of full glasses, top down;
- for every glass that is receiving champagne and isn’t full, the pour still needed to fill it is its room left divided by its rate; the smallest of these is the next step;
- advance every level by its rate times the step, mark the glasses that reached 1 as full, and repeat.
In exact fractions, every step and every level stays a fraction, and glasses that fill at the same moment are found together, because their times are equal, not merely close:
from fractions import Fraction
def fill_events(rows, number=Fraction):
glasses = [(r, c) for r in range(rows) for c in range(r + 1)]
level = {g: number(0) for g in glasses}
full, poured, events = set(), number(0), []
while len(full) < len(glasses):
rate = {g: number(0) for g in glasses}
rate[(0, 0)] = number(1)
for r, c in glasses: # top down, so each rate is final when used
if (r, c) in full and r + 1 < rows:
rate[(r + 1, c)] += rate[(r, c)] / 2
rate[(r + 1, c + 1)] += rate[(r, c)] / 2
waiting = [g for g in glasses if g not in full and rate[g] > 0]
step = min((1 - level[g]) / rate[g] for g in waiting)
filled = [g for g in waiting if (1 - level[g]) / rate[g] == step]
for g in waiting:
level[g] += rate[g] * step
poured += step
full.update(filled)
events.append((poured, filled))
return events
for poured, filled in fill_events(5):
print(poured, filled)
1 [(0, 0)]
3 [(1, 0), (1, 1)]
5 [(2, 1)]
7 [(2, 0), (2, 2)]
25/3 [(3, 1), (3, 2)]
11 [(4, 2)]
41/3 [(4, 1), (4, 3)]
15 [(3, 0), (3, 3)]
31 [(4, 0), (4, 4)]
The tower does not fill row by row. The middle of row 4 fills at 11, and both of its neighbours at , before the edges of row 3 at 15. The edges are the last glasses in every row, and they hold the tower’s last word: the bottom corners, at .
Each event fills at least one glass, so there are at most as many events as glasses, for rows, and each costs a pass over all of them: , or . That sounds bad and isn’t. Thirty rows, 465 glasses and 239 events take a tenth of a second in exact fractions, and the fractions stay small: the largest denominator in 30 rows is 18,930,513. champagne.fill_events is the same algorithm for a tower of any shape, which the last sections use.
Because the answers are exact, they can be checked exactly rather than approximately. For every glass of a 12-row tower, the tests pour exactly its fill pour and find that it has received exactly 1, then pour a billionth less and find it short. Here is every glass of the 10-row tower:
| Row | Fill pours, left to right |
|---|---|
| 0 | 1 |
| 1 | 3, 3 |
| 2 | 7, 5, 7 |
| 3 | 15, 25/3, 25/3, 15 |
| 4 | 31, 41/3, 11, 41/3, 31 |
| 5 | 63, 22, 77/5, 77/5, 22, 63 |
| 6 | 127, 183/5, 109/5, 93/5, 109/5, 183/5, 127 |
| 7 | 255, 311/5, 31, 403/17, 403/17, 31, 311/5, 255 |
| 8 | 511, 105, 226/5, 779/25, 467/17, 779/25, 226/5, 105, 511 |
| 9 | 1023, 1269/7, 1853/27, 2253/55, 100/3, 100/3, 2253/55, 1853/27, 1269/7, 1023 |
The whole pour is 29 events. When the middle of the bottom row fills, at , 31 of the 55 glasses are full, and nothing has reached the table yet. Pour in the 55 the tower holds and 37 glasses are full, 40.703125 is in glasses, and the other 14.296875 is on the table, all of it run off the full glasses in the middle of the bottom row while its edges are still dry.
What doubles get wrong
The interview version computes in floating point, and so does almost every program that would ever run this. The event simulation takes the number type as a parameter, so the two can be compared directly. Here are the events between 30 and 32 in a 10-row tower, first in exact fractions, then in doubles:
for number in (Fraction, float):
for poured, filled in fill_events(10, number):
if 30 < poured < 32:
print(repr(poured), filled)
Fraction(31, 1) [(4, 0), (4, 4), (7, 2), (7, 5)]
Fraction(779, 25) [(8, 3), (8, 5)]
30.999999999999996 [(7, 2), (7, 5)]
31.0 [(4, 0), (4, 4)]
31.159999999999997 [(8, 3), (8, 5)]
At exactly 31 poured, four glasses fill at once: the edges of row 4, which need , and glasses and , which happen to need exactly 31 as well. In doubles they come apart. The edges’ arithmetic is halving and subtracting one, which binary does exactly, so they still fill at 31. The path to runs through moments like and levels like , which binary cannot hold exactly, and the rounding lands it one unit in the last place below 31. On a 30-row tower the doubles are never far off: the largest relative error in any fill pour is , about the best a double can do. But there are 240 events instead of 239. Any code that asks “do these glasses fill together?”, or sorts glasses by when they fill, gets a different answer from the exact one, for a difference no output with fewer than 16 digits would show.
The figures on this page compute in doubles, so they have to decide what counts as “together”. The first version said: glasses within a billionth (relative) of the next fill, fill with it. The figure check (check_figures.py, which runs the figures’ own JavaScript in Node and compares it with the exact Python) passed on 10 and 30 rows and failed on 40. There, glass fills at , and the edges of row 33 at . These are two different moments, 0.12 apart, but in a number of seventeen billion that is 7 parts in a trillion, well inside a billionth, so the figure merged them. The tolerance now is : several hundred times the rounding the doubles show (at most ), and seventy times smaller than the closest pair of distinct fills in 40 rows. With it, all 419 events of 40 rows are grouped exactly as the fractions group them.
The general lesson is an old one. A tolerance has to lie between two things: the error the arithmetic makes, and the smallest real difference in the data. Both had to be measured here, and the second could only be measured with exact arithmetic.
The middle and the edges
The edges need , which doubles with every row. In every row up to 99, the first glass to fill is the middle one (or the middle two), and it needs far less:
How much must be poured before a row's first and last glass are full
The vertical scale multiplies by 100 at each grid line. Hover over the chart, or focus it and use the arrow keys, to read one row.
| Row | The middle fills at | Glasses above the row | The edges fill at |
|---|---|---|---|
| 11 | 43.95 | 66 | 4,095 |
| 20 | 101.80 | 210 | 2,097,151 |
| 39 | 274.83 | 780 | 1,099,511,627,775 |
| 99 | 1,100.55 | 4,950 | about |
The middle of row 11 fills before even 44 glasses have been poured, with 66 glasses above it. There isn’t enough champagne in the tower to fill more than 43 of them, so at least 23 glasses above it are still not full: the pour goes down the middle and leaves the sides behind. And the middle’s fill pour grows much more slowly than the number of glasses above it. Fitted on rows 50 to 99 (computed in doubles; wherever both were run, up to 40 rows, the exact and double fill pours agree within ), the fill pour grows like , and divided by it is 1.142 at row 25, 1.118 at row 50 and 1.117 at row 99. The middle needs about ; the edges need .
Where does the come from? Look at the tower at the moment the middle of row 99 fills. 1,064 glasses are full, and they hold almost all of the 1,100.55 poured, with the rest in glasses still filling. The full glasses form a narrow lens down the middle: 5 of them in row 4, 11 in row 16, 13 in rows 36 and 64, then narrowing to the 2 in row 99. A lens about a hundred rows long and no more than 14 glasses wide. If its width grows like , as the spread of Pascal’s triangle does (the binomial coefficients of row are concentrated within about of the middle), then the glasses in it number about , and so does the pour. That is a reason the fit is plausible, not a proof; the exponent above is measured, and no claim here rests on the reason.
Champagne poured into a tower goes where Pascal’s triangle sends it, to the middle. The edges of row receive only of what flows through their rows, and they have to wait until everything above them is full and overflowing.
Glasses of other sizes
Nothing in the event simulation depends on the glasses holding one unit. champagne.py takes a capacity for every glass, and a tower is just a list of glasses and, for each one, the glasses below it with their shares. Two checks from the tests: make every glass twice as big and every fill pour doubles, as it must, since the whole pour scales. Make only the bottom row of a 10-row tower half-size, and its corners fill at instead of 1,023: the edge chain needs one unit less at each level, and the halving above it turns that into saved.
What this part showed
- Pascal’s triangle is the flow through bottomless glasses. Glass receives , because paths lead to it and each carries .
- A capacity makes the tower nonlinear. The interview question is a short simulation; the inverse question needs a search.
- Bisection inverts anything that only rises, but it returns an interval, and halving can never land on a third.
- Each glass receives Pascal’s share of the pour, minus Pascal’s share of what the glasses above it keep. If every glass above a glass were full when it filled, that would give its fill pour in closed form; the edges fill too late for that, and the formula is too high from glass on.
- The pour is a sequence of events, with every rate fixed in between, and following them in exact fractions finds every fill pour, the fill order and every tie.
- Doubles are accurate and still wrong about ties. Four glasses fill at exactly 31; doubles fill two of them a step early. And a tolerance has to sit between the rounding error and the smallest real gap, which here was 7 parts in a trillion.
- The middle fills like , the edges like . The pour runs down a narrow lens in the middle of the tower.
Later parts keep Pascal’s triangle and change other rules: the arithmetic, the question, and the rule that builds the triangle.
A note on the evidence
Run, not argued. Every number in this post is printed by test_champagne.py, whose checks all pass, run on Python 3.15.2 on Windows 11 on 26 November 2026. The figures and the cover compute their own numbers in JavaScript, from one block of code that is byte-identical in all three. check_figures.py runs that block in Node v26.13.1 and compares it with the exact Python: every fill pour of towers of 3 to 10, 30 and 40 rows agrees within , every fill event holds the same glasses, and the decomposition under the first figure equals the inflow on 2,000 random pours. Every Python block on this page was copied out of the published Markdown and run, in order, and printed exactly the output shown under it.
Exact, fitted and explained. The fill pours, the edges, the formula and its failures, the ties and the square towers are exact, in Python’s Fraction, and checked exactly. The growth of the middle, , is a least-squares fit to fill pours computed in doubles on rows 50 to 99. The lens is measured; the reason it gives for the is a heuristic and is labelled as one.
Timings. The tenth of a second for 30 rows and 2.5 seconds for 100 rows in doubles are single runs on one laptop, there to say the method is fast enough, not to be compared with anything.
The model is the puzzle’s. Glasses that hold exactly one unit and split their overflow exactly in half are the interview problem, not fluid dynamics. Real champagne runs over the rim and down the outside of the glass, and no number here is a claim about a real tower.
What I found on the way, and would still change. The figure’s first tolerance merged two different fills at 40 rows; the figure check caught it before publishing, and the section on doubles tells how. The event simulation recomputes every rate after every event, which is . Only the glasses below the one that filled can change rate, so a version that updated just those, with the waiting glasses in a priority queue, would be faster; at the sizes here, nothing needed it.
The Pascal Puzzles: The Champagne Tower