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.
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.
The problem is named after Flavius Josephus, a Jewish historian and leader who lived in the 1st century. According to Josephus's 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's 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.
The details of the mechanism used in this feat are rather vague. According to James Dowdy and Michael Mays, [2] 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. [3] 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. [4] A history of the problem can be found in S. L. Zabell's Letter to the editor of the Fibonacci Quarterly . [5]
As to intentionality, Josephus asked: “shall we put it down to divine providence or just to luck?” [6] 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”. [6] [7] 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). [8]
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. [9] 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). [10]
In the following, denotes the number of people in the initial circle, and denotes the count for each step, that is, people are skipped and the -th is executed. The people in the circle are numbered from to , the starting position being and the counting being inclusive.
The problem is explicitly solved when every second person will be killed (every person kills the person on their left or right), i.e. . (For the more general case , a solution is outlined below.) The solution is expressed recursively. Let denote the position of the survivor when there are initially n people (and ). 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 were even, then the person in position x during the second time around the circle was originally in position (for every choice of x). Let . The person at who will now survive was originally in position . This yields the recurrence
If the initial number of people were 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 . This yields the recurrence
When the values are tabulated of and a pattern emerges ( OEIS: A006257 , 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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 1 | 3 | 1 | 3 | 5 | 7 | 1 | 3 | 5 | 7 | 9 | 11 | 13 | 15 | 1 |
This suggests that is an increasing odd sequence that restarts with whenever the index n is a power of 2. Therefore, if m and l are chosen so that and , then . 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 people and it goes to the st person. This person must be the survivor. So . Below, a proof is given by induction.
Theorem: If and , then .
Proof: The strong induction is used on n. The base case is true. The cases are considered separately when n is even and when n is odd.
If n is even, then choose and such that and . Note that . is had where the second equality follows from the induction hypothesis.
If n is odd, then choose and such that and . Note that . 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 :
The most elegant form of the answer involves the binary representation of size n: can be obtained by a one-bit left cyclic shift of n itself. If n is represented in binary as , then the solution is given by . The proof of this follows from the representation of n as or from the above expression for .
Implementation: If n denotes the number of people, the safe position is given by the function , where and .
Now if the number is represented in binary format, the first bit denotes and remaining bits will denote l. For example, when , 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 */publicintgetSafePosition(intn){// find value of L for the equationintvalueOfL=n-Integer.highestOneBit(n);return2*valueOfL+1;}
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 */publicintgetSafePosition(intn){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.}
In 1997, Lorenz Halbeisen and Norbert Hungerbühler discovered a closed-form for the case . They showed that there is a certain constant
that can be computed to arbitrary precision. Given this constant, choose m to be the greatest integer such that (this will be either or ). Then, the final survivor is
for all .
As an example computation, Halbeisen and Hungerbühler give (which is actually the original formulation of Josephus' problem). They compute:
This can be verified by looking at each successive pass on the numbers 1 through 41:
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 shifts from the first person is in position , where n is the total number of people. Let denote the position of the survivor. After the -th person is killed, a circle of remains, and the next count is started with the person whose number in the original problem was . The position of the survivor in the remaining circle would be if counting is started at ; shifting this to account for the fact that the starting point is yields the recurrence [12]
which takes the simpler form
if the positions are numbered from to instead.
This approach has running time , but for small and large there is another approach. The second approach also uses dynamic programming but has running time . It is based on considering killing k-th, 2k-th, ..., -th people as one step, then changing the numbering.[ citation needed ]
This improved approach takes the form
In number theory, Waring's problem asks whether each natural number k has an associated positive integer s such that every natural number is the sum of at most s natural numbers raised to the power k. For example, every natural number is the sum of at most 4 squares, 9 cubes, or 19 fourth powers. Waring's problem was proposed in 1770 by Edward Waring, after whom it is named. Its affirmative answer, known as the Hilbert–Waring theorem, was provided by Hilbert in 1909. Waring's problem has its own Mathematics Subject Classification, 11P05, "Waring's problem and variants".
A triangular wave or triangle wave is a non-sinusoidal waveform named for its triangular shape. It is a periodic, piecewise linear, continuous real function.
In mathematics, the floor function is the function that takes as input a real number x, and gives as output the greatest integer less than or equal to x, denoted ⌊x⌋ or floor(x). Similarly, the ceiling function maps x to the least integer greater than or equal to x, denoted ⌈x⌉ or ceil(x).
HSL and HSV are the two most common cylindrical-coordinate representations of points in an RGB color model. The two representations rearrange the geometry of RGB in an attempt to be more intuitive and perceptually relevant than the cartesian (cube) representation. Developed in the 1970s for computer graphics applications, HSL and HSV are used today in color pickers, in image editing software, and less commonly in image analysis and computer vision.
In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.
In number theory, a formula for primes is a formula generating the prime numbers, exactly and without exception. Formulas for calculating primes do exist; however, they are computationally very slow. A number of constraints are known, showing what such a "formula" can and cannot be.
The Doomsday rule, Doomsday algorithm or Doomsday method is an algorithm of determination of the day of the week for a given date. It provides a perpetual calendar because the Gregorian calendar moves in cycles of 400 years. The algorithm for mental calculation was devised by John Conway in 1973, drawing inspiration from Lewis Carroll's perpetual calendar algorithm. It takes advantage of each year having a certain day of the week upon which certain easy-to-remember dates, called the doomsdays, fall; for example, the last day of February, April 4 (4/4), June 6 (6/6), August 8 (8/8), October 10 (10/10), and December 12 (12/12) all occur on the same day of the week in any year.
In number theory, the integer square root (isqrt) of a non-negative integer n is the non-negative integer m which is the greatest integer less than or equal to the square root of n,
Zeller's congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date. It can be considered to be based on the conversion between Julian day and the calendar date.
In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, called the modulus of the operation.
In mathematics, the Zernike polynomials are a sequence of polynomials that are orthogonal on the unit disk. Named after optical physicist Frits Zernike, laureate of the 1953 Nobel Prize in Physics and the inventor of phase-contrast microscopy, they play important roles in various optics branches such as beam optics and imaging.
An ordinal date is a calendar date typically consisting of a year and an ordinal number, ranging between 1 and 366, representing the multiples of a day, called day of the year or ordinal day number. The two parts of the date can be formatted as "YYYY-DDD" to comply with the ISO 8601 ordinal date format. The year may sometimes be omitted, if it is implied by the context; the day may be generalized from integers to include a decimal part representing a fraction of a day.
The digital root of a natural number in a given radix is the value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached. For example, in base 10, the digital root of the number 12345 is 6 because the sum of the digits in the number is 1 + 2 + 3 + 4 + 5 = 15, then the addition process is repeated again for the resulting number 15, so that the sum of 1 + 5 equals 6, which is the digital root of that number. In base 10, this is equivalent to taking the remainder upon division by 9, which allows it to be used as a divisibility rule.
In number theory, a factorion in a given number base is a natural number that equals the sum of the factorials of its digits. The name factorion was coined by the author Clifford A. Pickover.
In mathematics, specifically in surgery theory, the surgery obstructions define a map from the normal invariants to the L-groups which is in the first instance a set-theoretic map with the following property when :
In modular arithmetic, Barrett reduction is a reduction algorithm introduced in 1986 by P.D. Barrett.
In graph theory, the graph bandwidth problem is to label the n vertices vi of a graph G with distinct integers so that the quantity is minimized . The problem may be visualized as placing the vertices of a graph at distinct integer points along the x-axis so that the length of the longest edge is minimized. Such placement is called linear graph arrangement, linear graph layout or linear graph placement.
In number theory, the prime omega functions and count the number of prime factors of a natural number Thereby counts each distinct prime factor, whereas the related function counts the total number of prime factors of honoring their multiplicity. That is, if we have a prime factorization of of the form for distinct primes , then the respective prime omega functions are given by and . These prime factor counting functions have many important number theoretic relations.
The purpose of this page is to catalog new, interesting, and useful identities related to number-theoretic divisor sums, i.e., sums of an arithmetic function over the divisors of a natural number , or equivalently the Dirichlet convolution of an arithmetic function with one:
A central problem in algorithmic graph theory is the shortest path problem. One of the generalizations of the shortest path problem is known as the single-source-shortest-paths (SSSP) problem, which consists of finding the shortest paths from a source vertex to all other vertices in the graph. There are classical sequential algorithms which solve this problem, such as Dijkstra's algorithm. In this article, however, we present two parallel algorithms solving this problem.