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 19 other posts like this one. Here are some other recent ones:

October 25, 2020

I read about static site generators like Hugo and Eleventy and saw the light. After some quick reads on comparing the leading contenders, I rolled up my sleeves with Eleventy.

May 28, 2020

After being dedicated mountain pass holders, then Ikon pass holders, then nomads purchasing advance tickets, I thought it'd be cool to create a map-based way of discovering new ski resorts.

December 29, 2019

I started programming in React when we adopted a React-based framework at my day job earlier this year. My wife's bakery needed to have a better order form. I reasoned that I could take her Google Form and improve the experience.