# Egg dropping contest

You are on the 0th floor of a infinitely tall building. At any floor, you can walk to the window and drop an egg. Your goal is to figure out the highest floor that the egg can withstand without breaking. However, you have a maximum of 3 eggs to use to figure this out, but you need to minimize the number of tries.

In formal terms:

1. You are given a function f(n) which returns bool(n <= X) for an unknown X, where 0 <= X
2. You must return the value of X (without accessing it directly)
3. f(n) must only return False a maximum of 3 times (in a single test case). If it returns False more than that, then your answer is disqualified.

# Restrictions

Your score is the total number of calls you make to f(n) (in the test cases below)

If you wish, you may forgo passing in a function, and simply "simulate" the above situation. However, your solving algorithm must know nothing of X.

Your algorithm should not hard code the test cases, or a maximum X. If I were to regenerate the numbers, or add more, your program should be able to handle them (with a similar score).

If your language doesn't support arbitrary precision integers, then you may use the long datatype. If your language doesn't support either, then you are out of luck.

The nth test case is generated using the following:

g(n) = max(g(n-1)*random(1,1.5), n+1), g(0) = 0, or approximately 1.25^n

# Test cases:

0,1,2,3,4,6,7,8,10,14,15,18,20,27,29,40,57,61,91,104,133,194,233,308,425,530,735,1057,1308,1874,2576,3162,3769,3804,4872,6309,7731,11167,11476,15223,15603,16034,22761,29204,35268,42481,56238,68723,83062,95681,113965,152145,202644,287964,335302,376279,466202,475558,666030,743517,782403,903170,1078242,1435682,1856036,2373214,3283373,4545125,6215594,7309899,7848365,8096538,10409246,15103057,20271921,22186329,23602446,32341327,33354300,46852754,65157555,93637992,107681394,152487773,181996529,225801707,324194358,435824227,579337939,600264328,827690923,1129093889,1260597310,1473972478,1952345052,1977336057,2512749509,3278750235,3747691805,5146052509


This is a , and the person with the lowest score wins!

• Related (see also questions linked in my comment on that one). Jan 2, 2016 at 17:12
• Related question on Puzzling SE (but also with a maximum floor number). Jan 2, 2016 at 17:19
• If I drop one egg from the window of the zeroth floor of any building, I'm pretty sure it will smash. Problem solved 😉 Jan 2, 2016 at 17:24
• @NathanMerrill Point is, this is essentially useless as we can't know anything about how probable each size of x is as you refuse to specify what we can assume about n. It's impossible to write an optimized answer if we don't know all the parameters of how you run the scoring. If you would tell us “your code is run on 100 test cases from n = 0 to 99,” that would be a useful guarantee. Or if you made g independent of n. Jan 2, 2016 at 20:37
• Voting to close: Without a probability distribution to fairly replicate the scoring process, the winning criterion is not objective. Jan 2, 2016 at 21:05

# Javascript, 442859442857 74825 calls

function findFloor(f){
var max = 1;
var min = 0;

//First egg.
var n = 1;
while (f(max)) {
min = max;
n += 1;
max = tetrahedral(n);
}

if (max <= min + 1){
return min;
}

//Second egg.
do {
var range = max - min;
var floor = min + reverseTriangle(range);
var smashed = !f(floor);
if (smashed) {
max = floor;
} else {
min = floor;
}
} while (!smashed && max > min + 1);

if (max <= min + 1){
return min;
}

//Third egg.
while (max > min + 1){
var floor = min + 1;
var smashed = !f(floor);
if (smashed) {
max = floor;
} else {
min = floor;
}
if (smashed) {
break;
}
}

return min;

}

function reverseTriangle(x) {
return Math.ceil((-1 + Math.sqrt(1 + 8 * x)) / 2);
}

function tetrahedral(n) {
return n * (n + 1) * (n + 2) / 6;
}


Test here

Scores for each individual test case:

0: 1, 1: 4, 2: 4, 3: 3, 4: 5, 6: 6, 7: 6, 8: 6, 10: 6, 14: 7, 15: 8, 18: 8, 20: 7, 27: 10, 29: 9, 40: 12, 57: 10, 61: 14, 91: 16, 104: 16, 133: 16, 194: 17, 233: 16, 308: 24, 425: 26, 530: 28, 735: 31, 1057: 33, 1308: 38, 1874: 32, 2576: 47, 3162: 45, 3769: 43, 3804: 55, 4872: 52, 6309: 63, 7731: 69, 11167: 69, 11476: 80, 15223: 90, 15603: 75, 16034: 82, 22761: 69, 29204: 110, 35268: 101, 42481: 105, 56238: 126, 68723: 113, 83062: 113, 95681: 160, 113965: 149, 152145: 148, 202644: 187, 287964: 238, 335302: 175, 376279: 258, 466202: 250, 475558: 247, 666030: 256, 743517: 237, 782403: 245, 903170: 278, 1078242: 256, 1435682: 408, 1856036: 304, 2373214: 401, 3283373: 286, 4545125: 328, 6215594: 510, 7309899: 616, 7848365: 458, 8096538: 683, 10409246: 754, 15103057: 787, 20271921: 653, 22186329: 957, 23602446: 754, 32341327: 1141, 33354300: 1033, 46852754: 984, 65157555: 839, 93637992: 1539, 107681394: 1130, 152487773: 1605, 181996529: 1845, 225801707: 1760, 324194358: 2346, 435824227: 2244, 579337939: 2670, 600264328: 2620, 827690923: 3047, 1129093889: 3334, 1260597310: 3813, 1473972478: 4076, 1952345052: 3946, 1977336057: 3599, 2512749509: 4414, 3278750235: 3600, 3747691805: 5580, 5146052509: 4751


### How it works:

1 egg:

When there is one egg, the best strategy is to go up 1 floor at a time and return the floor directly below the floor where it breaks first.

2 eggs:

When we have two eggs, the maximum number of floors we have to check will be the smallest n for which Tn is greater than the range of floors we have to check. Tn is the nth triangle number. The first throw will be done on the nth floor. The second throw will be done n - 1 floors above the first throw. The mth throw will be done n - m + 1 floors above the m - 1th throw. After the egg smashes, it will take n - m throws to determine the floor by the first method.

3 eggs:

With the first of our eggs we should determine an upper limit for the highest floor. Originally, I did this by doubling the floor number each time. After analyzing the algorithm for 2 eggs I thought that maybe it would be better if each time we throw the egg, the max throws for finding the correct floor with 2 eggs would increase by 1. This can be satisfied by using the tetrahedral numbers. After the first egg smashes, we can use the above methods for our remaining eggs.

The max egg drops it requires to determine the floor should be optimal. However, a better algorithm could probably be found where the average egg drops is better.

## Java, 68985 calls

public static long solve(Predicate<Long> eggSurvives) {
long bestFloor = 0, e1 = 1, e2;

for(long callsDone = 2; eggSurvives.test(e1); bestFloor = e1, e1 += callsDone * callsDone++);

for(e2 = bestFloor;; bestFloor = e2) {
e2 += Math.max((long)Math.sqrt(e1 - e2), 1);

if(e2 >= e1 || !eggSurvives.test(e2)) {
break;
}
}

for(long e3 = bestFloor + 1; e3 < e2 && eggSurvives.test(e3); e3++) {
bestFloor = e3;
}

return bestFloor;
}


Test results:

0: 1 1: 4 2: 4 3: 4 4: 4 6: 6 7: 6 8: 6 10: 7 14: 6 15: 7 18: 7 20: 8 27: 10 29: 10 40: 10 57: 10 61: 9 91: 9 104: 11 133: 18 194: 20 233: 18 308: 18 425: 17 530: 17 735: 28 1057: 31 1308: 30 1874: 30 2576: 39 3162: 47 3769: 60 3804: 34 4872: 65 6309: 37 7731: 48 11167: 79 11476: 39 15223: 56 15603: 82 16034: 93 22761: 88 29204: 111 35268: 110 42481: 127 56238: 126 68723: 135 83062: 117 95681: 115 113965: 137 152145: 138 202644: 115 287964: 234 335302: 223 376279: 244 466202: 220 475558: 193 666030: 214 743517: 225 782403: 230 903170: 338 1078242: 223 1435682: 303 1856036: 384 2373214: 453 3283373: 542 4545125: 459 6215594: 525 7309899: 600 7848365: 388 8096538: 446 10409246: 466 15103057: 650 20271921: 822 22186329: 899 23602446: 698 32341327: 804 33354300: 1065 46852754: 1016 65157555: 1408 93637992: 1390 107681394: 1638 152487773: 1283 181996529: 1877 225801707: 2067 324194358: 1842 435824227: 3110 579337939: 2983 600264328: 1817 827690923: 2450 1129093889: 2981 1260597310: 3562 1473972478: 4237 1952345052: 2244 1977336057: 3585 2512749509: 2893 3278750235: 3101 3747691805: 5182 5146052509: 4107


Test program:

import java.util.function.Predicate;

public class Eggs {
private static long totalCalls;
private static long calls;

public static void main(String[] args) {
for(long maxFloor : new long[] {0,1,2,3,4,6,7,8,10,14,15,18,20,27,29,40,57,61,91,104,133,194,233,308,425,530,735,1057,1308,1874,2576,3162,3769,3804,4872,6309,7731,11167,11476,15223,15603,16034,22761,29204,35268,42481,56238,68723,83062,95681,113965,152145,202644,287964,335302,376279,466202,475558,666030,743517,782403,903170,1078242,1435682,1856036,2373214,3283373,4545125,6215594,7309899,7848365,8096538,10409246,15103057,20271921,22186329,23602446,32341327,33354300,46852754,65157555,93637992,107681394,152487773,181996529,225801707,324194358,435824227,579337939,600264328,827690923,1129093889,1260597310,1473972478,1952345052,1977336057,2512749509L,3278750235L,3747691805L,5146052509L}) {
long resultingFloor = solve(f -> {
calls++;
return f <= maxFloor;
});

if(resultingFloor != maxFloor) {
throw new RuntimeException("Disqualified");
}

System.out.print(maxFloor + ": " + calls + " ");
totalCalls += calls;
calls = 0;
}

System.out.println("\nCalls = " + totalCalls);
}

public static long solve(Predicate<Long> eggSurvives) {
long bestFloor = 0, e1 = 1, e2;

for(long callsDone = 2; eggSurvives.test(e1); bestFloor = e1, e1 += callsDone * callsDone++);

for(e2 = bestFloor;; bestFloor = e2) {
e2 += Math.max((long)Math.sqrt(e1 - e2), 1);

if(e2 >= e1 || !eggSurvives.test(e2)) {
break;
}
}

for(long e3 = bestFloor + 1; e3 < e2 && eggSurvives.test(e3); e3++) {
bestFloor = e3;
}

return bestFloor;
}
}


While optimizing I aimed for trying to make the number of attempts with each egg roughly equal.

• The first egg goes up in number of floors based on the number of attempts so far.
• The second egg skips floors based on the square root of the maximum number of attempts that could be left (based on the lower and upper bound established by the first egg) so that the average number of attempts for the 3rd and final egg should on average be the same as attempts for the 2nd egg.

# Ruby, 67466 66026 calls

$calls = 0 def drop n$calls += 1
n <= $x end def test min = 0 test = 8 i = 8 while drop(test) min = test test += i*i i+=1 end max = test test = min+((max-min)**0.4).to_i while drop(test) min = test test = min+((max-min)**0.5).to_i end return min if min+1 == test min += 1 while drop(min+1) min end  Test code: tests = [0,1,2,3,4,6,7,8,10,14,15,18,20,27,29,40,57,61,91,104,133,194,233,308,425,530,735,1057,1308,1874,2576,3162,3769,3804,4872,6309,7731,11167,11476,15223,15603,16034,22761,29204,35268,42481,56238,68723,83062,95681,113965,152145,202644,287964,335302,376279,466202,475558,666030,743517,782403,903170,1078242,1435682,1856036,2373214,3283373,4545125,6215594,7309899,7848365,8096538,10409246,15103057,20271921,22186329,23602446,32341327,33354300,46852754,65157555,93637992,107681394,152487773,181996529,225801707,324194358,435824227,579337939,600264328,827690923,1129093889,1260597310,1473972478,1952345052,1977336057,2512749509,3278750235,3747691805,5146052509] tests.each{|n|$x = n;test;$calls} puts$calls


Results:

0: 3, 1: 4, 2: 4, 3: 5, 4: 5, 6: 5, 7: 6, 8: 4, 10: 6, 14: 6, 15: 7, 18: 10, 20: 6, 27: 7, 29: 9, 40: 10, 57: 13, 61: 15, 91: 13, 104: 13, 133: 15, 194: 12, 233: 18, 308: 16, 425: 15, 530: 15, 735: 16, 1057: 32, 1308: 30, 1874: 35, 2576: 35, 3162: 54, 3769: 32, 3804: 29, 4872: 45, 6309: 42, 7731: 55, 11167: 72, 11476: 60, 15223: 55, 15603: 71, 16034: 94, 22761: 82, 29204: 119, 35268: 106, 42481: 123, 56238: 127, 68723: 110, 83062: 95, 95681: 139, 113965: 149, 152145: 149, 202644: 144, 287964: 219, 335302: 189, 376279: 183, 466202: 234, 475558: 174, 666030: 235, 743517: 195, 782403: 235, 903170: 346, 1078242: 215, 1435682: 245, 1856036: 422, 2373214: 448, 3283373: 512, 4545125: 378, 6215594: 502, 7309899: 486, 7848365: 440, 8096538: 496, 10409246: 566, 15103057: 667, 20271921: 949, 22186329: 829, 23602446: 746, 32341327: 799, 33354300: 964, 46852754: 1125, 65157555: 1317, 93637992: 1000, 107681394: 1361, 152487773: 1215, 181996529: 2004, 225801707: 1752, 324194358: 1868, 435824227: 3084, 579337939: 2592, 600264328: 1726, 827690923: 2577, 1129093889: 3022, 1260597310: 2582, 1473972478: 3748, 1952345052: 2035, 1977336057: 3712, 2512749509: 2859, 3278750235: 2888, 3747691805: 5309, 5146052509: 4234


This algorithm works like my old algorithm, but has a few differences:

1. The first egg drop is at floor 8

2. The first increment is 8*8 = 64

These numbers are a result of random fine tuning by hand.