Time to Read: ~1 minWhen 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!

### Related Posts

There are 22 other posts like this one. Here are some other recent ones:

February 12, 2023

If you need to automatically create Google Calendar events from data in a Google Sheet, this pared-down example should work well for you.

October 10, 2021

I created this Shakespeare Insult Generator: it converts three arrays of adjectives and nouns into a delightful random insult that would have thrilled the Bard himself.

November 26, 2020

I decided to create a page that talked about what I was training for, and provided a way for me to provide bits of commentary as the training plan unfolded. The result is a nice mix of Eleventy magic, a CSS-only toggle switch, and the checkbox hack.