firsttube.com
dev.firsttube.com: Lockers Riddle

The riddle reads: A school has 100 students and 100 lockers, all in a row. They all start out closed. The first student walks down the line and opens each one. The second student closes the even numbered lockers. The third student approaches every third locker and changes its state. If it was open he closes it; if it was closed he opens it. The fourth student does the same to every fourth locker, and so on through 100 students. How many lockers are open, and which ones?

The solutions is below, both in text and illustrated. The code to solve the riddle is here: lockers.phps.

Open locker numbers are 1, 4, 9, 16, 25, 36, 49, 64, 81, 100


    ---------
01  |open    _
    ---------
02  |closed  |
    ---------
03  |closed  |
    ---------
04  |open    _
    ---------
05  |closed  |
    ---------
06  |closed  |
    ---------
07  |closed  |
    ---------
08  |closed  |
    ---------
09  |open    _
    ---------
10  |closed  |
    ---------
11  |closed  |
    ---------
12  |closed  |
    ---------
13  |closed  |
    ---------
14  |closed  |
    ---------
15  |closed  |
    ---------
16  |open    _
    ---------
17  |closed  |
    ---------
18  |closed  |
    ---------
19  |closed  |
    ---------
20  |closed  |
    ---------
21  |closed  |
    ---------
22  |closed  |
    ---------
23  |closed  |
    ---------
24  |closed  |
    ---------
25  |open    _
    ---------
26  |closed  |
    ---------
27  |closed  |
    ---------
28  |closed  |
    ---------
29  |closed  |
    ---------
30  |closed  |
    ---------
31  |closed  |
    ---------
32  |closed  |
    ---------
33  |closed  |
    ---------
34  |closed  |
    ---------
35  |closed  |
    ---------
36  |open    _
    ---------
37  |closed  |
    ---------
38  |closed  |
    ---------
39  |closed  |
    ---------
40  |closed  |
    ---------
41  |closed  |
    ---------
42  |closed  |
    ---------
43  |closed  |
    ---------
44  |closed  |
    ---------
45  |closed  |
    ---------
46  |closed  |
    ---------
47  |closed  |
    ---------
48  |closed  |
    ---------
49  |open    _
    ---------
50  |closed  |
    ---------
51  |closed  |
    ---------
52  |closed  |
    ---------
53  |closed  |
    ---------
54  |closed  |
    ---------
55  |closed  |
    ---------
56  |closed  |
    ---------
57  |closed  |
    ---------
58  |closed  |
    ---------
59  |closed  |
    ---------
60  |closed  |
    ---------
61  |closed  |
    ---------
62  |closed  |
    ---------
63  |closed  |
    ---------
64  |open    _
    ---------
65  |closed  |
    ---------
66  |closed  |
    ---------
67  |closed  |
    ---------
68  |closed  |
    ---------
69  |closed  |
    ---------
70  |closed  |
    ---------
71  |closed  |
    ---------
72  |closed  |
    ---------
73  |closed  |
    ---------
74  |closed  |
    ---------
75  |closed  |
    ---------
76  |closed  |
    ---------
77  |closed  |
    ---------
78  |closed  |
    ---------
79  |closed  |
    ---------
80  |closed  |
    ---------
81  |open    _
    ---------
82  |closed  |
    ---------
83  |closed  |
    ---------
84  |closed  |
    ---------
85  |closed  |
    ---------
86  |closed  |
    ---------
87  |closed  |
    ---------
88  |closed  |
    ---------
89  |closed  |
    ---------
90  |closed  |
    ---------
91  |closed  |
    ---------
92  |closed  |
    ---------
93  |closed  |
    ---------
94  |closed  |
    ---------
95  |closed  |
    ---------
96  |closed  |
    ---------
97  |closed  |
    ---------
98  |closed  |
    ---------
99  |closed  |
    ---------
100 |open    _
   -----------

This site is powered by Free Software, including Linux, Apache, MySQL, PHP, and Small Axe.
Source Code © 2002-2006, Adam Scheinberg unless otherwise noted.
Content may not be reprinted without permission
Syndicate my blog via: RSS or Atom.