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
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 |
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 |
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 |
I'm trying to solve this. But I'm not figuring the right solution. Can some one help what the answer is for the question below? You can use as many variables as you need, there are no negative numbers, all numbers are integers. You do not know the size of the integers, they could be infinitely large, so you can't count on truncating at any point. There are NO comparisons allowed, no if statements or anything like that. There are only four operations you can do on a variable. 1) You can set a variable to 0. 2) You can set a variable = another variable. 3) You can increment a variable (only by 1), and it's a post increment. 4) You can loop. So, if you were to say loop(v1) and v1 = 10, your loop would execute 10 times, but the value in v1 wouldn't change so the first line in the loop can change value of v1 without changing the number of times you loop. You need to do 3 things. 1) Write a function that decrements by 1. 2) Write a function that subtracts one variable from another. 3) Write a function that divides one variable by another. 4) See if you can implement all 3 using at most 4 variables. Meaning, you're not making function calls now, you're making macros. And at most you can have 4 variables. The restriction really only applies to divide, the other 2 are easy to do with 4 vars or less. Division on the other hand is dependent on the other 2 functions, so, if subtract requires 3 variables, then divide only has 1 variable left unchanged after a call to subtract. Basically, just make your function calls to decrement and subtract so you pass your vars in by reference, and you can't declare any new variables in a function, what you pass in is all it gets.
what is the difference between "types" and "data" in abap.
Given a arbitrary pointer to an element in a singly linked list?what is the time complexity for its deletion .
Explain with examples any 2 features of OOPS.
0 Answers Tavant Technologies, Verifone,
can we extend a class having only one parameterised constructor.Suggest the process to do it.
Expand the following a) ISDN b) CASE c) CSMA/CD d) OOPS e) MIMD
What is procedural oriented language language..? give some examples of this language....?
In project we have Documentation phase also,in that what is micro and macro designing?
what is the BAM? where we can use it in BizTalk server?
how many languages .net is supporting now?
How to use string functions in QTP?give some examples
Always use scope terminator like End-If with IF, End- Evaluate with Evaluate statement.Can somebody explain me the detail logical explanation?