2007-01-14

Josephus Problem

The fllowing description is copied from www.answers.com.

The Josephus problem is a theoretic problem occurring in computer science and mathematics.

There are n people standing in a circle waiting to be executed. After the first man is executed, k−1 people are skipped and the k-th man is executed. Then again, k−1 people are skipped and the k-th man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains, who is given freedom.

The task is to choose the place in the initial circle so that you survive (remain the last one), given n and k.

History

The problem is named after Flavius Josephus, a Jewish historian living in the 1st century. As the legend goes, he and his 40 comrade soldiers were trapped in a cave, surrounded by Romans. They chose suicide over capture and decided that they will form a circle and start killing themselves using a step of three. As Josephus did not want to die, he was able to find the safe place, and stayed alive, later joining the Romans who captured him.

Solution
Using arrays in Java, copied from csdn
http://codebus.googlepages.com/josephusarray

Solution 2:
Using circularly-linked list
http://codebus.googlepages.com/josephus