Every other prisoner ...
Math geek alert.
As an aside, the notes below are really a condensation of doing
the solution by a complete simulation. Even the chart at the
bottom is nearly so, as the numbers are a compression of the
number ranges and offsets.
Does anyone know a non-iterative way to solve this using elementary
functions (or even using Fibonacci numbers, or a base 2 representation
and a summation)? I put a mini-formula at the bottom, but it's just
a start.
Here are my notes (they read best with a fixed width font):
from 1 .. 1000 [by 1] --> take out 1, 3, 5 .. 999 --> leaving 2 .. 1000 [by 2]
[count == 1000] [count == 500]
from 2 .. 1000 [by 2] --> take out 2, 6, 10 .. 998 --> leaving 4 .. 1000 [by 4]
[count == 500] [count == 250]
from 4 .. 1000 [by 4] --> take out 4, 12, 20 .. 996 --> leaving 8 .. 1000 [by 8]
[count == 250] [count == 125]
renumber by dividing by 8.
from 1 .. 125 [by 1] --> take out 1, 3, 5 .. 125 --> leaving 2 .. 124 [by 2]
[count == 125] [count == 62]
from 2 .. 124 [by 2] --> take out 4, 8, 12 .. 124 --> leaving 2 .. 122 [by 4]
[count == 62] [count == 31]
from 2 .. 122 [by 4] --> take out 6, 14, 22 .. 118 --> leaving 2 .. 122 [by 8]
[count == 31] [count == 16]
from 2 .. 122 [by 8] --> take out 2, 18, 34 .. 114 --> leaving 10 .. 122 [by 16]
[count == 16] [count == 8]
from 10 .. 122 [by 16] --> take out 10, 42, 74 .. 104 --> leaving 26 .. 122 [by 32]
[count == 8] [count == 4]
from 26 .. 122 [by 32] --> take out 26, 90 --> leaving 58, 122 [by 64]
[count == 4] [count == 2]
from 58, 122 [by 64] --> take out 58 --> leaving 122 [by 128]
[count == 2] [count == 1]
Leaving 122.
Backing up: 122 * 8 == 976. The 976'th prisoner is last.
Model:
Prisoner numbers:
a(i) + (k - 1) * 2^i
i gives the generation number
a(i) is the number of the first prisoner
k ranges from 1 .. K(i)
K(i) is the number of prisoners remaining
a(i) ... (K(i) - 1) * 2^i gives the prisoner number range
The initial generation is generation 0, before any prisoners are removed.
The initial prisoner number, a(0), is 1.
The initial prisoner count, K(0), is 1000.
Then:
For each generation 'i' there is a parity of '0' or '1', where
parity '0' means that the first prisoner will be removed,
and parity '1' means that the second prisoner will be removed.
P(i) == 0, 1
In the given problem, P(0) == 0.
For a given 'i', 'P(i+1)' is given by:
P(i+1) = P(i) if K(i) is even;
P(i+1) = P(i) + 1 (mod 2) if K(i) is odd.
For a given 'i', 'a(i+1)' is given by:
a(i+1) = a(i) + 2^i if P(i) is 0;
a(i+1) = a(i) if P(i) is 1.
For a given 'i', 'K(i+1)' is given by:
K(i+1) = K(i) / 2 if K(i) is even;
K(i+1) = (K(i) + 1) / 2 if K(i) is odd and P(i) is 1;
K(i+1) = (K(i) - 1) / 2 if K(i) is odd and P(i) is 0.
Then:
i a(i) P(i) K(i) 2^i
0 1 0 1000 1
1 2 0 500 2
2 4 0 250 4
3 8 0 125 8
4 16 1 62 16
5 16 1 31 32
6 16 0 16 64
7 80 0 8 128
8 208 0 4 256
9 464 0 2 512
10 976 0 1 1024
Note that 976 == 1024 - 48 == 1024 - 32 - 16,
that is:
a(10) == 976 == sum { 0 .. 9 } of ( 2^i * (1 - P(i)) ) + 1
But, how does P(i) relate to the initial number?