TITLE: The locker problem - why squares?
QUESTION [25 upvotes]: There are 1000 lockers in a high school with 1000 students.  The 
  problem begins with the first student opening all 1000 lockers; next 
  the second student closes lockers 2,4,6,8,10 and so on to locker 1000;
  the third student changes the state (opens lockers closed, closes 
  lockers open) on lockers 3,6,9,12,15 and so on; the fourth student 
  changes the state of lockers 4,8,12,16 and so on. This goes on until 
  every student has had a turn.
How many lockers will be open at the end?

I worked out this problem by doing it for 10 lockers. The pattern was obvious (1,4,9 remained open).It's easy to then say that 31 lockers remain open at the end. I don't quite understand why only square numbers remain open at the end. Is there some deeper reason?

REPLY [7 votes]: The idea here is that it is easy to identify pairs of students who will open and close a locker (well, as easy as any interesting math problem ever is!).  If any given student will open or close a locker, it means their number is a divisor of the locker's number.
Consider locker X.  Let's assume there is a student Y who toggles the locker.  This means we know X/Y is an integer.  This also means that student X/Y will also toggle the locker.
Take locker 12, with factors 1, 2, 3, 4, 6, and 12.  If I rearrange them into pairs, (1, 12) (2, 6) (3, 4) we can see that for every student who opens a locker, there will be one that closes the locker.
Now, Let's look at a square.  Let's take 16, with factors 1, 2, 4, 8, 16.  If I rearrange them into pairs I get (1, 16) (2, 8) (4, 4)... but wait... I just had to use 4 twice.  I had to do that because it was a square.  However, student 4 is only going to toggle locker 16 once.  Thus, the locker will be left open at the end of the day!