Flavius Josephus, a Jewish-Roman historian from the first century, tells the story like this: A company of 40 soldiers, along with Josephus himself, were trapped in a cave by Roman soldiers during the Siege of Yodfat in 67 A.D. The Jewish soldiers chose to die rather than surrender, so they devised a system to kill off each other until only one person remained. (That last person would be the only one required to die by their own hand.)

All 41 people stood in a circle. The first soldier killed the man to his left, the next surviving soldier killed the man to his left, and so on. Josephus was among the last two men standing, "whether we must say it happened so by chance, or whether by the providence of God," and he convinced the other survivor to surrender rather than die.

This tale may be apocryphal and fantastic, but it gives rise to a fascinating math problem. That is: If you're in a similar situation to Josephus, how do you know where to stand so you will be the last man standing? This is the subject of a new video from the wonderful YouTube channel Numberphile.

If you start running through this sequence with different numbers of people in the starting circle, you will see a few patterns emerge. First of all, the final survivor is never someone in an even-numbered position because all of the people standing in even-numbered positions are killed first (1 kills 2, 3 kills 4, and so on). Do enough trial and error and you might notice that any time the starting number of people is a power of 2, the final person standing is the same as the person who started the sequence (position number 1). This is the key to figuring out where you should stand. When the number of people left standing is equal to a power of 2, then you want it to be your turn to kill your neighbor.

As Numberphile demonstrates, you can use math to determine the winning spot beforehand. You just need to figure out what the highest power of 2 is that is smaller than the starting number of people. For Josephus, the starting number is 41, and the highest power of 2 that is fewer than 41 is 32 (2 to the power of 5). You want it to be your turn when there are exactly 32 people left. Because of the way the problem works, with every other person dying, the position you want to stand in is 2 times the difference between 41 and 32 (41 - 32 = 9), plus 1. So, 2 x 9 + 1 = 19. There's the magic number: Josephus must have been standing in position 19 of the circle (or his fellow survivor was, in which case he was in position 35, second-to-last standing). The video does a bang-up job of explaining this part visually, so give it a watch.

Some quick mental math could help you figure this out in a pinch, but a computer scientist would have an even easier time determining where to stand. When you express your starting number in binary, a quick, simple pattern exists to let you know where you need to stand to live. (You'll just have to watch the video to see the binary shortcut to a quick answer.) So, if you didn't already have enough reason to study computer science, then being able to act quickly when confronted with a problem like Josephus's should be plenty incentive to crack open a textbook on coding.