Problem 1092. Decimation
When dealing to the Roman Army, the term decimate meant that the entire unit would be broken up into groups of ten soldiers, and lots would be drawn. The person who was unlucky enough to draw the short straw would be executed by the other nine members of his group.
The bloodthirsty Roman Centurion Carnage Maximus decided to apply this to his prisoners, with a few gruesome differences. Rather than kill every tenth prisoner and allow the rest to live, he is going to leave only one prisoner alive and kill all of the others. Instead of killing every tenth prisoner, he chooses a number (kill_every). If kill_every=3, he kills every third prisoner. If kill_every=5, he kills every fifth prisoner. He always chooses a number between 2 and the number of prisoners he has, and this process will be repeated until there is only one prisoner left. For example, if there are 10 prisoners, and kill_every=3
First iteration: 1 2 3 4 5 6 7 8 9 10
1-2-3 4-5-6 7-8-9 10
Prisoners 3, 6 and 9 will be killed.
Second iteration: 1 2 4 5 7 8 10
Because Prisoner 10 was counted during the first iteration, the executions will proceed as such: 10-1-2 4-5-7 8-10, so prisoners 2 and 7 will be killed
Third iteration: 1 4 5 8 10 8-10-1 4-5-8 10, so prisoners 1 and 8 executed.
Fourth Iteration: 10-4-5 10 Prisoner 5 is executed.
Fifth iteration: 10-4 10 Prisoner 10 is executed
Since the sole survivor is prisoner 4, he is released.
You are an unlucky prisoner caught by Carnage Maximum. Prior to lining up the prisoners, he reveals the number of prisoners he has and his value of kill_every for the day. Your job is to figure out which prisoner you need to be in order to survive. Write a MATLAB script that takes the values of num_prisoners and kill_every. The output will be survivor, which is the position of the person who survives. If you write your script quickly enough, that person will be you.
Good luck!
Solution Stats
Problem Comments
-
8 Comments
I seem to get 1773 for test case 6.
I also get 1773 for test case 6
I'll look into it a bit later on. In the meantime, that test case is deleted, so you can see if your solutions work for the rest of the test cases.
An interesting test case would have kill_every=1.
Am I the only one who finds the problem description really lacking? WTF does it mean "Because Prisoner 10 was counted during the first iteration,..." IMHO this is just blah and not a well-defined problem definition...
At least 2 of the test cases do not match what I find if I run the sequence by hand on paper. Specifically:
num_prisoners = 30; kill_every = 5;
Survivor is prisoner number 14, not number 3
and,
num_prisoners = 10; kill_every = 10;
Survivor is prisoner number 6, not number 8
Based on other comments, it looks like the test cases may have changed at some point. I believe at least these two (and likely a couple others) are now incorrect.
@Christopher Lanning, decimate(10, 10) = 8 is correct; perhaps you made a mistake on paper. When doing this by hand, it's easiest to count kill_every steps, wrapping around at the end of the line and skipping over crossed-out entries (think Eeny Meeny Miny Moo). Prisoner 6 gets killed in the 4th iteration.
Also, I agree with Andrew Newell's earlier comment that there should be a test case for kill_every = 1.
Solution Comments
Show commentsProblem Recent Solvers239
Suggested Problems
-
Sort a list of complex numbers based on far they are from the origin.
5625 Solvers
-
Project Euler: Problem 5, Smallest multiple
1329 Solvers
-
936 Solvers
-
Matrix multiplication across rows
323 Solvers
-
253 Solvers
More from this Author80
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!