ExplorationsThe Josephus problem, and the answer you can read off in binary

Counting Around the Circle

Exhaustively checkedrecursionbinarycountingcurious

Number people from 1 around a circle, count person 1 as the first, and remove every second person while continuing around the ring. With five people, the removals are 2, 4, 1 and 5, leaving person 3. For this counting rule, moving the leading 1 in the group size’s notation to the end gives the survivor’s position.

Current view: The circle

The circle emptying, one count and one departure at a time.

The circle · 5 people, counting to 2person 3 survives
132134254person 3 survivesperson 5 leaves
Every count and departure, as a table
Departures from a circle of 5 counting to 2, in order, ending with the survivor
StepThe count lands onLeavesStill standing
11, 224
23, 443
35, 112
43, 551

Person 3 is left standing, and is the one row not in this table: nothing ever happens to them.

person
one place in the circle, numbered clockwise from the top
gone
a place already counted out, marked with the step it left at
the count
the line the counting walks, stepping over the places already empty
the survivor
the one place the counting never reaches

Each disc is a person, numbered clockwise from the top with person 1 at twelve o’clock. A filled disc is still standing and a hollow one has gone, marked with the step at which they left. During a count the discs the counting lands on are joined by a line, so you can see the count skipping over people who have already left, and the last disc that line reaches is the one that goes.

Of 5 people counting to two, person 3 is left standing - which is 101 with its leading 1 moved to the end, read as 011.

People
5
Count
2
Survivor
3

The circle

Numbered 1 upwards, clockwise from the top. Up to 200, which is where the numbers stop being readable.

Step by step

Use Step for one change you can inspect, or Run to watch the changes accumulate.

Speed
Advanced parameters
Count

Counting to two is the case with a rule you can apply in your head: write the number of people in binary and move the leading 1 to the end.

Cases worth seeing

Try this

Set the circle to 100 people counting to two and work out the survivor in your head before pressing Run. What is 100 in binary, and what does moving its leading 1 to the end give you?

1/3

What is going on

Each explanation begins with a worked example and follows the same operation through intuition, formal statements and proofs. Later sections distinguish what is established from questions that remain open, so you can follow the level of detail useful to you.

Worked by handFive people, counting to two: the circle emptied by hand, one count at a time, until person 3 is left standing.
  1. 1 2 3 4 5count 1, 21 3 4 5person 2 leaves
  2. 1 3 4 5count 3, 41 3 5person 4 leaves
  3. 1 3 5count 5, 13 5the count runs off the end and wraps round, so person 1 leaves
  4. 3 5count 3, 53person 5 leaves, and person 3 is left standing

The laboratory draws that circle rather than listing it: each person is a disc numbered clockwise from the top, the count is drawn as a line from disc to disc, and whoever it reaches last is hollowed out and labelled with the step at which they left.

Five is 101 in . Move the leading 1 to the far end and it reads 011, which is 3 - the person who survived. Forty-one is 101001, which rotates to 010011, or 19; and a circle of forty-one people counting to two does leave person 19 standing. The answer to a question about people standing in a ring is one of a number moved from one end to the other.

01

What you are seeing

A plain description of the process

Number people 1 to around a circle. Start counting at person 1. Every th person leaves the circle, and the count carries straight on from where it stopped, skipping anybody who has already gone. Eventually one person is left.

With five people counting to two, the count reaches 1 and 2, so 2 leaves. It reaches 3 and 4, so 4 leaves. It reaches 5 and then wraps round to 1, so 1 leaves. It reaches 3 and 5, so 5 leaves. Person 3 is the survivor.

The question is not what happens, which is only bookkeeping. It is whether you can say who survives without doing the bookkeeping - and for you can, in one line, for any circle at all.

02

Why it starts to make sense

Intuition, before any algebra

Watch one lap of the circle at . Everybody in an even position leaves, and everybody in an odd position is still standing. That is the entire first lap, and it has halved the problem.

Now the useful case. Suppose there are exactly people. After one lap, 2, 4, 6 and 8 have gone, four people remain, and the count is about to reach person 1 again - exactly as it did at the very start, with a circle that is again a . So the same thing happens on the next lap, and the next, and person 1 is untouchable. Try 2, 4, 16 or 32 in the laboratory and person 1 wins every time.

That is the pivot. If a circle whose size is a always leaves the person the count started on, then any other circle only has to be reduced to one, and reducing it means removing enough people to bring the count round to a fresh start on a power-of-two circle. How many is "enough"? Exactly the above the previous power of two.

03

The key idea

Write the number in and move one digit

Write the circle size as

which is just "the largest that fits, plus what is left over". Then, counting to two, the survivor is .

Here is what makes that worth a page rather than a footnote. In , is the leading 1 of , and is every digit after it. Doubling shifts those digits one place to the left, and adding one puts a 1 in the units place. So:

Take the of , move the leading 1 from the front to the back, and read the result. That is where to stand.

Forty-one is . Moving the leading 1 gives , which is 19. A hundred is , which becomes , or 73. A is a 1 followed by zeroes, so it becomes zeroes followed by a 1: person 1, every time, and only for those sizes.

None of this survives a change of count. At the rotation gives 19 for a circle of 41 and the survivor is 31, and it is wrong for most sizes below 64. The laboratory therefore withdraws the view at other counts instead of drawing a rule that does not apply there.

04

Formal statement

Precisely what is being claimed

Let be the label of the survivor in a circle of people, counting from person 1 and removing every th.

  1. and for . Proved, for every .
  2. If with , then . Proved.
  3. is the of rotated left by one place. Proved, and equivalent to 2.
  4. if and only if is a . Proved, and immediate from 2.

Statement 1 is the only one that mentions at all, and that is the honest summary of this page: the general problem has a , and the reading has a rule only at .

05

Proof

The whole argument, for counting to two

The . In a circle of , the first count removes person . What is left is a circle of people, and the next count begins at person . That is the same problem with a different set of labels, so if the smaller circle's survivor sits at offset from its own starting person, the original circle's survivor sits at offset from person 1, counted round. Reducing and returning to 1-based labels gives the stated formula. ∎

A power-of-two circle leaves the person the count starts on. on . For there is one person and nothing to do. For , one lap of a circle of removes every person in an even position, which is exactly half of them, and returns the count to the person it began with, now standing in a circle of . By induction that person survives. ∎

The closed form. Let with . Beginning at person 1 and counting to two, the first departures are persons , after which people remain and the next count begins at person . By the previous paragraph that person survives, so . ∎

The rotation. The of is a 1 followed by the of , padded with leading zeroes. Deleting that leading 1 leaves those digits, which read as ; appending a 1 on the right produces . So rotating the leading bit to the end computes exactly the closed form. ∎

And person 1. exactly when , exactly when . ∎

What is not proved. Nothing above says anything about . The pivot was that one lap at removes precisely half the circle and lands the count back at a starting position, and no part of that survives when a third of the circle goes each lap and the leftover does not divide the way it needs to.

06

Limits and frontier

Where this page stops being able to help

What this laboratory computes. The circle is simulated person by person up to 200 of them, which is where numbered discs stop being distinguishable rather than where the arithmetic stops being exact. Above that the survivor still comes from the , which is linear in and holds up to a million, and the page says it has stopped drawing rather than pretending the picture is there.

The was about counting to two, and it does not transfer. This is the part worth carrying away. The argument in layer 5 is complete and elementary, and it is also extremely specific: it turns on a lap at deleting exactly the even positions. Change the count to three and the survivor is still perfectly computable, one departure at a time, and this page can offer no rule for reading it off. That is a statement about what this page establishes. A description of the case does exist in the literature, and it is nothing like moving a ; the difference in difficulty between the two questions is not visible anywhere in how they are stated.

Read this next to Reverse, Add, and 196. Both rules are one sentence long and both can be run by a child with counters. One of them has an answer you can read off a number's ; the other has swallowed a billion digits of computation without anybody being able to say anything at all. From the outside there is no way to tell which kind of rule you are looking at, and finding out is the entire work.

What is actually established

Every statement on this page, with its status, its exact scope, and the date that status was last checked.

Proved

Writing J(n, k) for the survivor of a circle of n counting to k, J(1, k) = 1 and J(n, k) = ((J(n − 1, k) + k − 1) mod n) + 1.

Scope
Every circle size n ≥ 1 and every count k ≥ 1.
Why
The first count removes person k, and what remains is the same problem on n − 1 people whose numbering starts at person k + 1. Relabelling that smaller circle and translating the answer back is the whole argument. It is what the laboratory computes for circles too large to draw.
Status checked
Proved

For k = 2, writing n = 2^m + l with 0 ≤ l < 2^m, the survivor is 2l + 1.

Scope
Every circle size n ≥ 1, counting to two.
Why
One pass round a circle of 2^m + l people removes l even-numbered people and leaves a circle of 2^m starting at person 2l + 1; a circle whose size is a power of two returns to whoever the count started on. Induction on m finishes it. This is the argument in Concrete Mathematics §1.3, and it is the reason this page can answer a question without running it.
Status checked
Proved

For k = 2, the survivor is the binary representation of n with its leading 1 moved to the far end.

Scope
Every circle size n ≥ 1, counting to two.
Why
The leading 1 of n is 2^m and the digits after it are l. Deleting that leading 1 leaves l, appending a 1 shifts it left one place and adds one, and 2l + 1 is the closed form above. The rotation is not an analogy for the formula; it is the same arithmetic written in the other notation.
Status checked
Proved

For k = 2, person 1 survives exactly when n is a power of two.

Scope
Every circle size n ≥ 1, counting to two.
Why
The survivor 2l + 1 equals 1 precisely when l = 0, which is precisely when n = 2^m. Both directions follow from the closed form, so this is a proof rather than a pattern noticed in the table - though the table does show it, at 2, 4, 8, 16, 32 and 64.
Status checked
Exhaustively checked

In a circle of 41 counting to three, the survivor is person 31, and the departures run 3, 6, 9, … in that order.

Scope
The single circle of 41 people counting to three, emptied to the last person. One case, checked completely, and nothing beyond it.
Why
The classical telling of this puzzle. It is marked as checked rather than proved because that is exactly what was done here: this one circle was emptied, three separate ways, and they agreed. No argument on this page establishes it in general.
Computation (exact)
One circle of 41 people, simulated to completion in exact integer arithmetic, and cross-checked against the O(n) recurrence and against an independent circular linked list in the test suite. Circles are simulated up to 200 people and the recurrence is walked up to 1,000,000. Algorithm josephus-1.
Status checked
Exhaustively checked

The circle drawn here and the formula quoted here give the same survivor for every size and count the laboratory offers.

Scope
Every circle from 1 to 200 people at every count from 1 to 100, and every circle from 1 to 500 at counts 2 to 10 against a second implementation.
Why
A page that draws one computation and asserts another has to check that they are the same computation. The full order of departures is compared, not merely the survivor: a count that starts in the wrong place can still land on the right person by coincidence, and did so once while this kernel was being written.
Computation (exact)
Exact integer arithmetic throughout, run in the test suite rather than in your browser: the array simulation, the O(n) recurrence and a circular linked list, compared position by position. Algorithm josephus-1.
Status checked
Observed in this run

At k = 3 the binary rotation is simply wrong: for more than forty of the sizes from 1 to 64 it names somebody other than the survivor.

Scope
Circle sizes 1 to 64 counting to three, compared against the k = 2 rotation rule by this page.
Why
What this page found, and no more than that. It is not a claim that no rule exists for k = 3: a description of that case is in the literature, and it is nothing like moving one bit. What the laboratory can honestly show is that the rule it teaches at k = 2 does not survive the change, and that the survivor there has to be computed rather than read.
Status checked

Sources

Review notes show which bibliographic details Mathomaly has checked and which remain unresolved. Checking a publication record does not independently verify its proof.

  1. Ronald L. Graham, Donald E. Knuth, Oren Patashnik, Concrete Mathematics: A Foundation for Computer Science, Addison-Wesley, second edition, 1994. Link

    Develops the Josephus recurrence and its binary closed form when every second person is removed. The laboratory derives that special case independently.

    Bibliographic record checked. This is not an independent verification of the proof.

    Bibliographic review:

    AI-assisted bibliographic audit. Evidence type: author record.

    Author's book page confirms title, three authors and second edition.

  2. Andrew M. Odlyzko, Herbert S. Wilf, Functional iteration and the Josephus problem, Glasgow Mathematical Journal 33(2), pp. 235–240, 1991. Link

    Studies the Josephus problem beyond the binary special case. The laboratory does not reproduce the paper's general results.

    Bibliographic record checked. This is not an independent verification of the proof.

    Bibliographic review:

    AI-assisted bibliographic audit. Evidence type: publisher record.

    Cambridge-supplied record confirms both authors, 1991 and volume 33(2), pages 235–240.

  3. A006257 - Josephus problem: a(2n) = 2a(n) - 1, a(2n+1) = 2a(n) + 1, The On-Line Encyclopedia of Integer Sequences. Link

    The k = 2 survivors, 0-indexed there and 1-indexed here: the published listing this page checks its sawtooth against.

    Bibliographic record checked. This is not an independent verification of the proof.

    Bibliographic review:

    AI-assisted bibliographic audit. Evidence type: source text.

    Checked the sequence identifier, definition and displayed initial terms on the database entry.

Connected by how they work, not by sharing a topic label.