There is a room with 1000 light switches, numbered 1, 2, 3,
4, ... 1000, all turned off.
Outside the room there are 1000 men, numbered man1, man2,
...man 1000
In order, each man walks into the room and changes the
position of each switch that is a multiple of his number.
That is: man1 flips every switch
man2 flips switches 2, 4, 6, 8 ....1000
man3 flips switches 3, 6, 9, ..... 999
.....
Man 1000 flips switch 1000
After all 1000 men are done, how many switches are on?

Answers were Sorted based on User's Feedback



There is a room with 1000 light switches, numbered 1, 2, 3, 4, ... 1000, all turned off. Outside ..

Answer / rams

The answer is 31. All the perfect squares are on. ie 1,4,9,16,25,36....961(31*31).

Here is the logic.

The first person switches on every thing.

The multiple factors come in pairs, so the changes done by multiple factors has no effect. So all the Switches are on.

The switches are turned off by the person with same number.

The square root switches on the lights for the perfect squares.

Is This Answer Correct ?    9 Yes 0 No

There is a room with 1000 light switches, numbered 1, 2, 3, 4, ... 1000, all turned off. Outside ..

Answer / orenise

The above code does not work, Please ignore it. I will Post
the answer, when i got the correct Code.

Is This Answer Correct ?    2 Yes 0 No

There is a room with 1000 light switches, numbered 1, 2, 3, 4, ... 1000, all turned off. Outside ..

Answer / orenise

The answer is 31. here is the code...

private static void Q1() {
int firstMan = 1;
int lastMan = 1000;
int numOfSwitches = 1000;
boolean[] switches = new boolean[numOfSwitches + 1];
// Print the switches initial state.
System.out.println("switches");
int col = 0;
for (int j = 0; j < switches.length; j++) {
System.out.print("[" + switches[j] + "], ");
if (col >= 10) {
System.out.println();
col = 0;
}
col++;
}

for (int currentMan = firstMan; currentMan <= lastMan;
currentMan++) {
for (int switchNum = currentMan; switchNum <=
lastMan; switchNum++) {
int divids = switchNum % currentMan;
if (divids == 0) {
switches[switchNum] = !switches[switchNum];
}
}
}

// Print the switches final state.
System.out.println("switches");
int col2 = 0, sum = 0;
for (int j = 0; j < switches.length; j++) {
System.out.print("[" + switches[j] + "], ");
if (switches[j]) {
sum++;
}
if (col2 >= 10) {
System.out.println();
col2 = 0;
}
col2++;
}
System.out.println(" sum = " + sum);
}

Is This Answer Correct ?    3 Yes 2 No

Post New Answer

More Programming Languages AllOther Interview Questions

how we can call xml file in java file using Android platform?

2 Answers   TCS,


what you mean by mantis in IT interview? why we use mantis? advantage of mantis?

1 Answers  


Define Form modules, Class modules and Standard modules

0 Answers   Global Hunt,


Hi..Am done with my Masters recently..Am planning to learn TIBCO.. could anyone suggest me about how the job market will be and Is there any course necessary to learn prior to Tibco..i mean any prerequisite. I dnt have any knowledge on PL/SQL thats it... Plz suggest me in a best way...

0 Answers  


THERE ARE 4 SOURCE FILES WHICH CONTAINS SAME METADATA CREATE A TARGET THAT SHOULD DISPLAY THE FILE NAME ALONG WITH THE RECORD PLEASE SEND THE ANSWERFOR THIS QUESTION WITH MAPPING

0 Answers   Wipro,






what is difference between java and c#

2 Answers   Cyient,


For C sharp,At the time of software is implemented at client site, is it required that client machine have C sharp(Microsoft Visual Basic) setup?

1 Answers   Honeywell,


Can anyone send me NIC question papers alongwith answers on nidhi1485@yahoo.co.in? Urgently needed.. Thanks in advance

0 Answers  


when will triggars the at new event in abap and web dybn pro?

0 Answers   TCS,


what is the current salary package in India for a lamp programmer

0 Answers   HCL,


where is available in this mantis toturials?

0 Answers  


what is client server & server? What r types of Joins? explsin it! which types of indexex in sql server? what is triggers?

0 Answers  


Categories