Description (from Rosetta Code)
| Josephus problem is a math puzzle with a grim description: prisoners are standing on a circle, sequentially numbered from to .
An executioner walks along the circle, starting from prisoner , removing every -th prisoner and killing him.
As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed. >
For example, if there are prisoners and , the order the prisoners are killed in (let's call it the "killing sequence") will be 1, 3, 0, and 4, and the survivor will be #2.
Given any , find out which prisoner will be the final survivor.
In one such incident, there were 41 prisoners and every 3rd prisoner was being killed ().
Among them was a clever chap name Josephus who worked out the problem, stood at the surviving position, and lived on to tell the tale.
Which number was he?
The captors may be especially kind and let survivors free, and Josephus might just have friends to save.
Provide a way to calculate which prisoner is at any given position on the killing sequence.
We start with two lists. The first one contains initially the prisoners (their number), and the second one contains the kills, and it is initially empty.
On every kill, the number of the killed prisoner is deleted from the first list, and it is (striked out) appended to the second one, in order to show the order of kills.
In order to leave more than one survivor, the cycle is repeated n-s times, where n is the number of prisoners and s is the number of survivors.
At the end, the two lists are retrieved.
Even when Fōrmulæ is 1-based, the list is filled starting with the 0 prisoner, in order to the results can be compared with other languages (mostly 0-based).
Case 1. Given example
5 prisoners, killing every 2:
Case 2. Task
41 prisoners, killing every 3:
Case 3. Extra
The same as the previous case, but leaving 3 survivors:
Much larger example. 23,482 prisoners, killing every 3,343, leaving 3 survivors. Only the survivors are shown (the first element of the resulting list is extracted):
Additional stuff. Drawing history
The following function creates a raster graphics of size n squares width, and n + 1 squares height, where n is the number of prisoners. The size of the square is defines as pixels.
The horizontal axis (right to left) is the number of the prisoner. The vertical axis (top to bottom) is the number of cycle.
A live prisoner is drawn as green, a dead one is drawn as black.
Drawing for the case 5 prisoners, killing every 2 (cell size 20x20 pixels):
Drawing for the case 41 prisoners, killing every 3 (cell size is 5x5 pixels):
Drawing for the case 500 prisoners, killing every 6 (cell size is 1x1 pixel):
These drawings have several interesting properties:
- If we divided the drawing in horizontal lines, the first one has n green squares and 0 black squares, the second one has n - 1 green squares and 1 black square, ..., the last one has 0 green squares and n black squares, but these squares are not necessarily contiguous.
- Conversely, if we divided the drawing in vertical lines we see a similar pattern, but the lines are not ordered. However, their colors are always contiguous (a first green part, and then a black one).
- The drawing always contains the same number of green and black squares, ½ n(n+1) of each color.
- The drawing is highly ordered at top, gradually changing to be highly disordered at bottom. It resembles a Escher drawing that slightly changes from one scene to another.