Josephus Problem

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

Such games are used to pick out a person from a group, e.g. eeny, meeny, miny, moe.

Josephus Problem
Claude Gaspar Bachet de Méziriac's interpretation of the Josephus problem with 41 soldiers and a step size of 3, showing that places 16 and 31 are last to be killed – time progresses inwards along the spiral, green dots denoting live soldiers, grey dead soldiers, and crosses killings
Josephus Problem
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.

In the particular counting-out game that gives rise to the Josephus problem, a number of 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' firsthand 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.

— Josephus n.d., p. 579, Wars of the Jews, Book III, Ch. 8, para 7

The details of the mechanism used in this feat are rather vague. According to James Dowdy and Michael Mays, 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. 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. A history of the problem can be found in S. L. Zabell's Letter to the editor of the Fibonacci Quarterly.

As to intentionality, Josephus asked: “shall we put it down to divine providence or just to luck?” But the surviving Slavonic manuscript of Josephus tells a different story: that he “counted the numbers cunningly and so managed to deceive all the others”. 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 (for k = 3 below).

Variants and generalizations

Josephus Problem 
Variant of the Josephus problem with 15 Christians and 15 Turks and a step size of 9 – time progresses inwards along the spiral, green dots denoting live soldiers, grey dead soldiers, and crosses killings

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. The Christians need to determine where to stand to ensure that only the Turks are tossed. In other versions the roles of Turks and Christians are interchanged.

Graham, Knuth & Patashnik 1989, p. 8 describe and study a "standard" variant: 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. It is supposed 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).

Solution

Josephus Problem 
Penultimate (pink) and ultimate (ultramarine) places in the Josephus problem for various group size, n and step size, k. In the SVG file, hover over the values to show the full order of killing.

In the following, Josephus Problem  denotes the number of people in the initial circle, and Josephus Problem  denotes the count for each step, that is, Josephus Problem  people are skipped and the Josephus Problem -th is executed. The people in the circle are numbered from Josephus Problem  to Josephus Problem , the starting position being Josephus Problem  and the counting being inclusive.

k = 2

The problem is explicitly solved when every second person will be killed (every person kills the person on their left or right), i.e. Josephus Problem . (For the more general case Josephus Problem , a solution is outlined below.) The solution is expressed recursively. Let Josephus Problem  denote the position of the survivor when there are initially n people (and Josephus Problem ). 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 is as though there were no first time around the circle.

If the initial number of people was even, then the person in position x during the second time around the circle was originally in position Josephus Problem  (for every choice of x). Let Josephus Problem . The person at Josephus Problem  who will now survive was originally in position Josephus Problem . This yields the recurrence

    Josephus Problem 

If the initial number of people was odd, then person 1 can be thought of 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 x was originally in position Josephus Problem . This yields the recurrence

    Josephus Problem 

When the values are tabulated of Josephus Problem  and Josephus Problem  a pattern emerges (OEISA006257, also the leftmost column of blue numbers in the figure above):

n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Josephus Problem  1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1

This suggests that Josephus Problem  is an increasing odd sequence that restarts with Josephus Problem  whenever the index n is a power of 2. Therefore, if m and l are chosen so that Josephus Problem  and Josephus Problem , then Josephus Problem . It is clear that values in the table satisfy this equation. Or it can be thought that after l people are dead there are only Josephus Problem  people and it goes to the Josephus Problem st person. This person must be the survivor. So Josephus Problem . Below, a proof is given by induction.

Theorem: If Josephus Problem  and Josephus Problem , then Josephus Problem .

Proof: The strong induction is used on n. The base case Josephus Problem  is true. The cases are considered separately when n is even and when n is odd.

If n is even, then choose Josephus Problem  and Josephus Problem  such that Josephus Problem  and Josephus Problem . Note that Josephus Problem . Josephus Problem  is had where the second equality follows from the induction hypothesis.

If n is odd, then choose Josephus Problem  and Josephus Problem  such that Josephus Problem  and Josephus Problem . Note that Josephus Problem . Josephus Problem  is had where the second equality follows from the induction hypothesis. This completes the proof.

l can be solved to get an explicit expression for Josephus Problem :

    Josephus Problem 

The most elegant form of the answer involves the binary representation of size n: Josephus Problem  can be obtained by a one-bit left cyclic shift of n itself. If n is represented in binary as Josephus Problem , then the solution is given by Josephus Problem . The proof of this follows from the representation of n as Josephus Problem  or from the above expression for Josephus Problem .

Implementation: If n denotes the number of people, the safe position is given by the function Josephus Problem , where Josephus Problem  and Josephus Problem .

Now if the number is represented in binary format, the first bit denotes Josephus Problem  and remaining bits will denote l. For example, when Josephus Problem , 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); return 2 * valueOfL  + 1; } 

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. 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   */ public int getSafePosition(int n) {     return ~Integer.highestOneBit(n*2) & ((n<<1) | 1);     //     ---------------------- ---  | ------------     //     Get the first set bit   |   | Left Shift n and flipping the last bit     //    and take its complement  |   |     //                             |   |     //                Multiply n by 2  |     //                         Bitwise And to copy bits exists in both operands. } 

k = 3

In 1997, Lorenz Halbeisen and Norbert Hungerbühler discovered a closed-form for the case Josephus Problem . They showed that there is a certain constant

    Josephus Problem 

that can be computed to arbitrary precision. Given this constant, choose m to be the greatest integer such that Josephus Problem  (this will be either Josephus Problem  or Josephus Problem ). Then, the final survivor is

    Josephus Problem  if is rounded up else Josephus Problem 

for all Josephus Problem .

As an example computation, Halbeisen and Hungerbühler give Josephus Problem  (which is actually the original formulation of Josephus' problem). They compute:

    Josephus Problem 
    Josephus Problem  and therefore Josephus Problem 
    Josephus Problem  (note that this has been rounded down)
    Josephus Problem 

This can be verified by looking at each successive pass on the numbers 1 through 41:

    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
    2, 4, 7, 8, 11, 13, 16, 17, 20, 22, 25, 26, 29, 31, 34, 35, 38, 40
    2, 4, 8, 11, 16, 17, 22, 25, 29, 31, 35, 38
    2, 4, 11, 16, 22, 25, 31, 35
    2, 4, 16, 22, 31, 35
    4, 16, 31, 35
    16, 31
    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 Josephus Problem  shifts from the first person is in position Josephus Problem , where n is the total number of people. Let Josephus Problem  denote the position of the survivor. After the Josephus Problem -th person is killed, a circle of Josephus Problem  remains, and the next count is started with the person whose number in the original problem was Josephus Problem . The position of the survivor in the remaining circle would be Josephus Problem  if counting is started at Josephus Problem ; shifting this to account for the fact that the starting point is Josephus Problem  yields the recurrence

    Josephus Problem 

which takes the simpler form

    Josephus Problem 

if the positions are numbered from Josephus Problem  to Josephus Problem  instead.

This approach has running time Josephus Problem , but for small Josephus Problem  and large Josephus Problem  there is another approach. The second approach also uses dynamic programming but has running time Josephus Problem . It is based on considering killing k-th, 2k-th, ..., Josephus Problem -th people as one step, then changing the numbering.[citation needed]

This improved approach takes the form

    Josephus Problem 

References

Citations

Sources

Further reading

Tags:

Josephus Problem HistoryJosephus Problem Variants and generalizationsJosephus Problem SolutionJosephus ProblemComputer scienceCounting-out gameEeny, meeny, miny, moeMathematics

🔥 Trending searches on Wiki English:

Canada2023 IBA Women's World Boxing ChampionshipsPortugal national football teamHarry PotterList of school shootings in the United States (2000–present)Jason MomoaWhitney HoustonGibraltarJason SegelLeonardo da VinciVande Bharat ExpressChabeloChelsea F.C.Priyanka ChopraIOSAnnie LennoxChinaErin DarkeBRICSAriana GrandeJuliette LewisKeffalsMississippiCaleb PlantYugoslav coup d'étatJ. Robert OppenheimerLiz ParnovList of mass shootings in the United States in 2023Vikram Sarabhai2023 Turkey–Syria earthquakeHong ChauTornado outbreak of March 24–26, 2023The Mandalorian (season 3)Mateo ReteguiSteven SpielbergCleopatraList of highest-grossing Indian filmsJennifer AnistonYou (season 4)List of most-liked Instagram postsLewis HamiltonVinayak Damodar SavarkarBernie Nolan2020 United States presidential electionUnited KingdomArtificial intelligenceSalman KhanAnthony VolpeJason David FrankGPT-4XXXDemi MooreMaldivesVal KilmerMacOSAnup SoniFIFA World CupBrazilFreddie MercuryGrey's AnatomySelena GomezJessie Mei LiRothschild familyLeonardo DiCaprioDavid KoreshDavid (Michelangelo)Suits indexJason BatemanHailey Van Lith2022 Russian invasion of UkraineList of countries and dependencies by populationChor Nikal Ke BhagaIrelandRam CharanSingaporeFleetwood MacTed Lasso2023 Pennsylvania chocolate factory explosionPriscilla Presley🡆 More