Credit to Geobits in TNB for the idea
A post without sufficient detail recently posited an interesting game:
2 children sit in front of an array of candy. Each piece of candy is numbered 1 to
x being the total amount of candy present. There is exactly 1 occurrence of each number.
The goal of the game is for the children to eat candy and multiply the values of the candy they have eaten to arrive at a final score, with the higher score winning.
However the original post missed key information, such as how candy is selected, so the kids in our story decided that the older kid gets to go first, and can eat up to half the candy, however once he announces the end of his turn, he can't change his mind.
One of the kids in this game doesn't like candy, so he wants to eat as little as possible, and he once watched his dad write some code once, and figures he can use the skills gained from that to work out how much candy he needs to eat to ensure victory, whilst still eating as little as possible.
Given the total number of candy
x, your program or function should output the smallest amount of candy he has to eat to ensure victory,
n, even if his opponent eats all the remaining candy.
Naturally bigger numbers make bigger numbers, so whatever amount you'll give him, he'll eat the
n largest numbers.
xwill always be a positive integer in the range
0 < x! <= lwhere
lis the upper limit of your language's number handling capabilities
- It is guaranteed that the kid will always eat the
nlargest numbers, for example for
x = 5and
n = 2, he will eat
x = 1 n = 1 (1 > 0) x = 2 n = 1 (2 > 1) x = 4 n = 2 (3 * 4 == 12 > 1 * 2 == 2) x = 5 n = 2 (4 * 5 == 20 > 1 * 2 * 3 == 6) x = 100 n = 42 (product([59..100]) > product([1..58])) x = 500 n = 220 (product([281..500]) > product([1..280]))
Unfortunately, our brave contestant has nothing to write his code with, so he has to arrange the pieces of candy into the characters of the code, as a result, your code needs to be as small as possible, smallest code in bytes wins!