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
-
10 Comments
Also, I agree with Andrew Newell's earlier comment that there should be a test case for kill_every = 1.
There are so many problems with the problem description.
1). There is no explanation as to why 10 is wrapped around to the front in the second iteration. If it is okay to have a group of one in the first iteration; why not the second?
2). On the "Third iteration" line, the 1st and 4th are chosen, instead of indexing by 3, 6, 9, ... etc.
3). Why was the array expanded to a length of 12 for the third iteration, 8 for the second iteration, and 4 for the fourth iteration?
I want to like this problem, it sounds interesting. I think any valid solution is achieved by accident rather than design. The person posting this problem should be ashamed at the poor description of the problem. This really sets the bar low, shame.
@Jason the key realization that'll address all the concerns you raise is that you shouldn't think of decimation as happening in discrete rounds to begin with.
Instead, imagine that the prisoners are standing in a circle, and Carnage Maximus is standing in the middle, pointing to each living prisoner in turn. When the number of times he has pointed is divisible by kill_every, then that prisoner is taken away to be executed, and the decimation process continues. The circle is now smaller - but Carnage Maximus is ignorant of this and simply keeps on pointing.
In other words, in the example, you start with this sequence of prisoners (this is what you'd get if you pointed to each person in the circle in term, without ever taking anyone away):
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 ...
After 3, 6 and 9 have been removed - with Carnage Maximus still pointing at the empty spot where 9 used to stand -, the circle is now
1 2 4 5 7 8 . 10 1 2 4 5 7 8 10 1 2 4 5 7 8 10 1 2 ...
and the . is where Carnage Maximus is pointing. Moving on, the 3rd and 6th prisoners he points to next are 2, 7, so the circle is now
1 4 5 . 8 10 1 4 5 8 10 1 4 5 8 10 ...
Now the 3rd and 6th prisoners he'll point to next are 1 and 8, after which the circle is
4 5 . 10 4 5 10 4 5 10 ...
Then 5 is removed next,
4 . 10 4 10 4 10 ...
and after that 10 is removed, leaving 4. I hope that this clears up what's happening and why it does, in fact, make perfect sense!
Solution Comments
Show commentsProblem Recent Solvers259
Suggested Problems
-
Swap the first and last columns
21020 Solvers
-
It dseon't mettar waht oedrr the lrettes in a wrod are.
1913 Solvers
-
Create matrix of replicated elements
383 Solvers
-
Implement a bubble sort technique and output the number of swaps required
321 Solvers
-
738 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!