Main image for The Locker Prank Time to Read: ~1 min

When I read about this Popular Mechanics Riddle of the Week, I knew I could solve the problem by writing code. After all, I was a math minor in college, but my computer science degree wins out when I'm confronted with something like this. Brute force! Here's the riddle:

There are 100 lockers that line the main hallway of Chelm High School. Every night, the school principal makes sure all the lockers are closed so that there will be an orderly start to the next day. One day, 100 mischievous students decide that they will play a prank. The students all meet before school starts and line up. The first student then walks down the hallway, and opens every locker. The next student follows by closing every other locker (starting at the second locker). Student 3 then goes to every third locker (starting with the third) and opens it if it’s closed, and closes it if it’s open. Student 4 follows by opening every fourth locker if it’s closed and closing it if it’s open. This goes on and on until Student 100 finally goes to the hundredth locker. When the principal arrives later in the morning, which lockers does she find open?

Popular Mechanics Riddle of the Week

The mathematical solution published by Popular Mechanics is far more elegant than my brute force approach. That said, I was able to come up with a way to visualize the changes as each student goes through the rows of lockers. Also, you can change the number of students and lockers to get quite the animation going!

Subscribe to Wanderfull

Did you enjoy this? Did it help you? Make you laugh? Dare I say, all of the above? If you like my work — my writing, distributed work tips, or drawing, you can get more every week. Subscribe below for my weekly Substack: Wanderfull!