Wednesday, 29 February 2012

The 100 Prisoners Problem

Swami Gulagulaananda asked:
"Where would you stand to save your life?"

Here is a simple problem. Go ahead and try to solve it...

In an island are stranded 100 people and you are one among them. You were captured by tribals, who say that only one among the 100 can survive. And here is the condition. One of you is given an axe. And he has to hack and kill the guy to his right, and pass the axe to the guy after him. And the cycle continues...

So, if the people were numbered 1, 2, 3.... 100, then 1 kills 2, and gives to 3. 3 kills 4 and gives it to 5 and so on. The 99th guy kills 100th guy and passes it back to 1, who kills number 3 and so on. The question is, if you were in that group, where would you stand to be the last man standing?

And the solution is... *Spoiler Alert*
There are 3 ways of solving this.

Method I
One, is by finding a pattern. Do it for groups of 1, 2, 3 and so on... say till 10.
If the group had 1 guy, survivor = 1
If the group had 2 guys, survivor = 1
If the group had 3 guys, survivor = 3
If the group had 4 guys, survivor = 1
If the group had 5 guys, survivor = 3
If the group had 6 guys, survivor = 5
If the group had 7 guys, survivor = 7
If the group had 8 guys, survivor = 1
Just continue, you will see the pattern. For every number where the value is equal to 2 to power of n, as in 1, 2, 4, 8... the survivor is person 1. For any other number, the survivor is = 1 + (2 times the difference between the nearest two power number).

The answer by the way is 73rd position...

Method II
Programming! We played this game just to come up with answers... The following are the 3 solutions each one of us came up with in a different language...

Solution in Python (Shortest program written by co-worker Amod Pandey, but took the most time to finish :P)


Solution in Ruby, which I wrote - I finished second, I have some trace statement for help :


And this is what I wrote subsequently...
And my final submission in Ruby, based on the PDF solution below...
And Niyaz has written in Javascript... He finished coding the fastest. Here it is.



Subsequent vastly improved solutions by Niyaz - One line solutions!!
This one is in Javascript
And one more in Python

Finally, Shashank's Java code.


And then I solved this using Linked Lists
Method III
Apparently this problem is called Josephus problem. My friends Goutham Kamath and Prashanth Harshangi knew the problem along with a really fancy solution - they have provided the link to a PDF file here


9 comments:

Nikhil Anand said...

answer is u should be 72th person to the right of the person who gets the axe.

i.e: if 1 gets the axe u should be 73 to save ur ass...

PS:i solved it using AP first then
again using excel no coding required just select n drag :P

Nikhil Anand said...

answer is u should be 72th person to the right of the person who gets the axe.

i.e: if 1 gets the axe u should be 73 to save ur ass...

PS:i solved it using AP first then
again using excel no coding required just select n drag :P

Nik said...

73rd is right. How did you do it with Excel?

khanijkumar said...

Nikhil Anand - Is it the way, you solved the prob using excel.

https://lh6.googleusercontent.com/-elj97fBiE9M/T05Wxfu-G3I/AAAAAAAAA_o/N67Ud98VZFo/w402/2012-02-29

khanijkumar said...

Nikhil Anand - is this the way you solved the prob using excel

https://lh6.googleusercontent.com/-elj97fBiE9M/T05Wxfu-G3I/AAAAAAAAA_o/N67Ud98VZFo/w402/2012-02-29

Nikhil Anand said...

khanij is right..:)

Nik said...

By Excel, I am guessing you write the first two or three elements and drag in order to generate a sequence. Then keep repeating, right?

khanijkumar said...

Nikhil Baliga - thats true.

Debugged said...

I am an old timer who knows only C
Solution using traditional C :

#include
#include
int main()
{
int num,*array,flag = 0, i=0, ptr = 0,pos;
printf("\nEnter number of people\n");
scanf("%d", &num);
array = (int*)malloc(sizeof(int) * num);
memset(array, 0xffff, num * sizeof(int));
i = num;
while (num > 1) {
if(array[ptr]) {
flag = !flag;
if (!flag) {
array[ptr] = 0;
num --;
}else
pos = ptr + 1;
}
ptr = (ptr+1)%i;
}
printf("Answer : %d", pos);
return 0;
}

It would be interesting to see efficiency of the different solutions (time to completion, space taken etc) along with the length of the solution.