Josephus Problem Calculator
Problem
- The Josephus problem seats 41 people in a circle and eliminates every 2th person until one remains; the survivor stands at position 19 counting from 1.
- The recurrence J(1) = 0 and J(n) = (J(n - 1) + 2) mod n builds the answer one circle size at a time, ending at the zero-indexed value 18.
- For step 2 the closed form agrees: with n = 2^5 + 9, the survivor is 2 x 9 + 1 = 19.
- Josephus is the classic recursion teaching example: the same shift-and-wrap idea solves card tricks, elimination games and round-robin scheduling puzzles.
This Josephus problem calculator finds the survivor's position in the classic counting-out puzzle, where people stand in a circle and every k-th person is eliminated until one remains. Enter the number of people and the step size, and the tool reports the winning position, counted with the starting person as position 1. The count is done with the standard Josephus recurrence, J(1) = 0 and J(n) = (J(n-1) + k) mod n, computed in zero-indexed form and then reported 1-indexed so it matches how people actually count. For example, with 10 people and every 3rd eliminated, the survivor is position 4. With 7 people and every 3rd eliminated, the survivor is also position 4. The first person eliminated is always at position k, counting the starting person as 1.
The recurrence is the accepted standard for this calculation because it builds the answer one person at a time, tracking exactly how the survivor's position shifts as each elimination changes the size of the circle. For step 2 there is also a closed form: write n as 2^m + l, and the survivor is 2l + 1. The classic story has 41 soldiers, and with every 2nd person eliminated the survivor is position 19, since 41 = 2^5 + 9 and 2 x 9 + 1 = 19. This page shows the formula, the step-by-step workings, worked examples and sources, so you can follow how each answer is reached rather than just reading the result.
Worked examples
The classic 41 soldiers
(41 · 2)
| Survivor position (1-indexed) | 19 |
| Zero-indexed survivor J(n, k) | 18 |
| People in the circle | 41 |
| Elimination step | every 2th person |
| First eliminated | position 2 |
Ten people, every third out
(10 · 3)
| Survivor position (1-indexed) | 4 |
| Zero-indexed survivor J(n, k) | 3 |
| People in the circle | 10 |
| Elimination step | every 3th person |
| First eliminated | position 3 |
Seven people, every third out
(7 · 3)
| Survivor position (1-indexed) | 4 |
| Zero-indexed survivor J(n, k) | 3 |
| People in the circle | 7 |
| Elimination step | every 3th person |
| First eliminated | position 3 |
The story and the problem
The Josephus problem takes its name from a story told about Flavius Josephus, a first-century historian who, according to the account, was among a group of soldiers trapped in a cave. Rather than surrender, the group arranged themselves in a circle and agreed to count around it, eliminating every k-th person until only one remained. Josephus, the story goes, survived. Whatever the historical truth of that account, the mathematical puzzle it inspired has a clean and exact statement, and a josephus problem calculator exists to do the counting for you.
State the problem precisely: n people stand in a circle, numbered 1 through n. Counting begins at person 1, and every k-th person is eliminated. After each elimination the count resumes with the next person still standing, and the circle closes up as people drop out. The question is simple to ask and sometimes tedious to answer by hand: which position is the last one left? Note one immediate consequence of the setup: the first person eliminated is always at position k, counting the starting person as 1.
The survivor can be found with a recurrence. In zero-indexed form it reads J(1) = 0 and J(n) = (J(n-1) + k) mod n, and the pages here compute it that way before reporting the answer in 1-indexed form to match the numbering of the people. For the special case of step 2 there is even a closed form: write n as 2^m + l, and the survivor is 2l + 1. The classic story's 41 soldiers fit this exactly. With every 2nd person eliminated the survivor is position 19, since 41 = 2^5 + 9 and 2 x 9 + 1 = 19.
Small cases show how the answer moves with n and k. With 10 people and every 3rd eliminated, the survivor is position 4. With 7 people and every 3rd eliminated, the survivor is also position 4 — the same position despite a different-sized circle. Results like these are why running the count by hand is error-prone and why a calculator is useful: changing n or k by one can shift the survivor, leave it unchanged, or move it somewhere unexpected.
Solving by recurrence
The recurrence behind a josephus problem calculator comes from a simple observation: once the first person is removed, the people who remain form a smaller circle with the same rules. The only thing that changes is where counting starts. If you can solve the smaller circle, you can shift its answer back into the original numbering and get the answer for the full circle. That shift is the whole idea, and it is what the calculator computes step by step.
Start with the smallest case. With one person left, that person is the survivor, so J(1) = 0. This is written in zero-indexed form, meaning position 0 is the first person. To move from a circle of n-1 people to a circle of n people, note that the first elimination happens at position k, counting the starting person as 1. After that removal, counting resumes at the person right after the eliminated one, and the remaining n-1 people are relabeled from 0. Solving that smaller circle gives J(n-1). To convert that label back to the original circle, add k and wrap around with mod n: J(n) = (J(n-1) + k) mod n. The calculator runs this recurrence from J(1) = 0 up to J(n), then reports the result 1-indexed by adding 1, since most people count positions starting at 1.
The indexing shift is easy to get wrong, so it helps to see it in action. With 10 people and every 3rd person eliminated, the survivor is position 4. With 7 people and every 3rd person eliminated, the survivor is also position 4. Different circle sizes can land on the same position, which is why the recurrence is worth trusting over intuition. The first person eliminated is always at position k, counting the starting person as 1. That fixed first step is what anchors the shift: the smaller circle's labels begin just after position k, so adding k and taking the result mod n puts the survivor back where it belongs.
For step 2 there is a shortcut that skips the loop. Write n as 2^m + l, where 2^m is the largest power of 2 not greater than n. Then the survivor is 2l + 1. The classic story has 41 soldiers: with every 2nd person eliminated the survivor is position 19, since 41 = 2^5 + 9 and 2 x 9 + 1 = 19. The recurrence gives the same answer, but the closed form shows why the survivor's position moves in a pattern as n grows. For other step sizes there is no such simple formula, so the recurrence remains the general method.
In practice, the calculator builds the recurrence from the bottom up. It starts at J(1) = 0, applies J(n) = (J(n-1) + k) mod n for each larger circle size, and converts the final zero-indexed value to a 1-indexed position. The closed form for step 2 serves as a check on the result, and the worked examples above show the recurrence and the shortcut agreeing.
The step 2 closed form
When the step is exactly 2 — every second person is eliminated — the Josephus problem stops needing a loop. There is a closed form that gives the survivor directly, and it can be stated two ways: as an arithmetic rule, or as a trick with binary digits. Both describe the same answer, and a josephus problem calculator can use either one to check its work.
The arithmetic version: write n as 2^m + l, where 2^m is the largest power of two that does not exceed n and l is whatever is left over. The survivor is then 2l + 1. The binary version says the same thing in a different language. Take n, write it in binary, and rotate its digits left by one place — move the leading 1 to the end. The result is the survivor's position. That single rotation replaces the whole recurrence J(1) = 0 and J(n) = (J(n-1) + k) mod n, which the calculator otherwise evaluates step by step in zero-indexed form and reports 1-indexed.
The classic case verifies both versions. The story has 41 soldiers, and with every second person eliminated the survivor is position 19. Check the arithmetic first: 41 = 2^5 + 9, so m = 5 and l = 9, and 2 x 9 + 1 = 19. Now check the binary rotation. Forty-one in binary is 101001. Rotate those digits left by one place and the leading 1 moves to the end: 010011. Read that back as a number and you get 19. The two methods agree, which is what the closed form promises.
The rotation is easy to do by hand once you are comfortable with binary, and it is a fast sanity check on any answer the calculator produces for step 2. For other steps there is no comparably simple closed form, so the recurrence is the reliable route. Two examples from the calculator show the step matters: with 10 people and every 3rd eliminated, the survivor is position 4, and with 7 people and every 3rd eliminated, the survivor is also position 4. Neither of those comes from the step 2 formula, and applying the binary rotation to them would give the wrong answer.
One related fact is worth keeping separate from the survivor formula. The first person eliminated is always at position k, counting the starting person as 1 — so with step 2 the first to go is position 2, and with step 3 it is position 3. That is a statement about the first elimination, not about who survives, and it holds regardless of how many people are in the circle.
Where Josephus shows up
The Josephus problem looks like a puzzle about ancient soldiers, but the same elimination pattern turns up in settings that have nothing to do with sieges. Any time you repeatedly count a fixed number of items around a circle and remove the one you land on, you are running a Josephus process. A josephus problem calculator lets you test those cases quickly instead of tracing the circle by hand.
Card tricks are one familiar home for it. Magicians use counted-out eliminations to force a spectator's card into a known position, and the arithmetic behind the trick is the same recurrence the calculator uses: J(1) = 0 and J(n) = (J(n-1) + k) mod n, computed in zero-indexed form and reported 1-indexed. Elimination tournaments follow a similar shape, where players are removed in a fixed counting order until one remains, and the survivor's seat can be predicted in advance. Round-robin scheduling and interview problems use the identical structure, often disguised as people in a circle, prisoners, or a hot-potato game.
The counting convention is where most wrong answers come from, so it is worth stating plainly. The first person eliminated is always at position k, counting the starting person as 1. If someone instead counts the starting person as 0, or begins the count at the next person, every result shifts. The calculator reports positions 1-indexed, which matches how most people read a circle of numbered seats.
A few worked cases show how much the step size matters. With 10 people and every 3rd eliminated, the survivor is position 4. With 7 people and every 3rd eliminated, the survivor is also position 4. For step 2 there is a closed form: write n as 2^m + l and the survivor is 2l + 1. The classic story has 41 soldiers: with every 2nd person eliminated the survivor is position 19, since 41 = 2^5 + 9 and 2 x 9 + 1 = 19.
Notice how the closed form makes step-2 cases instant while other step sizes still need the recurrence. That is why a calculator is handy for checking a card trick's setup, seeding a tournament bracket, or verifying an interview answer before you commit to it.
- Card tricks: counted-out eliminations force a card into a known position.
- Elimination tournaments: players drop out in a fixed counting order until one seat remains.
- Round-robin scheduling and interview puzzles: the same circle-and-count structure under a different story.
- Counting conventions matter: the first person eliminated is at position k, counting the starting person as 1, and changing that shifts every answer.
FAQs
Who survives the classic 41 person Josephus problem?
With every 2nd person eliminated in a circle of 41, the survivor is at position 19. The often-quoted answer of 31 belongs to the every-3rd counting convention used in the original story, not to the every-2nd version. So both numbers can be correct depending on the step size you choose.
What is the survivor for 10 people, every 3rd out?
For 10 people counted every 3rd, the survivor is at position 4 when positions are counted from 1. The recurrence gives the zero-indexed value 3, which becomes position 4 once you shift to 1-indexed counting. This page reports results 1-indexed, so you would see 4.
Why do sources disagree on Josephus answers?
Counting conventions differ between sources. One question is whether the eliminated person is the k-th when counting the starter, and another is whether positions start at 0 or 1. This page counts the starter as 1 and reports 1-indexed positions, so results from other tools may differ by one or by step convention.
Is there a formula for general k?
No simple closed form is known for general k. The recurrence used here evaluates in O(n) time, so it handles large circles quickly without needing a formula. That makes the calculator practical even when the number of people is large and the step size is arbitrary.
How does the binary trick work for step 2?
For step 2, write n in binary and move the leading 1 to the units place. Applied to 41, that rotation gives 19, matching the survivor for the classic every-2nd case. The trick works only for k equal to 2, not for general step sizes.
What if k is larger than n?
The modulo in the recurrence handles a step size larger than the circle naturally. Elimination simply wraps around the circle several times per step, and the recurrence still yields the correct survivor. You do not need to adjust k before running the calculation on this page.