1234567890193245123 people stand in a circle. Starting from person 1, every second person is removed from the circle. Who is the last remaining person?
As an explicit example, if there were 5 people, it would play as such: We count 1, 2; 2 is removed. 3, 4; 4 is removed. 5, 1; 1 is removed. 3 (since 2 is already removed), 5; 5 is removed. Only 3 remains, so the answer is 3.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
One can even generalise this, if J ( n ) is the last person left if n people are standing in a circle, then J ( 2 p + q ) = 2 q + 1 . Here, 2 p > q and so p = 6 0 in this case.
Probably the most elegant solution I know for this problem is using the binary form of the number: For a given n, We convert n to binary, so that it looks like 1b1b2...bm and then we shift the first 1 to the right by a cyclic shift so that now we have b1b2...bm1 in binary form. Let this binary number have the decimal equivalent as d. Then the survivor is d. For those interested, you can read about it on Wikipedia. Its given quite nicely there. Also for a given n, there is an explicit formula, (2(n-2^floor((ln(n)/ln(2))))+1) th person is the survivor.
More generally, n people stand in a circle. Starting from the first person, every second person is eliminated from the circle. Who is the last remaining person? For the case where n is not a power of 2, we write n as n = 2 m + k , where 2 m is the largest power of 2 less than or equal to n , and solve for k ; k people must be eliminated to reduce the problem to a power of 2, which means that 2 k people must be passed over, and thus person 2 k + 1 will be the survivor.
Applying this general solution for n people standing in a circle to the case of 1234567890193245123 people standing in a circle: the closest power of 2 to 1234567890193245123 is 2 6 0 . So we have the equation 1234567890193245123 = 2 6 0 + k . Solving for k , we get that k = 81646385586398147, and thus the last remaining person is person 2(81646385586398147) + 1 = 163292771172796295.
Problem Loading...
Note Loading...
Set Loading...
The Josephus problem J ( n , k ) has a linear time solution over all valid k . However, this is too slow, as the number given in the problem statement is about 2 6 1 . For the special case of k = 2 , however, there exists a solution with greatly improved running time. By removing the most significant bit (MSB), shifting the number 1 to the left, and readding the MSB at the beginning, we obtain the answer for J ( n , 2 ) . Convince yourself this works on the sample case given for n = 5 . The code is then a matter of implementation. Sample is given in C++ below.