Puzzle: 100 Prisoners and a Light Bulb
100 prisoners are imprisoned in solitary cells. Each cell is windowless and soundproof. There's a central living room with one light bulb; the bulb is initially off. No prisoner can see the light bulb from his or her own cell. Each day, the warden picks a prisoner equally at random, and that prisoner visits the central living room; at the end of the day the prisoner is returned to his cell. While in the living room, the prisoner can toggle the bulb if he or she wishes. Also, the prisoner has the option of asserting the claim that all 100 prisoners have been to the living room. If this assertion is false (that is, some prisoners still haven't been to the living room), all 100 prisoners will be shot for their stupidity. However, if it is indeed true, all prisoners are set free and inducted into MENSA, since the world can always use more smart people. Thus, the assertion should only be made if the prisoner is 100% certain of its validity.
Before this whole procedure begins, the prisoners are allowed to get together in the courtyard to discuss a plan. What is the optimal plan they can agree on, so that eventually, someone will make a correct assertion?
P.S. Probabilistic analysis for this problem can be difficult, so try simulations to test out your solutions to see how long they take on average.
(
43 comments
| 26 points
| on HN
)
1) If the lightbulb is off, leave the room.
2) If the lightbulb is one, turn it off and increment his count.
Every other prisoner follows the following rules:
1) If the lightbulb is on, do not touch it.
2) If the lightbulb is off and you have not flipped it on before, flip it on.
3) If the lightbulb is off but you have previously flipped it on, then do not touch it.
When the counter reaches 100, he may tell the guard that everyone has visited.
Some statistics from my little python simulation:
n: 1000 (I ran my simulation 1000 times)
Mean: 10425.721 days
Stddev: 1005.34467779 days
Min: 7506 days
Q1: 9711 days
Median: 10391 days
Q3: 11074 days
Max: 14090 days
Note: can someone tell me how to do single linebreaks in the comments?
If I knew how to paste code in here, I would show you my simple python script to test it.
Edit: 1000 runs: Average: 10109 Lowest: 7389 Highest 13461
It would probably be helpful if that information was readily available via a link near the "add comment" and "reply" buttons
I still have a bug in my algorithm and I'm not even sure it will really work.
if bulb on: do not touch
if bulb off:
if you haven't been visited before, consider yourself counted, and do nothing.
if you have been in here before, turn it on. you are now the counter. look at the day number (1-based). if it's day 36, then 35 people (including yourself) have visited the room.
day 100:
if bulb off, and you haven't been there before, declare victory (super lucky!)
if off, but you have been there before, you're the counter and the count is 99.
if bulb on: turn it off.
after day 100:
use the original solution with a counter.
speedup:
you get about 12.2 people counted in the first 100 days. normally it's somewhere around one person counted per 100 days. so total time is around 100 + 87.8*100. a bit better. not near 3500 though.
i think the improvement was a little less good than i guessed because i jumpstart getting the first dozen or so people counted, and they are a little easier to count than the last people.
http://www.ocf.berkeley.edu/~wwu/papers/100prisonersLightBul...
It includes my answer :)
My solution, before I look at others' replies: One person is designated to turn on the light. The designated person should be the person with the most well-ingrained sleeping pattern in the group. If anybody comes in and it's their first time in the room and the light is on, they turn it off. If the designated guy finds the light to be on for 3 visits in a row and he's sure it's been 200 days since the first time, I think he can fairly confidently declare that all have visited the room.
On any given day, a given prisoner has a .99 probability of NOT visiting the room.
Over a period of N days, a given prisoner has a pow(.99, N) probability of having _NEVER_ visited the room. Therefore, the probability a given prisoner HAS visited the room at least once over a period of N days is (1-pow(.99, N)).
Therefore, the probability that X prisoners have each visited the room at least once over a period of N days is pow((1-pow(.99, N)), X)[1]. If I'm in the room, the question I need to answer is: How likely is it that each of the 99 other prisoners have visited the room at least once in the preceeding N days?
Let's visit Mr. Python:
After 1000 days, I'm guessing everyone's visited. Screw you guys, I'm going home.[3][1]I know this can't be exactly right, because prisoner visits aren't independent events, but I figure it can't be that wrong, and I want to go home.
[2]Cost is calculated in expected days of life forgone, multiplying (1-P(safe)) by 50 years by 365. Arbitrary, but it gives some idea of how much expected life you gain by waiting longer to roll the dice.
[3]I know this is ducking the intention of the question, but I think the point that death isn't that bad a risk if the probability is low is a legitimate one.
If I'm right, I saved myself 22 years in solitary confinement.
(I'm working on a solution; will post it here soon)
The prisoners select a counter, whose count begins at 0 and increases to 1 when he himself enters the room.
When the counter enters the room:
1)If the bulb is off, leave
2)If the bulb is on and relatively cool, turn it off, increment count by 1, and leave
3)If the bulb is on and relatively hot, turn it off, increment count by 2, and leave
When others enter the room:
1)If the bulb is off and the prisoner has yet to signify his presence, turn it on at the end of the day. This counts as signifying one's presence.
2)If the bulb is on and the prisoner has yet to signify his presence, leave it on all day. If the bulb was relatively cool, then this will count as signifying one's presence.
3)If the bulb is on and relatively cool and the prisoner has already signified his presence, turn it off at the start of the day and back on at the end of the day.
4)Else, do not touch the bulb.
When the count reaches 100, he asserts that all have visited.
Everytime someone visits the room has to take the following two steps:
(1)actions:
- if it's their first time make sure the light is off (i.e. if off don't touch, if it was on, turn it off)
- if you have been there before make sure the light is on (i.e. if it was off turn it on, and if it was already on don't touch it)
(2)counting:
- if the light was off when you walked in then add one more to your count
- if it was on then don't count anything
this way everyone counts, we now have 100 counters. And whoever reaches hundred first (counting themselves in) should report it.
Note: the guy that went in on day one, should not count for the day before, but everyone else will count their day before if appropriate (based on the rules above).
I think you should give the best answer to this question NOW, the one you say that takes 3500 days... I also hope that the first answer given by slashcom is not in fact the best answer (as you said it yourself) and you didn't just waste everyone's time for nothing ...even if its 5 min of everyone's time, it adds up to a lot of time and its not ethical...
the reason I am posting this is because this post is almost out of the news and it already seems to be forgotten ... I would be pretty pissed if I had spent more time like other users such as curi and everyone else that tried to solve your problem...
I couldn't resist peeking ;)
The counter will always turn the light off (and count the number of times he does so).
When the counter reaches 99, he knows that all 100 prisoners have visited the room.
It could take forever - especially since the jailer can not be truly random and could potentially pick a single prisoner (or even the counter) every single day.
All it takes is two prisoners to go to the room in between counter visits, and the counter will only ever reach 98.
See the subtle difference between slashcom's answer and yours?
When each prisoner goes into the room they will cut themselves and leave their mark in blood in the room. Or they could scratch the wall etc.
Before you say this won't work because the warden could mess with the marks I don't have 30 something years to wait around for randomness to finish.
In other words, is the intent to use only whether the bulb is on or off to solve this?