← All tools

Josephus Problem Solver

The Josephus problem: n people stand in a circle and count off, eliminating every k-th person until one remains. Find the survivor's position, the full elimination order, and — for k = 2 — the elegant closed form. The story goes that the historian Flavius Josephus, trapped with 40 soldiers in a cave and choosing to die by lot rather than surrender, used exactly this mathematics to position himself (and a friend) among the last survivors. Beyond the legend, the same counting-out recurrence underpins nursery rhymes (eeny, meeny, miny, moe) and round-robin scheduling. Everything runs locally in your browser.

People (n)
Step (k)

Survivor

Elimination order

k = 2 closed form

The Josephus problem. Number the people 0…n−1 around the circle. Let g(i) be the survivor's index among i people; then g(1) = 0 and g(i) = (g(i−1) + k) mod i — adding k each round accounts for the next elimination and the rotation, and the survivor's index shifts into the new, smaller circle. This O(n) recurrence gives the answer directly. The elimination order (the Josephus permutation) is the sequence in which people are removed; its last entry is the survivor. For the special case k = 2 there is a neat closed form: write n = 2a + ℓ (with ℓ < 2a), and the survivor's 1-based position is 2ℓ + 1 (0-based 2ℓ) — so for n = 41 = 32 + 9 the survivor is position 19, and for any power of two the survivor is position 1. Pairs with the Collatz Conjecture, Kaprekar's Routine, and N-Queens tools. Everything runs locally — nothing leaves your browser.