
Puzzle:  100 Prisoners and a Light Bulb - amichail
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.<p>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>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.
======
slashcom
The prisoners elect a counter. His count is initially 0, but increases to 1
the first time he walks into the room. Whenever he visits the lightbulb he
will do the following:

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.

~~~
slashcom
I just realized I used "one" instead of "on". :-P

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?

~~~
DanielBMarkham
Somebody should do an Ask PG for this. The line breaks in the comments drive
me nuts. Sometimes they work, sometimes they don't. Sometimes you can start
with them working and then they're not working again.

------
drm237
I think the chance of anyone being lucid enough to carry out the solution
after 9.5 years of solitary confinement is small enough that the solution
doesn't really matter. Someone will say they've all been in there after the
first year just to put themselves out of their misery!

~~~
barns
dude, it's a puzzle. Don't think of it as a real life situation.

------
falsestprophet
In stead of waiting a decade or more, I would make a shank.

~~~
aswanson
Upmodded for proof of first optimal brute-force solution.

------
oditogre
I now have Tool stuck in my head. Yes, it's related to the puzzle. Would
probably be a better puzzle if you said they were aiming to get out ASAP
(although you could fairly assume it...)

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.

~~~
barns
The puzzle states that he has to be 100% sure that everyone had been to the
room. "fairly confidently" is not good enough here. And, it won't matter if
you increase the number of visits in a row to 5, 20, or more, there is still
no guarantee.

~~~
mxh
Balls to that. I'm armed with a bad attitude and worse math. By my reckoning:

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:

    
    
       N    P(safe)  Cost[2]
     ----- -------- ----------
      100: 0.000000 (18250.00)
      200: 0.000001 (18249.99)
      300: 0.006887 (18124.31)
      400: 0.166419 (15212.86)
      500: 0.520678 ( 8747.63)
      600: 0.787901 ( 3870.80)
      700: 0.916504 ( 1523.81)
      800: 0.968598 (  573.08)
      900: 0.988391 (  211.87)
     1000: 0.995735 (   77.83)
     1100: 0.998437 (   28.53)
     1200: 0.999428 (   10.45)
     1300: 0.999790 (    3.82)
     1400: 0.999923 (    1.40)
     1500: 0.999972 (    0.51)
     1600: 0.999990 (    0.19)
     1700: 0.999996 (    0.07)
     1800: 0.999999 (    0.03)
     1900: 0.999999 (    0.01)
     2000: 1.000000 (    0.00)
     2100: 1.000000 (    0.00)
     2200: 1.000000 (    0.00)
     2300: 1.000000 (    0.00)
     2400: 1.000000 (    0.00)
     2500: 1.000000 (    0.00)
     2600: 1.000000 (    0.00)
     2700: 1.000000 (    0.00)
     2800: 1.000000 (    0.00)
     2900: 1.000000 (    0.00)
     3000: 1.000000 (    0.00)
     3100: 1.000000 (    0.00)
     3200: 1.000000 (    0.00)
     3300: 1.000000 (    0.00)
     3400: 1.000000 (    0.00)
     3500: 1.000000 (    0.00)
    

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.

~~~
run4yourlives
I'm with you. Hell, even 1000 days is a long time, I'd give 'er after 800. If
I'm wrong, such is life.

If I'm right, I saved myself 22 years in solitary confinement.

~~~
lotu
You will also save your self 22 years in solitary confinement if your wrong.
So win win.

------
pixcavator
I love this: "...from his or her own cell", "...if he or she wishes"!

------
corentin
It can take less than 20 days if they have Michael Scofield with them.

(I'm working on a solution; will post it here soon)

------
Darmani
Assuming this is an incandescent lightbulb with mediocre or poor heat
conductivity and that the lapse between "end of the day" and "start of the
day" is sufficiently short, extra information can be communicated using the
temperature of the bulb (unless it burns out). For example:

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.

~~~
cstejerean
You can also try to twist the bulb in the socket and observe the orientation
of the text written on it relative to other markers in the room.

------
shayan
I would make every prisoner into a counter!

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).

~~~
curi
so, the light gets turned off 100 times in total. there are 100 different
people counting. none ever reach 100 in their count.

~~~
shayan
I was trying to eliminate the one counter to make it faster, but I see your
point ...you just saved a bunch of lives, and got yourself an up vote!...I
looked at the other possible solutions and they all seem to take a while, but
I am planning a escape, are you in?

------
shayan
dear amichail,

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...

~~~
dood
A solution that takes around 12 years
[<http://www.algonet.se/~ug/projects/lightbulb/double.html>]

I couldn't resist peeking ;)

~~~
shayan
hopefully we'll get the one with 3500 days too (at this point just to know we
weren't fooled and wasting time)

------
walesmd
The group selects one person to work as a counter. All other prisoners will
only turn on the light if it is off and it is their first time in the room (if
it's on they do nothing).

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.

~~~
brlewis
Aren't the chances of it not taking forever infinitesimal?

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?

------
jakewolf
I was going to bite my tongue before commenting on this, but that is the
answer.

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.

------
bcater
I've written a simulator that seems to fit with intuition, but you guys might
want to check out my code
(<http://scripts.mit.edu/~bcater/prisoners_code.php>). It gives me just under
10,000 operations, on average.

------
oconnor0
Are the prisoners prevented from turning the bulb or using other pieces of the
room as counters?

In other words, is the intent to use only whether the bulb is on or off to
solve this?

~~~
amichail
The intent is to use the light bulb in the obvious way.

------
jinking
thats eazy if they all go in they should be able to say ...that bulb thing
wount help really whats if it blows how many thing will a light buld light b4
it blows

