Egg Problem


Harshit Maheshwari
Computer Science, IIT-Kanpur

Let's review the famous egg problem. The general form of the puzzle is stated like this:

"Given 2 eggs and a 100 storey building what strategy would you follow to 1) determine the height from which the eggs would start breaking 2) Minimize the number of drops made"

This in itself can be easily solved with pen and paper. But, if we extend this to $k$ eggs and $n$ floors then the problem gets quickly out of hand and we need to write code to solve this. We will review how the minimum number of drops varies with number of eggs. First we will get the exact expression for $k=1,2,3$
Case 1 ($k=1$)
This is the trivial case. When we have only one egg we will have to check from the base of the building and go up checking each and every floor. Let $h$ denote the minimum number of steps required then, we obtain
$$h=n$$ which is clearly a linear relation.
Case 2 ($k = 2$)
This is the famous case. To arrive at the solution, suppose the initial height from which we will drop the egg is $h$. Now if the first egg breaks in the first trial then number of subsequent trials required is $trial(1, h-1)$ where $trial(e, h)$ gives us the number of trials required for a building of height $h$ and with number of eggs $e$. This is Case1. So, for 2 eggs if we start from height $h$ and first egg breaks at $h$ itself then the minimum number of drops required will be $trial(1, h-1)$. Let's denote it by $p$
Now, suppose the egg doesn't break. Then we can next throw the egg from $h+x_0$ floor. The question that arises is: What should this $x_0$ be ?? The value of $x$ should be such that if the first egg breaks on the $h+x_0$ floor then minimum number of trials required subsequently with 1 egg for $x-1$ floors, i.e. $trial(1, x-1)$ is 1 less than the case when first egg broke on $h$ floor. This is because when we dropped the egg from $h+x_0$ height we utilized one more trial, and we want to minimize the maximum number of trials required. Therefore, $x_0 = p-1$. Next when we drop the egg we will drop it from $h + x_1$ and so on, s.t. $$ x_i = p-i - 1$$ Also, $$h + (h-x_0) + (h-x_1) + .. (h - x_{last}) = n$$ This gives us the following relation: $$ p + (p-1) + (p-2) + ... 1 = n$$ Solving, the above gives us the quadratic equation: $$ \frac{p^2 + p}{2} = n$$ Notice, it is a quadratic equation as in contrast with Case 1, which was linear.
Case 3 ($k=3$)
Now, consider the case with 3 eggs. Suppose, we throw the first egg from height $h$ and it breaks. Now, the number of trials required is $trial(2, h-1)$. Let's denote it by $p$. This is given by the quadratic equation: $ \frac{p^2 + p}{2} = n$
Now, suppose the egg doesn't break. Now, we should throw it from height $h+x_0$ such that $trial(2, x-1) + 1 = p$ $$p - 1 = \frac{-1 + \sqrt[2]{1+8x_0}}{2}$$ $$p \times (p-1) = x_0$$
Similarly, we get
$$ (p-i) \times (p - i - 1) = x_i$$ Also, $$h + (h-x_0) + (h-x_1) + .. (h - x_{last}) = n$$ So, $$\frac{p(p+1)}{2} + \frac{p(p-1)}{2} + \frac{(p-1)(p-2)}{2} + ... \frac{(2)(1)}{2} = n$$ Solving this we get: $$ \frac{p^3 + 2p}{6} = n$$ Notice, it is a cubic equation as in contrast with Case 2, which was quadratic.
Note
Astute readers may have already noticed that the initial height of the floor from which the first egg should be dropped ($h$) is proportional to the $\frac{1}{egg}$ power of number of floor of the building ($n$). $$ h \propto (n)^{\frac{1}{egg}}$$

Graphs

For egg = 1 and floor = 1000; we get a linear graph

For $e = 2$ and $n = 1000$; we get a square root graph.

For $e = 3$ and $n= 1000$; we get a cube root graph





References

Chart.js
Solution of cubic and quartic equations C++