/* A school has 100 students and 100 lockers. Student 1 opens all 100 lockers. Student 2, beginning with the * second locker, closes every other locker. Student 3, starting with locker 3, changes every 3rd locker (opens * any closed locker, closes any open locker). Student 4, starting with locker 4, changes every 4th locker, etc. * When done, which lockers are open and which are closed? * * Here, we use lockers 0..99 and students 1..100 but student i is going to start at locker i-1. We use true for * open and false for closed */ public class Prog7_23 { public static void main(String[] args) { boolean[] lockers = new boolean[100]; for(int i=0;i<100;i++) lockers[i] = true; // student 1 opens all lockers to start for(int student=2;student<=100;student++) // students 2-100 take turns for(int locker=student-1;locker<100;locker=locker+student) // for each locker, notice we start at student-1 since lockers are actually 0-99 if(lockers[locker]) lockers[locker]=false; // change lockers[locker] (open if closed, close if opened) else lockers[locker] = true; for(int i=0;i<100;i++) // output the results System.out.println((i+1) + " " + lockers[i]); } }