# Josephus problem

In computer science and mathematics, the Josephus problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game.

A drawing for the Josephus problem sequence for 500 people and skipping value of 6. The horizontal axis is the number of the person. The vertical axis (top to bottom) is time (the number of cycle). A live person is drawn as green, a dead one is drawn as black.[1]

People are standing in a circle waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed.

The problem — given the number of people, starting point, direction, and number to be skipped — is to choose the position in the initial circle to avoid execution.

## History

The problem is named after Flavius Josephus, a Jewish historian living in the 1st century. According to Josephus' account of the siege of Yodfat, he and his 40 soldiers were trapped in a cave by Roman soldiers. They chose suicide over capture, and settled on a serial method of committing suicide by drawing lots. Josephus states that by luck or possibly by the hand of God, he and another man remained until the end and surrendered to the Romans rather than killing themselves. This is the story given in Book 3, Chapter 8, part 7 of Josephus' The Jewish War (writing of himself in the third person):

However, in this extreme distress, he was not destitute of his usual sagacity; but trusting himself to the providence of God, he put his life into hazard [in the manner following]: "And now," said he, "since it is resolved among you that you will die, come on, let us commit our mutual deaths to determination by lot. He whom the lot falls to first, let him be killed by him that hath the second lot, and thus fortune shall make its progress through us all; nor shall any of us perish by his own right hand, for it would be unfair if, when the rest are gone, somebody should repent and save himself." This proposal appeared to them to be very just; and when he had prevailed with them to determine this matter by lots, he drew one of the lots for himself also. He who had the first lot laid his neck bare to him that had the next, as supposing that the general would die among them immediately; for they thought death, if Josephus might but die with them, was sweeter than life; yet was he with another left to the last, whether we must say it happened so by chance, or whether by the providence of God. And as he was very desirous neither to be condemned by the lot, nor, if he had been left to the last, to imbrue his right hand in the blood of his countrymen, he persuaded him to trust his fidelity to him, and to live as well as himself.[2]

The details of the mechanism used in this feat are rather vague. According to James Dowdy and Michael Mays,[3] in 1612 Claude Gaspard Bachet de Méziriac suggested the specific mechanism of arranging the men in a circle and counting by threes to determine the order of elimination.[4] This story has been often repeated and the specific details vary considerably from source to source. For instance, Israel Nathan Herstein and Irving Kaplansky (1974) have Josephus and 39 comrades stand in a circle with every seventh man eliminated.[5] A history of the problem can be found in S. L. Zabell's Letter to the editor of the Fibonacci Quarterly.[6]

Josephus had an accomplice; the problem was then to find the places of the two last remaining survivors (whose conspiracy would ensure their survival). It is alleged that he placed himself and the other man in the 31st and 16th place respectively.[7]

## Variants and generalizations

A medieval version of the Josephus problem involves 15 Turks and 15 Christians aboard a ship in a storm which will sink unless half the passengers are thrown overboard. All 30 stand in a circle and every ninth person is to be tossed into the sea. Where should the Christians stand to ensure that only the Turks are tossed?[8] In other versions the roles of Turks and Christians are interchanged.

In Concrete Mathematics: A Foundation for Computer Science, Graham, Knuth and Patashnik describe and study a "standard" variant:[9] Determine where the last survivor stands if there are n people to start and every second person (k = 2 below) is eliminated.

A generalization of this problem is as follows. We suppose that every mth person will be executed from a group of size n, in which the pth person is the survivor. If there is an addition of x people to the circle, then the survivor is in the p + mx-th position if this is less than or equal to n + x. If x is the smallest value for which (p + mx) > (n + x), then the survivor is in position (p + mx) − (n + x).[10]

## Solution

In the following, ${\displaystyle n}$ denotes the number of people in the initial circle, and ${\displaystyle k}$ denotes the count for each step, that is, ${\displaystyle k-1}$ people are skipped and the ${\displaystyle k}$-th is executed. The people in the circle are numbered from ${\displaystyle 1}$ to ${\displaystyle n}$.

### k=2

We explicitly solve the problem when every second person will be killed, i.e. ${\displaystyle k=2}$. (For the more general case ${\displaystyle k\neq 2}$, we outline a solution below.) We express the solution recursively. Let ${\displaystyle f(n)}$ denote the position of the survivor when there are initially ${\displaystyle n}$ people (and ${\displaystyle k=2}$). The first time around the circle, all of the even-numbered people die. The second time around the circle, the new 2nd person dies, then the new 4th person, etc.; it's as though there were no first time around the circle.

If the initial number of people was even, then the person in position ${\displaystyle x}$ during the second time around the circle was originally in position ${\displaystyle 2x-1}$ (for every choice of ${\displaystyle x}$). Let ${\displaystyle n=2j}$. The person at ${\displaystyle f(j)}$ who will now survive was originally in position ${\displaystyle 2f(j)-1}$. This gives us the recurrence

${\displaystyle f(2j)=2f(j)-1\;.}$

If the initial number of people was odd, then we think of person 1 as dying at the end of the first time around the circle. Again, during the second time around the circle, the new 2nd person dies, then the new 4th person, etc. In this case, the person in position ${\displaystyle x}$ was originally in position ${\displaystyle 2x+1}$. This gives us the recurrence

${\displaystyle f(2j+1)=2f(j)+1\;.}$

When we tabulate the values of ${\displaystyle n}$ and ${\displaystyle f(n)}$ we see a pattern:

 ${\displaystyle n}$ ${\displaystyle f(n)}$ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1

This suggests that ${\displaystyle f(n)}$ is an increasing odd sequence that restarts with ${\displaystyle f(n)=1}$ whenever the index n is a power of 2. Therefore, if we choose m and l so that ${\displaystyle n=2^{m}+l}$ and ${\displaystyle 0\leq l<2^{m}}$, then ${\displaystyle f(n)=2\cdot l+1}$. It is clear that values in the table satisfy this equation. Or we can think that after ${\displaystyle l}$ people are dead there are only ${\displaystyle 2^{m}}$ people and we go to the ${\displaystyle 2l+1}$th person. He must be the survivor. So ${\displaystyle f(n)=2l+1}$. Below, we give a proof by induction.

Theorem: If ${\displaystyle n=2^{m}+l}$ and ${\displaystyle 0\leq l<2^{m}}$, then ${\displaystyle f(n)=2l+1}$.

Proof: We use strong induction on ${\displaystyle n}$. The base case ${\displaystyle n=1}$ is true. We consider separately the cases when ${\displaystyle n}$ is even and when ${\displaystyle n}$ is odd.

If ${\displaystyle n}$ is even, then choose ${\displaystyle l_{1}}$ and ${\displaystyle m_{1}}$ such that ${\displaystyle n/2=2^{m_{1}}+l_{1}}$ and ${\displaystyle 0\leq l_{1}<2^{m_{1}}}$. Note that ${\displaystyle l_{1}=l/2}$. We have ${\displaystyle f(n)=2f(n/2)-1=2((2l_{1})+1)-1=2l+1}$, where the second equality follows from the induction hypothesis.

If ${\displaystyle n}$ is odd, then choose ${\displaystyle l_{1}}$ and ${\displaystyle m_{1}}$ such that ${\displaystyle (n-1)/2=2^{m_{1}}+l_{1}}$ and ${\displaystyle 0\leq l_{1}<2^{m_{1}}}$. Note that ${\displaystyle l_{1}=(l-1)/2}$. We have ${\displaystyle f(n)=2f((n-1)/2)+1=2((2l_{1})+1)+1=2l+1}$, where the second equality follows from the induction hypothesis. This completes the proof.

We can solve for ${\displaystyle l}$ to get an explicit expression for ${\displaystyle f(n)}$:

${\displaystyle f(n)=2(n-2^{\lfloor \log _{2}(n)\rfloor })+1}$

The most elegant form of the answer involves the binary representation of size ${\displaystyle n}$: ${\displaystyle f(n)}$ can be obtained by a one-bit left cyclic shift of ${\displaystyle n}$ itself. If we represent ${\displaystyle n}$ in binary as ${\displaystyle n=1b_{1}b_{2}b_{3}\dots b_{m}}$, then the solution is given by ${\displaystyle f(n)=b_{1}b_{2}b_{3}\dots b_{m}1}$. The proof of this follows from the representation of ${\displaystyle n}$ as ${\displaystyle 2^{m}+l}$ or from the above expression for ${\displaystyle f(n)}$.

Implementation: If n denotes the number of people, the safe position is given by the function ${\displaystyle f(n)=2l+1}$ ,where ${\displaystyle n=2^{m}+l}$ and ${\displaystyle 0\leq l<2^{m}}$.

Now if we represent the number in binary format, the first bit denotes ${\displaystyle 2^{m}}$ and remaining bits will denote ${\displaystyle l}$. For example, when n=41, its binary representation is

n = 1 0 1 0 0 1

2m = 1 0 0 0 0 0

l = 0 1 0 0 1

    /**
*
* @param n the number of people standing in the circle
* @return the safe position who will survive the execution
*   f(N) = 2L + 1 where N =2^M + L and 0 <= L < 2^M
*/
public int getSafePosition(int n) {
// find value of L for the equation
int valueOfL = n - Integer.highestOneBit(n);
int safePosition = 2 * valueOfL  + 1;

return safePosition;
}


### Bitwise

The easiest way to find the safe position is by using bitwise operators. In this approach, shifting the most-significant set bit of n to the least significant bit will return the safe position.[11] Input must be a positive integer.

n = 1 0 1 0 0 1

n = 0 1 0 0 1 1

    /**
*
* @param n (41) the number of people standing in the circle
* @return the safe position who will survive the execution
* ~Integer.highestOneBit(n*2)
* Multiply n by 2, get the first set bit and take its complement
* ((n<<1) | 1)
* Left Shift n and flipping the last bit
* ~Integer.highestOneBit(n*2) & ((n<<1) | 1)
* Bitwise And to copy bits exists in both operands.
*/
public int getSafePosition(int n) {
return ~Integer.highestOneBit(n*2) & ((n<<1) | 1);
}


### k=3

In 1997, Lorenz Halbeisen and Norbert Hungerbühler discovered a closed-form for the case ${\displaystyle k=3}$. They showed that there is a certain constant

${\displaystyle \alpha \approx 0.8111...}$

that can be computed to arbitrary precision. Given this constant, choose ${\displaystyle m}$ to be the greatest integer such that ${\displaystyle round(\alpha \cdot (3/2)^{m})\leq n}$ (this will be either ${\displaystyle m^{\prime }=round(\log _{3/2}n/\alpha )}$ or ${\displaystyle m^{\prime }-1}$). Then, then the final survivor is

${\displaystyle f(n)=3(n-round(\alpha \cdot (3/2)^{m}))+2}$ if we rounded up else ${\displaystyle 1}$

for all ${\displaystyle n\geq 5}$.

As an example computation, Halbeisen and Hungerbühler give ${\displaystyle n=41,k=3}$ (which is actually the original formulation of Josephus' problem). They compute:

${\displaystyle m^{\prime }\approx round(\log _{3/2}41/0.8111)\approx round(9.68)=10}$
${\displaystyle round(\alpha \cdot (3/2)^{m^{\prime }})\approx round(0.8111\cdot (3/2)^{10})=47}$ and therefore ${\displaystyle m=9}$
${\displaystyle round(0.8111\cdot (3/2)^{9})\approx round(31.18)=31}$ (note that we rounded down)
${\displaystyle f(n)=3(41-31)+1=31}$

We can verify this looking at each successive pass on the numbers ${\displaystyle 1}$ through ${\displaystyle 41}$:

${\displaystyle 1,2,4,5,7,8,10,11,13,14,16,17,19,20,22,23,25,26,28,29,31,32,34,35,37,38,40,41}$
${\displaystyle 2,4,7,8,11,13,16,17,20,22,25,26,29,31,34,35,38,40}$
${\displaystyle 2,4,8,11,16,17,22,25,29,31,35,38}$
${\displaystyle 2,4,11,16,22,25,31,35}$
${\displaystyle 2,4,16,22,31,35}$
${\displaystyle 4,16,31,35}$
${\displaystyle 16,31}$
${\displaystyle 31}$

### The general case

Dynamic programming is used to solve this problem in the general case by performing the first step and then using the solution of the remaining problem. When the index starts from one, then the person at ${\displaystyle s}$ shifts from the first person is in position ${\displaystyle ((s-1){\bmod {n}})+1}$, where n is the total number of persons. Let ${\displaystyle f(n,k)}$ denote the position of the survivor. After the ${\displaystyle k}$-th person is killed, we're left with a circle of ${\displaystyle n-1}$, and we start the next count with the person whose number in the original problem was ${\displaystyle (k{\bmod {n}})+1}$. The position of the survivor in the remaining circle would be ${\displaystyle f(n-1,k)}$ if we start counting at ${\displaystyle 1}$; shifting this to account for the fact that we're starting at ${\displaystyle (k{\bmod {n}})+1}$ yields the recurrence

${\displaystyle f(n,k)=((f(n-1,k)+k-1){\bmod {n}})+1,{\text{ with }}f(1,k)=1\,,}$

which takes the simpler form

${\displaystyle g(n,k)=(g(n-1,k)+k){\bmod {n}},{\text{ with }}g(1,k)=0}$

if we number the positions from ${\displaystyle 0}$ to ${\displaystyle n-1}$ instead.

This approach has running time ${\displaystyle O(n)}$, but for small ${\displaystyle k}$ and large ${\displaystyle n}$ there is another approach. The second approach also uses dynamic programming but has running time ${\displaystyle O(k\log n)}$. It is based on considering killing k-th, 2k-th, ..., ${\displaystyle (\lfloor n/k\rfloor k)}$-th people as one step, then changing the numbering.[citation needed]

This improved approach takes the form

${\displaystyle g(n,k)={\begin{cases}0&{\text{if }}n=1\\(g(n-1,k)+k){\bmod {n}}&{\text{if }}1

## Notes

1. ^ Josephus Problem. A solution to the task Josephus problem in the Rosetta Code, written in Fōrmulæ. The Fōrmulæ wiki. Retrieved September 19, 2019.
2. ^ Flavius Josephus, Wars of the Jews III. 8. 7. (Translation by William Whiston).
3. ^ Dowdy & Mays 1989, p. 125
4. ^ Bachet, C. G. (1612). Problemes Plaisants ed Delectables qui se font par les Nombres. p. 174.
5. ^ Herstein, I. N.; Kaplansky, I. (1974). Matters Mathematical. Harper and Row. pp. 121–126.
6. ^ Zabell, S. L. (1976). "Letter to the editor". Fibonacci Quarterly. 14: 48, 51.
7. ^ Rouse Ball, W. W. (1896). Mathematical Recreations and Essays (2nd ed.). Macmillan.
8. ^ Newman, J. R. (1988). "The World of Mathematics". 4. Tempus: 2403–2405. Cite journal requires |journal= (help)
9. ^ Graham, R. L.; Knuth, D. E.; Patashnik, O. (1989). Concrete Mathematics: A Foundation for Computer Science. Addison Wesley. p. 8. ISBN 978-0-201-14236-5.
10. ^ Robinson, W. J. (1960). "The Josephus Problem". The Mathematical Gazette. 44 (347): 47–52. doi:10.2307/3608532. JSTOR 3608532.
11. ^ "Josephus Problem using Bitwise Operation (Java)". GitHub. January 7, 2018. Retrieved January 7, 2018.