Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I made many off-by-one errors on the way to this proposed solution, but I think I have it.
- Assign each knight a position number, starting with 1 and going counterclockwise.
- At any given time, let N be the number of remaining knights. Let P be the lowest position of the remaining knights. For example, we might start with 13 knights. After the initial sit-down, before Arthur has dismissed anyone, the list of knights will look like this, where horizontal position indicates each knight's position number:
- x x x x x x x x x x x x x (N = 13, P = 1)
- We want to calculate in the general case what P will be when N reaches 1.
- I'll use the N = 13 example, and transform the list in three iterations to arrive at N = 1.
- For the first iteration, remove the even-numbered knights -- and ALSO remove the FIRST knight in the list. We know he will be removed when Arthur comes back around the circle, because N = 13 is odd, so we proactively throw him away. This gives us:
- x x x x x x x x x x x x x (N = 13, P = 1)
- - - x - x - x - x - x - x (N = 6, P = 3)
- For the second iteration, where N = 6 is even, remove the second, fourth, and sixth knights, but DON'T throw away the first knight. So:
- x x x x x x x x x x x x x (N = 13, P = 1)
- - - x - x - x - x - x - x (N = 6, P = 3)
- - - x - - - x - - - x - - (N = 3, P = 3)
- For the third iteration, because N is now odd again, throw away the second knight AND (proactively) the first knight:
- x x x x x x x x x x x x x (N = 13, P = 1)
- - - x - x - x - x - x - x (N = 6, P = 3)
- - - x - - - x - - - x - - (N = 3, P = 3)
- - - - - - - - - - - x - - (N = 1, P = 11)
- Note that with each iteration, the difference between consecutive positions doubles from 1 to 2 to 4 to 8. The above could be written as
- x x x x x x x x x x x x x (N = 13, P = 1)
- - - x - x - x - x - x - x (N = 6, P = 1 + 2)
- - - x - - - x - - - x - - (N = 3, P = 1 + 2 + 0)
- - - - - - - - - - - x - - (N = 1, P = 1 + 2 + 0 + 8)
- Therefore, when P changes (due to the lowest-numbered knight getting tossed), it increases by a power of 2.
- With this observation, I propose the following algorithm:
- start with N = # of knights, P = 1, i = 1 (P for position, i for iteration)
- while (N > 1) {
- if N is odd
- N = (N - 1)/2, P = P + 2^i
- else
- N = N / 2
- i = i + 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement