0

Stumpedā€¦ help

100 students, 100 lockers. s1 opens all locker, s2 changes second locker & then every second locker. s3 changes third locker & then every third locker & so on and so forth until all 100 students get the chance. output should only be n lockers that were left open after everything https://sololearn.com/compiler-playground/cAaGrIo4C1xo/?ref=app

17th Oct 2024, 6:34 PM
AcnkKRuv5A
4 Answers
+ 6
AcnkKRuv5A inside the range() function you can also define a step. range(start,end,step) e.g. range(1,10,2) -> 1,3,5,7,9 In your inner loop, you can set a step for each student. student 1 -> step = 1 student 2 -> step = 2 and so on... Hope this helps.
17th Oct 2024, 7:20 PM
Denise RoƟberg
Denise RoƟberg - avatar
+ 3
show your code
17th Oct 2024, 6:39 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
+ 2
Hi AcnkKRuv5A , To solve the 100 lockers problem, you don't need to simulate all the students opening and closing lockers. Instead, you can use a mathematical approach based on divisors. Each locker changes its state (open/closed) as many times as it has divisors. For example, locker 12 has divisors 1, 2, 3, 4, 6, and 12 (6 divisors). A locker with an even number of divisors will end up closed, and a locker with an odd number of divisors will remain open. Here's the key insight: only perfect squares (like 1, 4, 9, 16, etc.) have an odd number of divisors because one divisor pairs with itself (like 3 * 3 = 9). All other numbers have an even number of divisors. So, to find which lockers remain open, just identify the perfect squares up to the highest locker number. These are the lockers that will be open in the end. Summary: To find all open lockers when the number of lockers is n, take the squares of all numbers from 1 up to int(n^.5). Math makes programming easier. šŸ™‚
18th Oct 2024, 11:16 AM
Per Bratthammar
Per Bratthammar - avatar
+ 2
thank u, got it !
21st Oct 2024, 11:49 AM
AcnkKRuv5A