There are 1,500 loyal servants sitting around the king's table when they decide to play a little game. The 1st servant gets a sword and kills the 2nd servant. He then gives the sword to the 3rd servant, who then kills the 4th servant and then gives the sword to the 5th. This continues so that the 1,499th servant kills the 1,500th servant and gives the sword back to the 1st servant.
Now the 1st servant kills the 3rd and gives the sword to the 5th. This process continues until only one servant remains. Which number servant is the lone survivor?
Comments:
Carlos - 10 years, 8 months ago
I think there is a small problem that needs to be answered before anyone can really resolve the problem: We have 1500 servants, a pair number, next round will have 750, still a pair number, then 375, not a pair number, which means that at the end of the round, there will be 1 guy left that has no one to kill, unless he has to kill the first. So my question is, if there is guy left in a round (which eventually happens) does he kill the first guy or does he hands the sword to the first guy?
Hopefully he kills the first guy, otherwise it's too easy.
reply permalink
Max Burstein - 10 years, 8 months ago
It goes around in a circle. So once it reaches the highest number it goes back to the lowest number. So if there were 1-375 then 375 would kill 1 and pass to 3.
reply permalink
Anonymous - 10 years, 8 months ago
Servant 953 wins after killing servant 1465.
I used C# to figure this out. I assumed that after a round the guy left with the sword kills the first guy.
reply permalink
Max Burstein - 10 years, 8 months ago
We have a winner! Nice job
reply permalink
gulliver - 10 years, 8 months ago
Is there a way to solve this without programming it? Here's my effort in python:
reply permalink
Max Burstein - 10 years, 8 months ago
The mathy way to do it is a left circular shift. The formula for which happens to be i + 1 % n which is essentially what the problem is asking for in a roundabout way. I didn't realize it either when I first solved the problem but I think it's definitely a cool/efficient solution.
reply permalink
Dustin S - 10 years, 8 months ago
I used a circular linked-list to solve this! I agreed that the last servant standing is 953.
reply permalink
Jt - 10 years, 8 months ago
Python
reply permalink
bumbleguppy - 10 years, 8 months ago
javascript. It took me too long to do this, but I tried to make an array utility that does the circular search and return the next index. I guess I could have done an Array.prototype.
reply permalink
Zach - 10 years, 8 months ago
I did all the work in the actual for loop header itself, so there is no body at all. I think it's certainly less readable because of it, but its also a nice change so I'm keeping it that way.
reply permalink
Anonymous - 10 years, 8 months ago
Neat problem. In J:
Thanks for these Potds, and happy holidays!
reply permalink
Anonymous - 10 years, 8 months ago
It's simpler. Gotta break free from the procedural mindset!
reply permalink
Eddy - 10 years, 8 months ago
I tried to keep the solution as tiny as I could in C - GitHub link
reply permalink
MrZammler - 10 years, 8 months ago
Here's my attempt: ```C #include <stdio.h>
int servants[1500]; int cnt_alive=1500; int kill_next (int sword, int to_die); int next_alive (int sword);
int main(int argc, char *argv[]) { int i;
for (i=0;i<=1499;i++) servants[i]=1; //all alive at first
kill_next(0,1);
for (i=0;i<=1499;i++) if (servants[i]==1) printf("Servant [%d] is alive.\n", i+1);
return 0; }
int next_alive (int sword) { if (servants[sword]==1) return sword; else { sword++; if (sword>1499) sword=0; next_alive(sword); }
}
int kill_next (int sword, int to_die) { if (cnt_alive == 1) return;
printf("[%d] will kill [%d]\n",sword+1, to_die+1); servants[to_die]=0; cnt_alive--; sword = next_alive(to_die); to_die = next_alive(sword+1); kill_next(sword, to_die); }```
reply permalink
MrZammler - 10 years, 8 months ago
Sorry for the mess, not really sure how to quote code...
reply permalink
Anonymous - 10 years, 8 months ago
reply permalink