Skip to content
kd
6 Jun 2020
Back to blog

The egg tower puzzle

13 min read (2,525 words)

Table of contents

Here is a fun puzzle:

You are given two eggs, and access to a 100-storey tower. Both eggs are identical. The aim is to find out the highest floor from which an egg will not break when dropped out of a window from that floor. If an egg is dropped and does not break, it is undamaged and can be dropped again. However, once an egg is broken, that's it for that egg.

If an egg breaks when dropped from a floor, then it would also have broken from any floor above that. If an egg survives a fall, then it will survive any fall shorter than that.

The question is: What strategy should you adopt to minimize the number egg drops it takes to find the solution? (And what is the worst case for the number of drops it will take?)

Once you solved it for two eggs, can you solve it for three eggs? Can you find a generalized solution when you have a tower with xx floors and if you have NN eggs?

If you are interested in an easy to understand intuitive explanation to the 22 egg 100100 storey tower problem, check out the following video:

https://www.youtube-nocookie.com/embed/NGtt7GJ1uiM

We will go through this problem for the 22 eggs and 33 eggs variant, and derive the solution mathematically. Then, we will generalize the problem with xx floors and NN eggs.

Two eggs

We have 22 eggs to start with and a 100100 storey tower to explore.

Let's consider what we would do if we had just one egg.

With just one egg, we could drop the egg from floor 11. If it breaks, we stop and can definitively say that for any floor 11 and above all eggs would break. If it doesn't break, we can proceed to floor 22 and repeat.

This means that for a tower with 100100 floors we will need up to 100100 drops to definitively say at which floor the egg would break. The above strategy gives us the minimum number of drops to guarantee that we find at which floor the egg would break. Or in other words, we can search NN floors with NN drops. Let's call the lowest floor in the tower at which a egg would break the breaking floor. Here, NN is the breaking floor and N1N-1 is the highest floor from which an egg will not break. Once we can find the breaking floor, we can find the solution to this puzzle. The aim of this puzzle is to minimize the worst case when trying to guarantee finding the breaking floor.

What could we do differently with 22 eggs?

With 22 eggs, in fact we can explore the search space more efficiently. We can use the first egg to partition the total number of floors, and once the first egg breaks we can use the second egg to search floors within the last partition.

As an example, let's say you decide to drop the first egg from every 10th10^{th} floor. If it doesn't break at floor 1010, 2020, 3030, 4040 or 5050 but breaks at floor 6060 you can use the second egg to explore floors 5151 - 5959. If it finally breaks at floor 5959, you would have used 66 drops of the first egg and 99 drops of the second egg to find the breaking floor.

We can do even better though.

Let's assume that the minimum number of drops required to guarantee finding the breaking floor for a NN storey tower is xx. If when we drop the first egg and the egg breaks, we can use x1x - 1 drops of the second egg to find the breaking floor. We already know that we can search at most x1x - 1 floors with the 11 egg using x1x - 1 drops. So if our first drop was from a floor greater than xx, we would not be able to guarantee finding the solution to this problem. This means that if we have 22 eggs we would want to drop the first egg from floor xx, where xx is also the minimum number of drops that will guarantee finding the breaking floor in a NN storey tower.

When we drop the first egg from floor xx, if the egg breaks, we can use the second egg to find which floor from 11 to x1x - 1 is the solution. If the egg doesn't break, now we have used 11 drop. Let's assume that the first egg breaks on the second drop. When this happens, we can use the remaining egg to explore x2x - 2 floors with x2x - 2 drops find the breaking floor. With x2x - 2 drops, we can search from floor x+1x + 1 to floor x+(x2)x + (x - 2). This means that when we drop the first egg the second time, we should start from x+(x2)+1x + (x - 2) + 1, i.e., x+(x1)x + (x - 1), to allow for finding the breaking floor.

If the egg doesn't break on the second drop, we have now used 22 drops. If the egg is going to break on the third drop, we have to allow for searching x3x - 3 floors with the second egg. This means we should drop the first egg on our third attempt from floor number x+(x1)+(x3)+1x + (x - 1) + (x - 3) + 1, i.e., x+(x1)+(x2)x + (x - 1) + (x - 2). We can also say that with 33 drops and 22 eggs we are guaranteed to find the floor if it is within the first 3+(31)+(32)3 + (3 - 1) + (3 - 2) floors, i.e. 66 floors.

Seeing the pattern here?

For a NN storey tower, we need to ensure that with xx drops we cover all the floors of the tower. That gives us this constraint.

x+(x1)+(x2)+(x3)++1>=Nx + (x - 1) + (x - 2) + (x - 3) + \ldots + 1 >= N

Which can be rewritten as:

k=1xk>=N\sum_{k=1}^{x} k >= N

Or, in closed form as:

x×(1+x)2>=N\frac{x \times (1 + x)}{2} >= N

We know NN is 100100 in this problem.

x×(1+x)2>=100\frac{x \times (1 + x)}{2} >= 100

This tells us that with 1414 drops we can guarantee finding the breaking floor in a tower with up to 105105 floors.

Three eggs

We have already previously established the best strategy for 11 egg and 22 eggs.

Let's define some terminology:

f1(x)=xf_1(x) = x f2(x)=x×(1+x)2f_2(x) = \frac{x \times (1 + x)}{2}

where f1(x)f_1(x) and f2(x)f_2(x) are the number of floors that can be checked with 11 and 22 eggs respectively with xx drops.

And we know that f2(x)f_2(x) can be written as:

f2(x)=k=1xkf_2(x) = \sum_{k=1}^{x} k

So if we have 3 eggs and if the first egg breaks on the first drop, we want to ensure that we can check f2(x1)f_2(x - 1) floors with the remaining x1x - 1 drops. That means we should drop the first egg from floor:

1+f2(x1)1 + f_2(x - 1)

For the second drop, we can start from floor:

1+f2(x1)+1+f2(x2)1 + f_2(x - 1) + 1 + f_2(x - 2)

For the third drop, we can start from floor:

1+f2(x1)+1+f2(x2)+1+f2(x3)1 + f_2(x - 1) + 1 + f_2(x - 2) + 1 + f_2(x - 3)

Seeing a pattern emerge again?

Similar to before, the total number of floors we can check is constrained by NN:

1+f2(x1)+1+f2(x2)+1+f2(x3)++1>=N1 + f_2(x - 1) + 1 + f_2(x - 2) + 1 + f_2(x - 3) + \ldots + 1 >= N

This can be rewritten as:

k=1x1(1+f2(k))+1>=N\sum_{k=1}^{x-1} \left( 1 + f_2(k) \right) + 1 >= N

1+j=1x1(1+k=1jk)>=N1 + \sum_{j=1}^{x-1} \left( 1 + \sum_{k=1}^{j} k \right) >= N

x+j=1x1k=1jk>=Nx + \sum_{j=1}^{x-1}\sum_{k=1}^{j} k >= N

which results in:

x3+5x6>=N\frac{x^3 + 5x}{6} >= N

Solving this, we get 99 drops for 33 eggs. With just 99 drops, we can guarantee finding the breaking floor in a 129129 storey tower.

NN eggs

Let's see if we can generalize this for NN eggs.

With 11 egg and xx drops, we know we can check xx floors. Let's define this as f(x,1)f(x, 1), i.e.:

[f(x,1)=x(1)f(x, 1) = x \qquad(1)]{#eq-f_x_1}

where f(x,n)f(x, n) is the floors that can be checked with xx drops and nn eggs.

We can safely say that with 00 drops or 00 eggs we can check 00 floors.

f(0,n)=f(x,0)=0f(0, n) = f(x, 0) = 0

So we can write Equation 1{.quarto-xref} as the following:

f(x,1)=1+f(x1,1)+f(x1,0)f(x, 1) = 1 + f(x-1, 1) + f(x-1, 0)

For 22 eggs and xx drops, the number of floors we can check, i.e. f(x,2)f(x, 2), is:

[f(x,2)=k=1xk(2)f(x, 2) = \sum_{k=1}^{x} k \qquad(2)]{#eq-f_x_2}

We can expand this as the following:

f(x,2)=1+k=1x1k+x1f(x, 2) = 1 + \sum_{k=1}^{x-1} k + x - 1

and substitute in Equation 1{.quarto-xref} to get:

f(x,2)=1+f(x1,2)+f(x1,1)f(x, 2) = 1 + f(x-1, 2) + f(x-1, 1)

And, for 33 eggs and xx drops, the number of floors we can check, i.e. f(x,2)f(x, 2), is:

f(x,3)=x+j=1x1k=1jkf(x, 3) = x + \sum_{j=1}^{x-1}\sum_{k=1}^{j} k

Again, we can expand it and rearrange some terms:

f(x,3)=x+j=1x2k=1jk+k=1x1kf(x, 3) = x + \sum_{j=1}^{x-2}\sum_{k=1}^{j} k + \sum_{k=1}^{x-1} k

f(x,3)=1+x1+j=1x2k=1jk+k=1x1kf(x, 3) = 1 + x - 1 + \sum_{j=1}^{x-2}\sum_{k=1}^{j} k + \sum_{k=1}^{x-1} k

and substitute in Equation 2{.quarto-xref} to get:

f(x,3)=1+f(x1,3)+f(x1,2)f(x, 3) = 1 + f(x - 1, 3) + f(x - 1, 2)

We can see a pattern emerging here. The generalized equation can be written like so:

[f(x,n)=1+f(x1,n)+f(x1,n1)(3)f(x, n) = 1 + f(x - 1, n) + f(x - 1, n - 1) \qquad(3)]{#eq-f_x_n}

This is also known as a recurrence relation.

This result can be reasoned through intuition as well. To find the total floors you can check with nn eggs and xx drops, it will be 11 (your first drop) plus the maximum number of floors you can check with nn eggs and x1x-1 eggs (if the first egg does not break) plus the maximum number of floors you can check with n1n-1 eggs with x1x-1 eggs (if the first egg does break).

If we wanted to, we could expand this recurrence relation1.

Here is the expansion increasing the depth in the direction of number of drops:

f(x,n)=1+f(x1,n)+f(x1,n1)f(x, n) = 1 + f(x - 1, n) + f(x - 1, n - 1)

f(x,n)=3+f(x2,n)+2f(x2,n1)+f(x2,n2)f(x, n) = 3 + f(x - 2, n) + 2f(x - 2, n - 1) + f(x - 2, n - 2)

f(x,n)=7+f(x3,n)+3f(x3,n1)+3f(x3,n2)+f(x3,n3)f(x, n) = 7 + f(x - 3, n) + 3f(x - 3, n - 1) + 3f(x - 3, n - 2) + f(x - 3, n - 3)

f(x,n)=15+f(x4,n)+4f(x4,n1)+6f(x4,n2)+4f(x4,n3)+f(x4,n4)f(x, n) = 15 + f(x - 4, n) + 4f(x - 4, n - 1) + 6f(x - 4, n - 2) + 4f(x - 4, n - 3) + f(x - 4, n - 4)

f(x,n)=31+f(x5,n)+5f(x5,n1)+10f(x5,n2)+10f(x5,n3)+5f(x5,n4)+f(x5,n5)f(x, n) = 31 + f(x - 5, n) + 5f(x - 5, n - 1) + 10f(x - 5, n - 2) + 10f(x - 5, n - 3) + 5f(x - 5, n - 4) + f(x - 5, n - 5)

f(x,n)=63+f(x6,n)+6f(x6,n1)+15f(x6,n2)+20f(x6,n3)+15f(x6,n4)+6f(x6,n5)+f(x6,n6)f(x, n) = 63 + f(x - 6, n) + 6f(x - 6, n - 1) + 15f(x - 6, n - 2) + 20f(x - 6, n - 3) + 15f(x - 6, n - 4) + 6f(x - 6, n - 5) + f(x - 6, n - 6)

Eagle eye readers will notice a pattern.

Binomial coefficients in Pascal's triangle

It turns out that this expansion is related to the binomial coefficients.

If we had infinite number of eggs, you'd see that the first element is the only term that contributes to f(x,n)f(x, n), since the others will be 00 for xdx - d where dd is the depth in the triangle. That is to say, if we had infinite eggs, with 6 drops we can guarantee checking 63 floors.

Using this, we can say that, with xx drops we can guarantee checking 2x12^x - 1 floors if we had infinite eggs2.

Implementation

Let's implement this problem.

julia> println(VERSION)
1.4.0

We can implement Equation 3{.quarto-xref} as a function:

julia> f(x, n) = x == 0 || n == 0 ? 0 : 1 + f(x - 1, n) + f(x - 1, n - 1)
f (generic function with 1 method)

For 11 egg, the number of floors you can check is the number of drops you make.

julia> arr = 1:100;
julia> @test f.(arr, 1) == arr
Test Passed

And, we can verify that it works for 22 eggs as well.

julia> @test f(14, 2) >= 100
Test Passed
julia> @test f(14, 2) == 105
Test Passed

We can implement this generally like so:

julia> function starting_floor(floors, n)
for x in 1:floors
if f(x, n) >= floors
return x
end
end
end
starting_floor (generic function with 1 method)

And get the answer to the problem programmatically.

julia> starting_floor(100, 2)
14

Using the function f, we can also generate a table that explores what is the maximum number of floors that can be check for various number of drops and eggs.

Drops1 egg2 eggs3 eggs4 eggs5 eggs6 eggs7 eggs8 eggs9 eggs10 eggs
1 drop1111111111
2 drops2333333333
3 drops3677777777
4 drops4101415151515151515
5 drops5152530313131313131
6 drops6214156626363636363
7 drops7286398119126127127127127
8 drops83692162218246254255255255
9 drops945129255381465501510511511
10 drops1055175385637847967101210221023
11 drops1166231561102314851815198020352046
12 drops1278298793158525093301379640164082
13 drops13913771092237940955811709878138099
14 drops141054691470347264759907129101491215913
15 drops1512057519404943994816383228182782330826

Maximum number of floors that can be checked with xx drops and nn eggs.

You'll notice that the upper right corner of the table stays the same if you increase the number of eggs you have at your disposal. You can see this even more clearly in this visualization.

The relationship between the number of floors that can be checked, the
number of drops and number of eggs.

There's a minimal number of drops required to guarantee that you will find the breaking floor, even if you have unlimited eggs.

If you want to check 77 floors, as long as you have more than 33 eggs, you can have to use a minimum of 3 drops to guarantee finding the breaking floor.

For 100 floors, it is 77 drops. If you had unlimited eggs, you would use the largest partition possible. That means dividing the total floors by 22.

Partitioning the floors equally or in other words bisecting the floors, and exploring the partition of interest using the same strategy is the most efficient way of finding the breaking floor.

So there you go! We have solved the general case for the egg and tower puzzle.

Footnotes

  1. Thanks to /u/possiblywrong for pointing this out.

  2. If we had a finite number of eggs, an approximation of this recurrence can be made. See /u/mark_ovchain's insightful comment on this thread for more information.


Citation

@online{krishnamurthy2020theeggtowerpuzzle,
  author = {Dheepak Krishnamurthy},
  title = {The egg tower puzzle},
  year = {2020},
  date = {2020-06-06},
  url = {https://kdheepak.com/blog/the-egg-tower-puzzle/},
  langid = {en},
}

For attribution, please cite this work as:

Dheepak Krishnamurthy, "The egg tower puzzle", June 6, 2020 https://kdheepak.com/blog/the-egg-tower-puzzle/


Three built-in `neovim` features
Drawing in ASCII