PuzzleReels
Sections

Last One Standing

The maths · Episode 21 · 2:58 · gives the answer

The puzzle

41 people stand in a circle, numbered 1 to 41 clockwise. The rule is the film's, stated exactly because the answer depends on it:

  • Seats are numbered 1 to N, clockwise.
  • Counting begins at seat 1, and that person is safe on the first pass.
  • Seat 2 is out first, then seat 4, then seat 6: skip one, remove one, round and round.

The counting never restarts. When someone is removed, the count carries on from the next person still standing. Play until one person is left. Which seat is that?

The maths, properly

The rule, made exact

Picture a counter that sits on one person. The rule is: the person with the counter stays, the next person clockwise is removed, and the counter moves to the next person still standing. The counter starts on seat 1.

Everything that happens next depends only on the order of the people round the circle and on who holds the counter. So suppose some people have been removed and n remain, with the counter on one of them. What follows is exactly the original game on n people, with the people renamed 1,2,…,n clockwise starting from the counter. If the renamed survivor is i, we only need to say which real seat i is. That one idea gives everything below.

Proof one: two recurrences, then induction

Let J(N) be the surviving seat for N people. Clearly J(1)=1.

An even circle, N=2k. The counter is on seat 1, removes seat 2, moves to seat 3, removes seat 4, and so on. Seat 2j is removed by the counter on seat 2j−1. After seat 2k goes, the counter passes to the next person standing, which is seat 1. One lap has removed every even seat and nothing else, and the counter is back on seat 1.

The k people left are seats 1,3,5,…,2k−1, with the counter on the first. Renamed from the counter, person i is seat 2i−1. By the idea above the rest is the game on k people, so

J(2k)=2J(k)−1

An odd circle, N=2k+1 with k≥1. The first k removals are seats 2,4,…,2k as before, and the counter then moves to seat 2k+1. The count carries past the end of the circle, so the counter on seat 2k+1 removes the next person clockwise, which is seat 1. The counter moves to seat 3.

The k people left are seats 3,5,…,2k+1, with the counter on the first. Person i is seat 2i+1, so

J(2k+1)=2J(k)+1

Notice how the two cases differ. In the even circle seat 1 stays and becomes the first person. In the odd circle seat 1 is removed on the way round, and seat 3 becomes the first person.

The closed form, by strong induction on N. Claim: if N=2m+L with 0≤L<2m, then J(N)=2L+1.

Base case: N=1 is 20+0, and J(1)=1=2×0+1.

Step: take N≥2 and assume the claim for every smaller number of people.

  • If N is even, then m≥1 and L is even, because 2m is even. Put N=2k. Then k=2m−1+L/2 with 0≤L/2<2m−1, so by hypothesis J(k)=2·(L/2)+1=L+1. The even recurrence gives J(N)=2(L+1)−1=2L+1.
  • If N is odd and at least 3, then m≥1 and L is odd. Put N=2k+1. Then k=2m−1+(L−1)/2 with 0≤(L−1)/2<2m−1, so J(k)=2·(L−1)/2+1=L. The odd recurrence gives J(N)=2L+1.

Either way the claim holds for N, so it holds for every N. Since 2L+1 is odd, the survivor is always an odd seat.

Proof two: a circle of 2m people

This is the film's picture of halving, and it needs no recurrence for the odd case.

A power of two. With 2m people, seat 1 survives. Induct on m. For m=0 there is one person. For m≥1 one lap removes every even seat and returns the counter to seat 1, leaving 2m−1 people with seat 1 as the first. By hypothesis the first person survives, so seat 1 does.

Any number. Take 2m+L people with 0≤L<2m. The first L removals are seats 2,4,…,2L, all in the first lap, since 2L≤2m+L. The counter then stands on seat 2L+1, which exists and has not yet been touched, because 2L+1≤2m+L exactly when L<2m. Exactly 2m people remain, and the counter is on one of them. That is the game on a power of two, where the person holding the counter survives. So the survivor is seat 2L+1. This is the same answer, reached with no induction beyond the power of two.

Proof three: why the digits rotate

Write L with exactly m binary digits bm−1…b0, allowing leading zeros. Then N=2m+L is the m+1 digits

1bm−1…b0

and 2L+1 is L shifted one place left with a 1 in the units place:

bm−1…b01

That is the same string with its leading 1 moved to the end. It has m+1 digits too, and may start with a zero, which is simply dropped when you read it as a number.

Two special cases follow at once. A power of two is a single 1 followed by m zeros, which rotates to 0…01, which is 1. And N=2m−1 is all ones, which rotates to itself, so J(N)=N: the last seat. The converse holds as well. If J(N)=N then 2L+1=2m+L, so L=2m−1 and N is all ones. Likewise 2L+1=1 forces L=0, so seat 1 survives only when N is a power of two.

The halving chain in the film is this same fact seen one digit at a time. Taking N to ⌊N/2⌋ deletes its last binary digit, and the recurrences say that the last digit of N decides which of 2J(k)−1 and 2J(k)+1 you want. A process that keeps halving a number is a process about its binary digits.

A worked case: 41

The film walks the recurrences down from 41, halving each time, until one person is left, then climbs back up. Each row uses the odd or even rule on the row below it.

nruleworkingJ(n)
41odd2×9+119
20even2×5−19
10even2×3−15
5odd2×1+13
2even2×1−11
1base case1

You can see the first step in the removals themselves. The first lap takes seats 2, 4, 6 and so on up to seat 40. The count then carries past seat 41 and takes seat 1. That leaves the odd seats from 3 to 41, which is the game on 20 people, renamed i↦2i+1. Its survivor is person 9, which is seat 2×9+1=19.

By the closed form: 41=32+9, so J=2×9+1=19. By the digits: 41 is 101001 in binary. Move the leading 1 to the end and you get 010011, which is 19.

The film's other examples

NbinaryrotatedJ(N)
4110100101001119
131101101111
1001100100100100173
8100000011
71111117

The last two rows are the special cases. 8 is a one and zeros, so the survivor is seat 1. 7 is all ones, so the survivor is seat 7, the last.

The table

Here is J(N) for every N from 1 to 16, written out by the recurrences. It falls back to 1 at the powers of two (1, 2, 4, 8, 16) and climbs by 2 in between.

N12345678910111213141516
J1131357135791113151

The small cases the viewer is asked to do by hand are in it. For 5 people the removals go 2, 4, 1, 5 and seat 3 stands.

Generalising

A million people. With 1,000,000 people, the largest power of two not above that is 524,288. So L is 475,712, and the survivor is seat 2L+1, which is 951,425. In binary the move is the same single one: 11110100001001000000 becomes 11101000010010000001. Nobody could simulate it by hand. The formula takes one subtraction.

A linear method that does not need binary. Number the seats from 0 and let g(n) be the survivor's number. After the first removal, which takes seat 1, the counter sits on seat 2, and n−1 people remain. Rename them from the counter: an old seat p becomes p−2 modulo n. So g(n)=(g(n−1)+2)modn, with g(1)=0, and the survivor is seat g(n)+1. This runs in n steps for any N. The verifier uses it as an independent check on the binary rule.

Every third person, as in the old story. The same argument for "remove every q-th person" gives g(n)=(g(n−1)+q)modn. The doubling recurrences and the binary rotation belong to every second person only. The film uses every second.

What changes if the puzzle changes

Who goes first. The answer depends on the convention, which is why the film puts it on screen. If the counter started so that seat 1 were removed first, the circle would be the film's circle with every seat moved one place round. The survivor would be one seat earlier than J(N) (the last seat if J(N) is 1). The verifier checks only the film's convention, so this variant is argued here and not computed.

Round numbers. The film's dial stops at a hundred, a thousand and a million. The survivors the verifier records are 73 for 100 people, 977 for 1000 and 951,425 for 1,000,000.

Checking it

scripts/josephus.mjs plays the circle out literally for every N from 1 to 1999, removing people from a list one at a time. For each N it asserts three things: the simulated survivor equals the binary rotation, equals 2L+1, and is odd. It asserts that both recurrences hold for every N in that range, and that seat 1 survives exactly at the powers of two. It also checks the table above, the spot values quoted in the film, and that the recorded kill order of each ring the film draws is a permutation of the seats that ends on the survivor. The 41 case is asserted to start with the even seats and then seat 1, and to leave seat 19.

The million is not simulated. The linear recurrence above runs for every n up to 1,000,000. It is compared with the simulation at every n up to 1999, and with the binary rotation at regular intervals and at the end. The script records 0 mismatches. A failure anywhere stops the build.

What is not checked by computation: the proof for all N. The induction above does that, and the simulation tests it only up to 1999. The script does not check any convention other than the film's, or any rule other than every second person.

Where this shows up

  • The name comes from the historian Flavius Josephus, and the story told about him: trapped in a cave with a band of soldiers who preferred death to capture, he is said to have arranged the order in which they would be killed so that he was one of the last two left. In the classic telling the count is every third person, and there are forty-one men. The film keeps forty-one and counts every second. The page does not vouch for the details of the old story.
  • Binary in a ring. The problem is a standard example of a recurrence solved by finding its pattern. The first chapter of Concrete Mathematics by Graham, Knuth and Patashnik works through it and finds the same recurrences and the same one-digit rotation for every second person.
  • Why the trick works at all. Each lap halves the circle and leaves the same problem on half as many people. Anything with that shape is likely to have an answer written in base 2.

Try it yourself

1. Who survives with 100 people?

In binary 100 is 1100100. Move the leading 1 to the end to get 1001001, which is 73. The closed form agrees: take away the largest power of two not above 100, double what is left and add one.

2. For which N does the last seat survive?

We need 2L+1=N=2m+L, which means L=2m−1 and so N=2m+1−1. These are the numbers whose binary digits are all ones, such as 7, for which the survivor is seat 7.

3. Why is the survivor always odd?

The closed form is 2L+1, which is odd. In the game itself: J(1)=1 is odd, and each recurrence turns the survivor of the smaller game into 2J(k)−1 or 2J(k)+1, which is odd whatever J(k) is.

4. What goes wrong with the recurrence if you count every third person?

A lap now removes a third of the circle, not half, and unless N is a multiple of 3 the lap ends part of the way through a trio, so where the next lap starts depends on the remainder. The tidy pair of doubling recurrences is gone, and with it the rotation of binary digits. The linear recurrence g(n)=(g(n−1)+3)modn still works. The verifier does not test it.