Lab 0: 20,000 Lights

JAVA 8 API

Instructions

A room has 20,000 light bulbs, each with its own pull-string. Twenty-thousand people walk into the room, one at a time. The first person pulls each string, turning all the lights on. The second person pulls every second light bulb's string (the 2nd, 4th, etc.) The third person pulls every third string, the fourth every fourth, and so on. Eventually the twenty-thousandth person pulls the string on the twenty-thousandth light.

A) How many light bulbs are on?
B) Which light bulbs are they?

Below is some 'starter code'. Think about either keeping count and then using the mod function (%2) to determine if it's off or on *OR* look up how to use an array of boolean and toggle the values of each element in the array between true and false.

public class HelloWorld{

     public static void main(String []args){
        int[] bulbs = new int[10];
        
        for(int i=1; i<= bulbs.length; i++)
        {
            for(int j = i; j <= bulbs.length; j+=i)
            {
                System.out.println(i + " - "+j);
                // either use the idea below or change this
                bulbs[j-1]++; 
                // System.out.println(bulbs[j-1]);
            } // end j loop
        }// end i loop
        
        for(int i = 0; i < bulbs.length; i++)
        {
            System.out.println(bulbs[i]);
        }
        
        
        
     } // end the function 
}