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.
- The principal asks the first student to walk down the main corridor of the school to close all the lockers.
- The principal then asks the second student to walk down the main corridor and open every other locker.
- 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.
- The fourth student will then repeat the same process for every fourth locker.
- 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.
lockers = [0] * 1000
Step 2:
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:
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!
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area