You are the village chief. Your village has 1000 residents including yourself. One day the ferocious EVEN DEMON attacks your village and snarls,
"All you 1000 villagers stand in a large CIRCLE, numbering yourself from 1 to 1000. Starting from number 1, I will eat every second villager (which means he will eat villager number 2, then villager number 4 and so on) and keep going around and around the circle eating up every second villager and keep doing this till there is only 1 villager left. That last villager I will spare and he is free to escape."
Since you are the village chief, you have the right to choose where you wish to stand.
In the original circle of 1000 villagers which number will you choose to stand at to be the last villager standing and escape the clutches of the EVEN DEMON?
This problem is not an original. It is adapted from a famous problem recorded by a Jewish historian.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
So nice!
your question says that he will start at number 1 and eat every second person. dose this mean he will look at person 1 and eat the man next to him or eat person 1 first
Can you generalize it for n people?
Note that this is actually the Josephus Problem
This can easily be solved by this algorithm without a proof:
1.) Turn the number n into binary (e.g. 1 0 0 0 = 1 1 1 1 1 0 1 0 0 0 2 )
2.) Transfer the leading digit to the rightmost of your new number (e.g. 1 1 1 1 1 0 1 0 0 0 2 → 1 1 1 1 0 1 0 0 0 1 2 )
3.) Turn your number formed in step two back to decimal (e.g. 1 1 1 1 0 1 0 0 0 2 = 9 7 7
Viola!!!
Log in to reply
That’s equivalent to the answer above: When satyen took out the highest power of 2, that’s equivalent to taking out the 1 in the left in binary representation, Then when he multiplied by 2 and added 1, that’s multiplying by 10 and add one in binary, ie add 1 in the right. So that looks like a rotation where you take out the one from the left and add it to the right :)
Very nice explanation.
I used the same process.............!!!!!
Nice explanation, did same process
I solved it the same way...
Nice !!!
good question
thanks . that explanation helps me a lot .
Great solutiuon and great way of explaining it too
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Nice. Using code. Did the same.
I admire people who can see the answer in the math way, I found it by a program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Generalization of this is Tn=1-N+2K..Where n=Uneaten No. or Safe number.... N=2^m..m=f(g(log k/log2)...where g(x)=antilog of x..f(x)=ceiling function of x..K=No. of people standing in the circle..
So in our case k=1000 so N=1024...Tn=1-1024+2000=977..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
It takes about 0.001 second to run
If 2^k ≤ n < 2^k+1 then f(n)=2(n−2^k)+1. Therefore, 2^9 ≤ 1000 < 2^10, implies f(1000) = 2(1000 - 2^9) + 1 i.e , f(1000) = 2*488 + 1 = 977.
http://rosettacode.org/wiki/Josephus_problem#Mathematica
It is so powerful and elegant that it hurts.
Problem Loading...
Note Loading...
Set Loading...
When the number of participants is a power of two :
Regardless of the number of participants n, person 1 survives the first pass. Since n is even, as every positive power of two is, person 1 survives the second pass as well. In the first pass, the process goes like this: person 1 is skipped, person 2 is eliminated, person 3 is skipped, person 4 is eliminated, … , person n-1 is skipped, person n is eliminated. The second pass starts by skipping person 1.
As long as the number of participants per pass is even, as it will be for a power of two starting point, the same pattern is followed: person 1 is skipped each time. Therefore, for any power of two, person 1 always wins.
When the number of participants is not a power of two, we know this much: person 1 can’t be the winner. This is because at least one pass will have an odd number of participants. Once the first odd participant pass is complete, person 1 will be eliminated at the start of the next pass.
Powers of two come into play here, but you have to change your perspective to see them. They don’t occur on pass boundaries — they span them. At some point, during pass 1, the number of participants remaining becomes a power of two. For example if there are 13 villagers, that occurs when 5 of the 13 people are eliminated, leaving an 8 person problem: 11, 12, 13, 1, 3, 5, 7, 9. This means person 11, the first person in the new power of two circle, wins.
Write n as n = 2^m + k, where 2^m is the largest power of two less than or equal to n. k people need to be eliminated to reduce the problem to a power of two, which means 2k people must be passed over. The next person in the circle, person 2k + 1, will be the winner. In other words, the winner w is w = 2k + 1.
n=1000. k=1000- 512 (largest power of 2 below 1000) = 488. w = 2k+1 = 977.