Josephus' Circle

With nearly 750 responses, and solutions written in everything from ABAP to MUMPS to XSLT, I’d say that last week’s Programming Praxis (Russian Peasant Multiplication) was certainly a success. The comments are most certainly worth a read, if nothing else but to see things like the circuit diagram solution, something done entirely using regular expressions, and some obscure childrens' language called Baltie 3. That said, I'm excited to present this next Programming Praxis.

Titus Flavius Josephus was an important first-century historian. Having survived the siege and destruction of Jerusalem in 70 AD, he authored several works on Jewish history, including The Jewish War and Antiquities of the Jews. Not only have his writings given valuable insight into first century Judaism, they provide an extra-Biblical account of early Christianity. But aside from the books, and the writings, and all of his other invaluable contributions to history, Josephus also told the story of how he had escaped death by quickly standing in the "safe spot" of what is now called Josephus’ Circle.

As the legend goes, Josephus and forty of his fellow soldiers retreated from the siege of Yodfat to a small cave in the hills. Surrounded by the Roman legion with no chance of escape, the soldiers saw no other choice but to commit suicide. Like the people of Masada, they would rather die than face capture.

In order to decide who would die in which order, the soldiers stood in a circle and, starting with the top of the circle and continuing clockwise, counted to three. The third man got the ax and the counting resumed at one. The process continued until there was no one left. Josephus, who didn't quite agree with the whole "we should all kill ourselves" idea, figured out the perfect way to avoid death: be the last man standing.

Of course, the above illustration has only twelve men, and Josephus somehow managed to figure out — very quickly — where to stand with forty others.

Your challenge: write the Josephus function. There should be two inputs (number of soldiers, soldiers to skip) and one output (index of the safe spot). And if you'd like, post your solution in the comments.