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

What is the purpose of <Trigger> element of <updatePanel> in Ajax

0 Answers  


shall we execute our java programmes in jre

0 Answers  


How to connect to ms word wit VB ojective is to prepare s/w to generate question paper , selects questions randomly from the ms access , database

0 Answers   Wipro,


HOW TO BREAK THE FIREWALL?

0 Answers   ME,


Explain the difference between an expert and a novice user. How would your strategy for designing user interfaces for an expert user differ from that for designing user interfaces for a novice user.

0 Answers  






What is the difference between procedure -oriented language and object oriented language?

22 Answers   IBM, Infosys, TCS,


Is there any standard procedure to test the application as a whole? Or How can I test complete application right from the requirement gathering?

0 Answers  


write a program on c(or)c++(or)java language i.e if i have 5 numbers like (10,24,3,9,15) i want to display highest number from these numbers

2 Answers  


I am taking the bmc control m/enterprise manager 7.0 scheduling test and just wanted to see what kind of questions they would ask or if anyone has taken the test and how long it is for how many questions?

0 Answers  


what is web configuration file

0 Answers   HCL,


what is dot net framework

0 Answers   NIC,


How to rename A1-A30 datasets into B1-B30 using macros?

1 Answers   Icon,


Categories