The School Lockers Puzzle

On the first day of school, the principal of Locker High school decides to conduct an experiment. The school has exactly 1,000 students and 1,000 lockers all lined up alongside the main corridor of the school.

  1. The principal asks the first student to walk down the main corridor of the school to close all the lockers.
  2. The principal then asks the second student to walk down the main corridor and open every other locker.
  3. The principal then asks the third student to walk down the main corridor and either open every third locker if it is closed, or close it if it is open.
  4. The fourth student will then repeat the same process for every fourth locker.
  5. And so on, till the last of the 1,000 students repeats this process for every 1,000th locker, so in fact, just opening the 1,000th locker if it is closed, or closing it if it is already open.

At the end of this experiment the principal decides to count the number of lockers which are closed.

It is possible to solve this puzzle using your mathematical skills, however in this challenge, your task is to implement a computer program (using Python) to simulate this experiment and at the end, count and output the total number of lockers which are closed.

We have started the code and completed three sections of the code as follows:

Step 1:
We start by initialising an array/list of 1,000 values representing the 1,000 lockers:

  • 0 will represent an open locker,
  • 1 will represent a closed locker.

“To start with all the lockers a open!”

lockers = [0] * 1000

Step 2:

“The first student walks down the corridor to close all the lockers!”

for i in range(0,1000):
  lockers[i] = 1

A similar approach will need to be implemented to reproduce the actions of the remaining 999 students! This will be your task!

Step 3:

“At the end, the principal of the school counts how many lockers are closed!”

total = 0 
for i in range(0,1000):
   total = total + lockers[i]
print("Total number of lockers closed: " + str(total))

Python Code

You will need to complete the following code to solve this challenge:

The Principal’s padlock

The solution to the above challenge is the combination of the principal’s padlock. Use the output of your code to see if you can unlock this padlock!



The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 30

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!