# The egg problem

The egg problem is a classic programming challenge. However, there is a mathematical solution. You can solve this problem analytically, even in the general case with eggs.

## The two-egg problem

### Problem

You have a 100-story building and two eggs. When you drop an egg from any floor of the building, the egg will either break, or it will survive the fall: If the egg breaks, then any greater fall would have broken it as well. If the egg survives, then it would have survived any lesser fall. Every egg is identical and totally interchangeable. You’d like to find the lowest floor that will break an egg. What is the minimum number of tries in which you are guaranteed to find the lowest egg-breaking floor?

### Solution

An egg could break at any time. When it does, we’ll be down to the one-egg problem. So we need to solve the one-egg problem first: How many floors can we explore with a single egg?

Since we can’t afford to lose our one and only egg, we’re forced to start at the ground floor. If the egg breaks there, then we’ve found the right floor. Otherwise, we get to keep our egg, and we can try again with the next higher floor. Continuing on like this, the distance we can fully explore with egg and tries is:
for

Now let’s use this to solve the two-egg problem, where we start with eggs and tries. When we drop an egg off a floor, we explore that floor. If the egg breaks, then we’re forced to explore the lower floors with egg and tries. But if the egg survives, then we can move on to the upper floors with eggs and tries. So the total distance that we can explore with eggs and tries is:

Using the earlier solution to the one-egg problem:

We can expand this recurrence relation:

When we’re out of tries, we can’t explore any further, no matter how many eggs we have left:

This gives the closed-form solution:

for

In particular:

So, given two eggs, we’re guaranteed to find the right floor of a 100-story building within fourteen tries.

### Strategy

We can use the recurrence relation from the previous section to get the sequence of floors from which we should drop our eggs:

On the first attempt, we have eggs and tries:

So our first attempt should be made on floor .

If the egg breaks on the fourteenth floor, then we’ll switch to the -egg algorithm and work our way up from the first floor. If the egg survives, then we’ll move on up to the branch of the recurrence relation. So our next attempt will be floors higher, on floor .

In the best-case scenario, the egg will break on the first floor, and we’ll be done in two tries. In the worst-case scenario, the egg will break on the thirteenth floor, and it will take all of our fourteen tries to find that out.

## The -egg problem

### Problem

Given eggs and tries, how many floors can we explore?

### Solution

Let’s start by dropping one egg: If it breaks, we’ll explore the lower floors; if it survives, we’ll explore the upper floors. Overall, the total distance we can explore is:

We’ve already solved the one- and two-egg problems:
for
for

And, taken together, these statements give us the solution to every -egg problem. In particular, here is the solution to the three-egg problem:

## The three-egg problem

### Problem

Given eggs and tries, how many floors can we explore?

### Solution

When , the -egg recurrence relation becomes:

Using the solution to the two-egg problem:

for

In particular:

So, given three eggs, we’re guaranteed to find the right floor of a 100-story building within nine tries.