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.