PuzzleReels
Sections

The Two Eggs

The maths · Episode 29 · 2:32 · gives the answer

The puzzle

A building has 100 floors. Somewhere there is a lowest floor c from which an egg breaks when dropped: from any floor below c an egg survives, and from c or above it breaks. You do not know c, and it could be any floor from 1 to 100. You have 2 identical eggs. A broken egg is gone, and an egg that survives can be dropped again. You want to find c exactly. What is the fewest drops that is guaranteed to be enough, whatever c turns out to be?

The maths, properly

The count that matters is the worst case: the strategy is judged by the unluckiest c.

The first instinct: halve it

Bisection is the right idea with unlimited eggs, and with two it fails. Drop from floor 50. If the egg breaks you have one egg left, and c is between 1 and 50, and the only safe way to search with a single egg is to go up one floor at a time from the bottom: 1, 2, 3 and so on. In the worst case, c=49 or c=50, that is 1 + 49 = 50 drops. The script runs bisection with this fallback for every c and finds the worst case is 50, first reached at c=49.

Route one: blocks of ten

Drop from 10, 20, 30 and so on up to 100. When an egg breaks, you walk up the last block one floor at a time with the other egg. The worst case is a top block: ten drops to get there, and then nine more to walk, 91 to 99. That is 19. The cost is not even. If the egg breaks at the first block you spend about ten drops, and at the top block, nineteen. The unlucky cases are much more expensive than the lucky ones, and that unevenness is waste.

Route two: level it out

Suppose the worst case is to be n drops. Drop first from floor n. If the egg breaks you have one egg and floors 1 to n−1 to walk, so 1+(n−1)=n drops: right on budget. If it survives, you have spent one drop and have n−1 left, so the next jump can be n−1 floors, to floor n+(n−1). If that egg breaks, you have used two drops and walk n−2 floors, again 2+(n−2)=n. Each time you survive, you have one fewer drop, so each jump is one floor shorter, and every way the egg can break costs exactly n.

The jumps are n,n−1,n−2,…,1, and together they reach floor

n+(n−1)+…+1=n(n+1)2.

We need this to be at least 100. With n=13 it is 91, which is too small, and with n=14 it is 105, which is enough. So n=14.

The jump sizes are 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1. Adding them up gives the floors 14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99, 102, 104, 105. The ladder is 105 high and the building is only 100, so the film caps it at 100: the floors dropped from are 14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99, 100.

Here is each rung of the ladder, and the worst case if the egg breaks there:

drop fromjumpdrops so farfloors to walktotal if it breaks here
141411314
271321214
391231114
501141014
60105914
6996814
7787714
8478614
9069514
95510414
99411314
100112012

Every worst case is at most 14. The last rung, floor 100, is cheaper because the ladder was cut off at the top. The script runs the ladder for every one of the 100 possible values of c, checks that it finds c each time, and the most drops any of them needs is 14.

Why you cannot do better

Let f(d,e) be the most floors you can search with d drops and e eggs, so that you are sure to find c. Drop an egg from some floor.

  • If it breaks, c is at or below that floor. You have d−1 drops and e−1 eggs, which can sort out f(d−1,e−1) floors below.
  • If it survives, c is above it. You have d−1 drops and still e eggs, which can sort out f(d−1,e) floors above.
  • The floor you dropped from is one more.

So the most you can cover is

f(d,e)=f(d−1,e−1)+f(d−1,e)+1,

and no strategy can cover more than this, because each drop gives only two answers. With one egg, you can only walk up, so f(d,1)=d. With two eggs the recurrence gives f(d,2)=f(d−1,1)+f(d−1,2)+1=(d−1)+1+f(d−1,2), so f(d,2)=d+f(d−1,2), which adds up to d(d+1)2. This is the ladder's triangle. The script checks the formula for every d from 1 to 30.

For 100 floors we need the smallest d with d(d+1)2≥100. That is d=14, because 13×14/2=91. So 14 is the best possible, by the recurrence and not only by the ladder.

What changes if the numbers change

Other heights. The same count works for any building. The script checks it for three heights:

floorsfewest drops with two eggs
104
5010
10014
20020

More eggs. With three eggs, 100 floors need 9 drops, and with four eggs, 8. Each extra egg lets you search a coarser grid with the first drops, with a second egg to back it up.

Checking it

scripts/two-eggs.mjs asserts the claims below before it writes maths.json, and its --check fails if the file is stale. It checks that:

  • the recurrence gives 14 as the fewest drops for 2 eggs and 100 floors, with 4 for 10, 10 for 50 and 20 for 200 floors;
  • with two eggs the most floors covered by d drops is d(d+1)/2, for every d up to 30;
  • three eggs need 9 drops and four need 8;
  • the ladder, with jumps 14, 13, 12 and so on down to 1, finds every c from 1 to 100, and takes at most 14 drops;
  • blocks of ten take at most 19 drops, always finding c;
  • bisection from floor 50 takes at most 50 drops, and takes 50 when the first drop breaks the egg;
  • each rung of the ladder costs at most 14 drops in its worst case.

Where this shows up

  • Worst case against average case. The puzzle asks for a guarantee, so luck is not allowed to help. Planning against the worst case often means balancing the costs of the branches, as the ladder does.
  • Testing and searching with a limited budget of failures. Whenever a test destroys something, such as a crash test, a stress test or a fuse, you cannot search freely. The egg is the simplest model of it.
  • Triangular numbers. The sum 1+2+…+n appears whenever every step costs one less than the last.

Try it yourself

1. How many drops do two eggs need for a 10-floor building, and where do you drop first?

4 drops. The triangular number 10 is 4×5/2, so the first drop is from floor 4, then 7, then 9, then 10.

2. With the ladder, the first egg survives floors 14, 27 and 39 and breaks at 50. What does the second egg do, and how many drops is that in the worst case?

Four drops have been used, at 14, 27, 39 and 50. Floors up to 39 are safe, so c is between 40 and 50, and the second egg walks up from 40: floors 40 to 49, at most ten more drops. The worst case is 4 + 10 = 14, which is the budget; the table above shows it on the row for floor 50.

3. Why is the first drop at 14 and not at 50?

A drop at 50 that breaks leaves 49 floors to walk with one egg, which costs 50 drops in all. A drop at 14 that breaks leaves only 13 floors to walk, which costs 14 drops in all. The ladder is built so that every break costs the same.