The guide
Who is left standing: the Josephus problem, one recurrence and a binary shortcut
Forty-one people stand in a circle and every third one is removed. A short recurrence says who is left, and when every second one goes, a single rotation of binary digits does the same.
- 31 and 16 the last two places for 41 people and a step of 3
- 2 l + 1 the survivor when every second person goes (n = 2^m + l)
- 977 survivor of 1,000 people when every second goes
- 10^12 people: the survivor is place 900488372225 when every second goes
- 30 or 31 the same survivor counted from 0 or from 1 (41 people, step 3)
Read the full guide Fold the guide
Forty-one people stand in a circle. Counting starts at the first of them. Every third person leaves, and the count carries on round the shrinking circle until one person remains. Who is it?
The last two places are 31 and 16: the person in place 31 survives, and the last one to go stood in place 16. The Wikipedia article on the Josephus problem gives exactly those two places for 41 people and a step of 3. This essay explains where they come from, why a few lines of arithmetic beat any amount of patient counting, and where the neat shortcuts stop working.
A story with a cave in it
The problem carries the name of Flavius Josephus, a Jewish historian and leader of the first century. According to the Wikipedia article, his own account of the siege of Yodfat has him and forty companions trapped in a cave by Roman soldiers. They chose death over capture and settled on drawing lots, each man to be killed by the next. Josephus writes that he and one other man were left until the end, by luck or by the hand of God, and surrendered.
Nothing in that passage mentions a circle or a count of three. The article credits Bachet, who in 1612 suggested arranging the men in a circle and counting by threes. Other tellers changed the numbers. One version has Josephus and 39 comrades with every seventh man eliminated. A medieval version puts thirty people on a ship in a storm, with every ninth person thrown overboard. The details drift from teller to teller. The mathematics does not care.
The article also mentions a Slavonic manuscript of Josephus that tells the story differently: he counted the numbers cunningly and deceived the others, and he had an accomplice. The puzzle then becomes finding two places, one for each survivor. The article says that for a step of 3 he is alleged to have taken the 31st and 16th places. That is why this tool reports the last two places and not only the winner.
A caution belongs here. The article on the siege of Yodfat points out that Josephus’s account is the only account of the battle, and that its credibility has long been a subject of inquiry. The cave is better read as the story that gave a puzzle its name than as a documented method of counting.
One recurrence for any step
Number the people 1 to n, begin counting at 1, and eliminate every k-th person who is still in the circle. Now suppose you could already solve the puzzle for a circle one person smaller. After the first elimination there are n minus 1 people left, and the count restarts at the person after the one who left. All you need is to translate the answer for the smaller circle back into the original numbering.
Counting places from 0 makes that translation tidy. The Wikipedia article gives it as g(1) = 0 and g(n) = (g(n - 1) + k) mod n. Read it aloud. One person alone is the survivor at place 0, and every time a person is added to the circle, the survivor moves k places along, wrapped round the larger circle. The version that counts from 1 differs only by a shift of one.
Try it with k = 3. Then g(2) = (0 + 3) mod 2 = 1, g(3) = (1 + 3) mod 3 = 1, g(4) = (1 + 3) mod 4 = 0 and g(5) = (0 + 3) mod 5 = 3. Counted from 1 those survivors are places 2, 2, 1 and 4, which matches the first entries of OEIS A054995. Forty steps of the same arithmetic end at g(41) = 30.
That 30 is the same person as the 31 above. One is counted from 0 and the other from 1, so a reader who sees 30 or 31 may be looking at a single answer written two ways. It is the easiest slip in the whole subject: a program returns g(n) as it stands and the reader takes it for a place counted from 1.
References also disagree about what the step means. OEIS A054995 describes deleting the integer two places clockwise, while A032434 deletes the integer k minus 1 places clockwise. The first of them notes that counting one place instead of two gives the survivors of the every-second-person game. A step of 3 in one source is a skip of two in another, so the tool states its rule on the page: counting starts at place 1 and the k-th person is removed.
Why every second person is special
When k = 2 the recurrence collapses. On the first pass round the circle, every even-numbered person goes. If n was even, the people left stand in places 1, 3, 5 and so on, and the person now at position x was originally at 2x minus 1. If n was odd, person 1 is the next to go, and the person now at position x was originally at 2x plus 1. The article writes these as f(2j) = 2f(j) - 1 and f(2j + 1) = 2f(j) + 1.
Tabulate the survivors for n from 1 to 16 and a pattern appears: 1, 1, 3, 1, 3, 5, 7, 1, 3, 5, 7, 9, 11, 13, 15, 1. They are odd numbers climbing in steps of two, and they restart at 1 whenever n is a power of 2. OEIS A006257 lists the same sequence.
Write n as 2^m + l, where l is at least 0 and less than 2^m. The survivor is 2 l + 1. For 41 people, 41 = 32 + 9, so l = 9 and the survivor is 2 x 9 + 1 = 19. In binary 41 is 101001, and moving its leading 1 to the end gives 010011, which is 19 again.
The binary form explains why. The leading bit is the 2^m, and the remaining bits are l. Doubling l shifts those bits one place left, and adding 1 puts a new bit at the end. The article calls this a one-bit cyclic shift of n. The article on circular shifts describes the general operation as moving an entry from one end of a sequence to the other, and here the first bit goes to the last place.
Bigger cases cost nothing extra. For 1,000 people, 1,000 = 512 + 488 and the survivor is 977. For a trillion people, 10^12 = 2^39 + 450,244,186,112, so the survivor is place 900488372225. That took a handful of operations, not a trillion steps.
The place eliminated last follows a similar halving rule. If t is that place, then t(2j) = 2t(j) - 1 and t(2j + 1) = 2t(j) + 1 for j of at least 2, with t(2) = 2 and t(3) = 1. This is the tool’s own derivation, by the same argument as the survivor, and it is checked against simulation and against the recurrence rather than taken from a source.
Counting, and why it runs out
The honest way to solve the puzzle is to play it: write the places in a list, count, remove, repeat. For 41 people that is a dozen lines of code and a blink. The method scales badly, because removing an entry from the middle of a list moves everything after it. A circle of a trillion cannot be played at all.
The recurrence still takes one step per person, so it is exact but not free. For a step other than 2, this tool caps n at ten million, which a background worker finishes in a fraction of a second. OEIS A032434 describes a faster recurrence that skips many of those steps when n is large and k is small. The entry says that for n = 10^80 and k = 7 about 1,200 steps are enough. The tool does not use it.
For k = 3 the article reports a closed form found by Halbeisen and Hungerbuhler in 1997. It rests on a constant of about 0.8111 and a power of 3/2. It is nothing like the clean rotation of bits, and the tool does not use that either. Only k = 2 has the shortcut, and applying 2 l + 1 to k = 3 for 41 people gives 19, not 31.
Numbers matter at this scale. A JavaScript number holds integers exactly up to 9,007,199,254,740,991. The tool’s largest case is 10^15 people and its largest survivor is below 2 x 10^15, so both fit. It still does the shifts and powers of two in BigInt and converts back once at the end, which fits MDN’s advice to avoid repeated conversion. The heavy work runs in a web worker, a background thread that keeps the page responsive. The independent check was done in Python, whose integers have unlimited precision.
What the page checks, and what it leaves out
Three comparisons guard the numbers. The recurrence equals a direct simulation for every n from 1 to 159 with k = 1, 2, 3, 4, 5, 7 and 10, which is 1,113 pairs. The closed form 2 l + 1 equals the simulation for every n up to 1,999 with k = 2. The same closed form equals the recurrence for every n up to 1,000,000. The self-check panel reruns them in the browser and reports any mismatch.
The inverse question is handled by running the recurrence once. For a step of 3 and a survivor at place 31, the values of n up to 1,000 are 41, 80, 115, 365 and 809. For a step of 2 and place 1, they are exactly the powers of 2. Every value found is at least the place wanted, since the survivor cannot stand beyond the end of the circle.
Some things are left out on purpose. The tool does not handle a step that changes as the game goes on, several survivors beyond the last two, or the continuous and biased versions. It lists the elimination order only up to 2,000 people, runs the plain simulation as a cross-check only up to 20,000, and refuses a step other than 2 above ten million people with a message that names the cap. Count the circle by hand once, and it is a puzzle. Use the recurrence and it is a column of small sums. Use the binary trick and it is one move of one digit.
Sources
Every source consulted for this guide. Links checked November 11, 2026.
- Josephus problem - Wikipedia Wikipedia (CC BY-SA 4.0)
- Siege of Yodfat - Wikipedia Wikipedia (CC BY-SA 4.0)
- Counting-out game - Wikipedia Wikipedia (CC BY-SA 4.0)
- Circular shift - Wikipedia Wikipedia (CC BY-SA 4.0)
- A006257 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- A054995 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- A032434 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- BigInt - JavaScript | MDN MDN Web Docs (CC BY-SA 2.5 or later)
- Number.MAX_SAFE_INTEGER - JavaScript | MDN MDN Web Docs (CC BY-SA 2.5 or later)
- Web Workers API - Web APIs | MDN MDN Web Docs (CC BY-SA 2.5 or later)
- Built-in Types - Python 3.13 documentation Python Software Foundation (PSF License)