⚔️ Josephus Problem

The Survival Strategy

Interactive Simulation

The Legend

The Jewish historian Josephus Flavius (37-100 CE) recounted how he and 40 companions were trapped in a cave during the Roman siege. Preferring death to capture, they decided to form a circle and eliminate every third person until one remained. Josephus, skilled in mathematics, calculated the safe position—and survived to tell the tale.

The Mathematics

Problem: n people stand in a circle. Starting from position 1, eliminate every k-th person. Which position survives?

Recurrence: J(n, k) = (J(n-1, k) + k) mod n, with J(1, k) = 0 (zero-indexed)

Special case k=2: When eliminating every second person, a clever binary trick exists. If n = 2^m + l where 0 ≤ l < 2^m, then J(n, 2) = 2l + 1 (one-indexed).

Example: For n=41, we have 41 = 32 + 9 = 2^5 + 9, so J(41, 2) = 2(9) + 1 = 19

Strategy

To survive, stand at position J(n, k). In Josephus's case with n=41 and k=3, legend says he chose position 31 or calculated the safe spot through his mathematical prowess.