View difference between Paste ID: LzDJcjCP and KYfsQWdN
SHOW: | | - or go back to the newest paste.
1
I made many off-by-one errors on the way to this proposed solution, but I think I have it.
2
3-
Assign each knight a position number, starting with 1 and going counterclockwise.
3+
Assign each knight a position number, starting with 1 and going clockwise.
4
5
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:
6
7
x x x x x x x x x x x x x (N = 13, P = 1)
8
9
We want to calculate in the general case what P will be when N reaches 1.
10
11
I'll use the N = 13 example, and transform the list in three iterations to arrive at N = 1.
12
13
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:
14
15
x x x x x x x x x x x x x (N = 13, P = 1)
16
- - x - x - x - x - x - x (N = 6, P = 3)
17
18
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:
19
20
x x x x x x x x x x x x x (N = 13, P = 1)
21
- - x - x - x - x - x - x (N = 6, P = 3)
22
- - x - - - x - - - x - - (N = 3, P = 3)
23
24
For the third iteration, because N is now odd again, throw away the second knight AND (proactively) the first knight:
25
26
x x x x x x x x x x x x x (N = 13, P = 1)
27
- - x - x - x - x - x - x (N = 6, P = 3)
28
- - x - - - x - - - x - - (N = 3, P = 3)
29
- - - - - - - - - - x - - (N = 1, P = 11)
30
31
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
32
33
x x x x x x x x x x x x x (N = 13, P = 1)
34
- - x - x - x - x - x - x (N = 6, P = 1 + 2)
35
- - x - - - x - - - x - - (N = 3, P = 1 + 2 + 0)
36
- - - - - - - - - - x - - (N = 1, P = 1 + 2 + 0 + 8)
37
38
Therefore, when P changes (due to the lowest-numbered knight getting tossed), it increases by a power of 2.
39
40
With this observation, I propose the following algorithm:
41
42
start with N = # of knights, P = 1, i = 1 (P for position, i for iteration)
43
44
while (N > 1) {
45
    if N is odd
46
        N = (N - 1)/2, P = P + 2^i
47
    else
48
        N = N / 2
49
    i = i + 1
50
}