Josephus Problem

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.


The answer is 163292771172796295.

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.

3 solutions

Spencer Whitehead
Jan 31, 2016

The Josephus problem J ( n , k ) J(n, k) has a linear time solution over all valid k k . However, this is too slow, as the number given in the problem statement is about 2 61 2^{61} . For the special case of k = 2 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 ) J(n, 2) . Convince yourself this works on the sample case given for n = 5 n=5 . The code is then a matter of implementation. Sample is given in C++ below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main() {
    long long n = 1234567890193245123;
    long long n2 = n;
    long long i = 0;
    while (1) {
        n2 >>= 1;
        if (!n2)
            break;
        i++;
    }
    cout << (((n & ~(1LL << i)) << 1LL) | 1LL) << endl;
}

One can even generalise this, if J ( n ) J(n) is the last person left if n n people are standing in a circle, then J ( 2 p + q ) = 2 q + 1 J(2^p+q)=2q+1 . Here, 2 p > q 2^p>q and so p = 60 p=60 in this case.

Akshat Sharda - 5 years, 4 months ago
Soumava Pal
Feb 1, 2016

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.

Frank Aiello
Feb 3, 2018

More generally, n 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 n is not a power of 2, we write n n as n = 2 m + k n = 2^m + k , where 2 m 2^m is the largest power of 2 less than or equal to n n , and solve for k k ; k k people must be eliminated to reduce the problem to a power of 2, which means that 2 k 2k people must be passed over, and thus person 2 k + 1 2k+1 will be the survivor.

Applying this general solution for n 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 60 2^{60} . So we have the equation 1234567890193245123 = 2 60 + k 2^{60} + k . Solving for k k , we get that k k = 81646385586398147, and thus the last remaining person is person 2(81646385586398147) + 1 = 163292771172796295.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...