Question
?Python (Lists and repetion) You have 1000 lockers and 1000 students. All lockers are initially locked. 1st student opens all lockers 2nd student closes every
?Python (Lists and repetion)
You have 1000 lockers and 1000 students. All lockers are initially locked.
1st student opens all lockers
2nd student closes every other locker
3rd student opens(if closed) or closes (if open) every 3rd locker
4th student opens(if closed) or closes (if open) every 4th locker
.
.
.
1000th student opens(if closed) or closes (if open) every 1000th locker
Write a program to determine which exact locker numbers are open, and total number that are open.
You are not allowed to use Python function count()!!
HINTS:
You have 1000 lockers and 1000 students. All lockers are initially locked.
(Hint: make an empty list lockers, then make a loop to append 1000 1's to that empty list)
1st student opens all lockers.
(Hint: make a loop to make each locker equal to a 0)
2nd student closes every other locker.
(Hint: make a loop that goes through every other locker (increment loop by 2) and make every other locker equal to 1)
3rd student opens(if closed) or closes (if open) every 3rd locker.
(Hint: make a loop that goes through every 3rd locker (increment loop by 3) and make every 3rd locker
if locker is equal to 1 , then make it equal to 0,
elif locker is equal to 0 ,then make it equal to 1 )
4th student opens(if closed) or closes (if open) every 4th locker.
(Hint: make a loop that goes through every 4th locker (increment loop by 4) and make every 4th locker:
if locker is equal to 1 , then make it equal to 0,
elif locker is equal to 0 ,then make it equal to 1 )
5th student opens(if closed) or closes (if open) every 5th locker.
(Hint: make a loop that goes through every 5th locker (increment loop by 5) and make every 5th locker:
if locker is equal to 1 , then make it equal to 0,
elif locker is equal to 0 , then make it equal to 1 )
Notice a pattern between 3rd, 4th, and 5th student?
It's is the same loop with the same IF's, except the loop increment changes.
We don't want to copy/paste this 1000 times.
So, make a another loop which changes the increments of the above loop from 3 to 1000, and nest the first loop in it.
Replace the loops for 3rd, 4th, 5th students with the above nested loops.
Now show which lockers are open, and which are closed.
Program Run: Locker Number 1 is open Locker Number 4 is open Locker Number 9 is open Locker Number 16 is open Locker Number 25 is open Locker Number 36 is open Locker Number 49 is open Locker Number 64 is open Locker Number 81 is open Locker Number 100 is open Locker Number 121 is open Locker Number 144 is open Locker Number 169 is open Locker Number 196 is open Locker Number 225 is open Locker Number 256 is open Locker Number 289 is open Locker Number 324 is open Locker Number 361 is open Locker Number 400 is open Locker Number 441 is open Locker Number 484 is open Locker Number 529 is open Locker Number 576 is open Locker Number 625 is open Locker Number 676 is open Locker Number 729 is open Locker Number 784 is open Locker Number 841 is open Locker Number 900 is open Locker Number 961 is open There are 31 lockers open
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started