Post · 5 min read· pp. 805–808

Josephus Problem

Statement

We are given the natural numbers nn and kk. All natural numbers from 11 to nn are written in a circle. First, count the kk-th number starting from the first one and delete it. Then kk numbers are counted starting from the next one and the kk-th one is removed again, and so on. The process stops when one number remains. It is required to find the last number.

This task was set by Flavius Josephus in the 1st century (though in a somewhat narrower formulation: for k=2k = 2).

This problem can be solved by modeling the procedure. Brute force modeling will work O(n2)O(n^{2}). Using a Segment Tree, we can improve it to O(nlog⁡n)O(n \log n). We want something better though.

Modeling a O(n)O(n) solution

We will try to find a pattern expressing the answer for the problem Jn,kJ_{n, k} through the solution of the previous problems.

Using brute force modeling we can construct a table of values, for example, the following:

n∖k123456789101111111111122121212121333221133224411223233455341244124665151453527774263547588176314487993118723881010545339178\begin{array}{ccccccccccc} n\setminus k & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 2 & 2 & 1 & 2 & 1 & 2 & 1 & 2 & 1 & 2 & 1 \\ 3 & 3 & 3 & 2 & 2 & 1 & 1 & 3 & 3 & 2 & 2 \\ 4 & 4 & 1 & 1 & 2 & 2 & 3 & 2 & 3 & 3 & 4 \\ 5 & 5 & 3 & 4 & 1 & 2 & 4 & 4 & 1 & 2 & 4 \\ 6 & 6 & 5 & 1 & 5 & 1 & 4 & 5 & 3 & 5 & 2 \\ 7 & 7 & 7 & 4 & 2 & 6 & 3 & 5 & 4 & 7 & 5 \\ 8 & 8 & 1 & 7 & 6 & 3 & 1 & 4 & 4 & 8 & 7 \\ 9 & 9 & 3 & 1 & 1 & 8 & 7 & 2 & 3 & 8 & 8 \\ 10 & 10 & 5 & 4 & 5 & 3 & 3 & 9 & 1 & 7 & 8 \\ \end{array}

And here we can clearly see the following pattern:

Jn,k=((Jn−1,k+k−1) mod n)+1J_{n,k} = \left( (J_{n-1,k} + k - 1) \bmod n \right) + 1
J1,k=1J_{1,k} = 1

Here, 1-indexing makes for a somewhat messy formula; if you instead number the positions from 0, you get a very elegant formula:

Jn,k=(Jn−1,k+k) mod nJ_{n,k} = (J_{n-1,k} + k) \bmod n

So, we found a solution to the problem of Josephus, working in O(n)O(n) operations.

Implementation

Simple recursive implementation (in 1-indexing)

text
int josephus(int n, int k) {
    return n > 1 ? (josephus(n-1, k) + k - 1) % n + 1 : 1;
}

Non-recursive form :

text
int josephus(int n, int k) {
    int res = 0;
    for (int i = 1; i <= n; ++i)
      res = (res + k) % i;
    return res + 1;
}

This formula can also be found analytically. Again here we assume 0-indexing. After we delete the first number, we have n−1n-1 numbers left. When we repeat the procedure, we will start with the number that had originally the index k mod nk \bmod n. Jn−1,kJ_{n-1, k} would be the answer for the remaining circle, if we start counting at 00, but because we actually start with kk we have Jn,k=(Jn−1,k+k)  mod nJ_{n, k} = (J_{n-1,k} + k) \ \bmod n.

Modeling a O(klog⁡n)O(k \log n) solution

For relatively small kk we can come up with a better solution than the above recursive solution in O(n)O(n). If kk is a lot smaller than nn, then we can delete multiple numbers (⌊nk⌋\lfloor \frac{n}{k} \rfloor) in one run without looping over. Afterwards we have n−⌊nk⌋n - \lfloor \frac{n}{k} \rfloor numbers left, and we start with the (⌊nk⌋⋅k)(\lfloor \frac{n}{k} \rfloor \cdot k)-th number. So we have to shift by that many. We can notice that ⌊nk⌋⋅k\lfloor \frac{n}{k} \rfloor \cdot k is simply −n mod k-n \bmod k. And because we removed every kk-th number, we have to add the number of numbers that we removed before the result index. Which we can compute by dividing the result index by k−1k - 1.

Also, we need to handle the case when nn becomes less than kk. In this case, the above optimization would cause an infinite loop.

Implementation (for convenience in 0-indexing):

text
int josephus(int n, int k) {
    if (n == 1)
        return 0;
    if (k == 1)
        return n-1;
    if (k > n)
        return (josephus(n-1, k) + k) % n;
    int cnt = n / k;
    int res = josephus(n - cnt, k);
    res -= n % k;
    if (res < 0)
        res += n;
    else
        res += res / (k - 1);
    return res;
}

Let us estimate the complexity of this algorithm. Immediately note that the case n<kn < k is analyzed by the old solution, which will work in this case for O(k)O(k). Now consider the algorithm itself. In fact, after every iteration, instead of nn numbers, we are left with n(1−1k)n \left( 1 - \frac{1}{k} \right) numbers, so the total number of iterations xx of the algorithm can be found roughly from the following equation:

n(1−1k)x=1,n \left(1 - \frac{1}{k} \right) ^ x = 1,

on taking logarithm on both sides, we obtain:

ln⁡n+xln⁡(1−1k)=0,@@MATHPH0@@x=−ln⁡nln⁡(1−1k),\ln n + x \ln \left(1 - \frac{1}{k} \right) = 0,@@MATH_PH_0@@x = - \frac{\ln n}{\ln \left(1 - \frac{1}{k} \right)},

using the decomposition of the logarithm into Taylor series, we obtain an approximate estimate:

x≈kln⁡nx \approx k \ln n

Thus, the complexity of the algorithm is actually O(klog⁡n)O (k \log n).

Analytical solution for k=2k = 2

In this particular case (in which this task was set by Josephus Flavius) the problem is solved much easier.

In the case of even nn we get that all even numbers will be crossed out, and then there will be a problem remaining for n2\frac{n}{2}, then the answer for nn will be obtained from the answer for n2\frac{n}{2} by multiplying by two and subtracting one (by shifting positions):

J2n,2=2Jn,2−1J_{2n, 2} = 2 J_{n, 2} - 1

Similarly, in the case of an odd nn, all even numbers will be crossed out, then the first number, and the problem for n−12\frac{n-1}{2} will remain, and taking into account the shift of positions, we obtain the second formula:

J2n+1,2=2Jn,2+1J_{2n+1,2} = 2 J_{n, 2} + 1

We can use this recurrent dependency directly in our implementation. This pattern can be translated into another form: Jn,2J_{n, 2} represents a sequence of all odd numbers, "restarting" from one whenever nn turns out to be a power of two. This can be written as a single formula:

Jn,2=1+2(n−2⌊log⁡2n⌋)J_{n, 2} = 1 + 2 \left(n-2^{\lfloor \log_2 n \rfloor} \right)

Analytical solution for k>2k > 2

Despite the simple form of the problem and a large number of articles on this and related problems, a simple analytical representation of the solution of Josephus' problem has not yet been found. For small kk, some formulas are derived, but apparently they are all difficult to apply in practice (for example, see Halbeisen, Hungerbuhler "The Josephus Problem" and Odlyzko, Wilf "Functional iteration and the Josephus problem").