The locker problem - why squares?

Solution 1:

Squares are the only integers which have an odd number of divisors.

All other (non-square) integers have an even number of divisors.


A deeper insight:

Given an integer $n$, for every integer $d$ which divides $n$, the integer $n/d$ also divides $n$.

If $n$ is non-square, then for every integer $d$ which divides $n$, the integer $n/d \neq d$.

So we can split the divisors of $n$ into pairs, hence $n$ has an even number of divisors.

If $n$ is square, then for every integer $d\neq\sqrt{n}$ which divides $n$, the integer $n/d \neq d$.

So we can split the divisors of $n$ except $\sqrt{n}$ into pairs, hence $n$ has an odd number of divisors.

Solution 2:

If a locker number has an even number of factors, it will be alternatively opened and closed and even number of times, ending in the same configuration it started.

Square numbers have a odd number of factors.