tags:

views:

30

answers:

1

Ages old question:

You have 2 hypothetical eggs, and a 100 story building to drop them from. The goal is to have the least number of guaranteed drops that will ensure you can find what floor the eggs break from the fall. You can only break 2 eggs.

Using a 14 drop minimum method, I need help writing code that will allow me to calculate the following:

Start with first drop attempt on 14th floor.

If egg breaks then drop floors 1-13 to find the floor that causes break.

ElseIf egg does not break then move up 13 floors to floor number 27 and drop again.

If egg breaks then drop floors 15-26 starting on 15 working up to find the floor egg breaks on.

ElseIf egg does not break then move up 12 floors to floor number 39 and drop again.

etc. etc. The way this increases is as follows 14+13+12+11+10+9+8+7+6+5+4+3+2+1 So always adding to the previous value, by one less.

I have never written a sorting algorithm before, and was curious how I might go about setting this up in a much more efficient way than a mile long of if then statements.

My original idea was to store values for the floors in an array, and pull from that, using the index to move up or down and subtract or add to the variables. The most elegant solution would be a recursive function that handled this for any selected floor, 1-100, and ran the math, with an output that shows how many drops were needed in order to find that floor. Maximum is always 14, but some can be done in less.

+1  A: 

That smells like homework ;o)

Anyway, as "BreakinEggSearch" isn't invented yet (be the first!) you could try something like the binary search algorithm. The rest is up to you (I don't want to spoil all the fun ;o)

das_weezul
binary search would not work here, because it is not efficient enough. Also it would not always find the solution while breaking only two eggs. However, I wanted some practice writing that word problem with some recursion. Help would be appreciated.
Peter
I googled a bit and found a solution: http://blog.taragana.com/index.php/archive/google-and-the-puzzle-of-dropping-eggs/But hey, I haven't heard of it before and was half right ;o)You should clarify question though.
das_weezul