## Nov 8, 2009

### The 1000 doors problem

Every morning, Mike the security guard at CP high school opens all 1000 doors in the building. Let's assume the doors are numbered 1-1000. The next security guard closes all even numbered doors. The third security guard touches all doors that are multiples of 3. If a door is open, he'll close it and vice versa. The fourth guard changes the position of every fourth door (if it's open he'll close it etc.,) and the fifth guard changes the position of every fifth door and so on, until the 1000th guard changes only door 1000.

HOW MANY DOORS ARE LEFT OPEN IN THE END?

1. i think it will require pen and paper.. :P
so... alasyam param dharam!!

2. This comment has been removed by a blog administrator.

3. 1000-500+333-250+200...........
int values of the nummbers obtained when 1000 is divided by all the numbers from 1 to 1000 with addition and subtraction alternatively... din have time to do such a big calculation.. ny one wth answer?

4. This comment has been removed by the author.

5. Only the doors numbers that are perfect squares will remain open. Doors 1,4,9,16...... will always remain open.
Try to figure out the reason (think number of divisors of a number).

1. This comment has been removed by the author.

6. Yes , door numbers that are perfect squares will remain opened. For a number 'X',including 1 and X itself ,at each divisor place 'a'the door gets closed/opened if it is opened/closed in its previous multiplicative factor 'b' such that a*b=X.Since the number of divisors for a perfect square is odd (open,close, open ) and for others the number of divisors is even (open close)hence doors with perfect squares will remain opened.

7. 30 open and 970 closed... cos there are 30 perfect squares (each perfect square has even factors excluding 1).. so these doors will be opened-closed an even number of times leading to no change.. all others will be changed since will be opened-closed odd number of times

8. 31x31 = 961

Therefore the answer is 31 and 969

9. 31 perfect squares are there btwn 1 and 1000
31 wll be open
all others wll be closd

10. This comment has been removed by the author.

11. In the end, only the doors of the perfect squares remain open... 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841 900 961
insane!!!!

Here is some R code I just wrote:
b <- rep(TRUE, 1000)

for (p in 2:1000){
for(i in 1:length(b)){ # 'i' is for index
if (i%%p == 0) {
b[i] <- !b[i]} #then open door
}
}
b

which(b== TRUE, arr.ind=TRUE)

12. Very nice and clean code! I was expecting many more lines!

13. nicely explained this puzzle video solution