How to throw 2 eggs out of a building and find floor F with ~ c * sqrt (F) throws?
I am reading the 4th edition of Robert Sedgwick's book and he has the following problem:
Suppose that you have an N-story building and 2 eggs. Suppose also that an egg
is broken if it is thrown off floor F or higher, and unbroken otherwise. Your
cost model is the number of throws. Devise a strategy to determine F such that
the number of throws ~c √ F for some constant c.
The first part of the problem is to find F in 2 √ N steps, and here's the solution for that:
Solution to Part 1: To achieve 2 * sqrt(N), drop eggs at floors sqrt(N), 2 *
sqrt(N), 3 * sqrt(N), ..., sqrt(N) * sqrt(N). (For simplicity, we assume
here that sqrt(N) is an integer.) Let assume that the egg broke at level k *
sqrt(N). With the second egg you should then perform a linear search in the
interval (k-1) * sqrt(N) to k * sqrt(N). In total you will be able to find
the floor F in at most 2 * sqrt(N) trials.
It also gives a hint for the ~ c √ F part (part 2):
Hint for Part 2: 1 + 2 + 3 + ... k ~ 1/2 k^2.
So what is the algorithm for doing this in ~ c √ F steps?
+3
source to share