We all know the Answer to Life, the Universe and Everything (Goolge it). So it was a little surprise when I came across this piece of code that prints out 6 as the ultimate answer.
On close inspection I found that the author seems to have confused theQuestion with theAnswer. Let’s correct this, shall we?
/*
"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
http://en.wikipedia.org/wiki/Notable_phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy
*/
class Life{ /* No additional Semi-colons may be added to this class. */
public static void main(String[] args) {
int theQuestion = 6; /* No Changes to This line! */
int theAnswer = theQuestion*9;
System.out.println("The ultimate answer is " + theQuestion);/* No Changes to This line! */
}
}
http://en.wikipedia.org/wiki/Notable_phrases_from_The_Hitchhiker%27s_Guide_to_the_Galaxy
Get the code to print the correct answer which is 42. Please respect all the wishes of the code author, which he has given as comments.
Its also pretty impolite to delete any code that the author has already written - so no deletes please.
Got an answer? Do leave it here.
package com.twisters;
ReplyDeleteclass Life{ /* No additional Semi-colons may be added to this class. */
public static void main(String[] args) {
int theQuestion = 6; /* No Changes to This line! */
int theAnswer = theQuestion*9;
theQuestion = theAnswer - 10;
System.out.println("The ultimate answer is " + theQuestion);/* No Changes to This line! */
}
}
This first solution gets the result according to the code author requests (plain and simple).
package com.twisters;
class Life{ /* No additional Semi-colons may be added to this class. */
public static void main(String[] args) {
int theQuestion = 6; /* No Changes to This line! */
int theAnswer = theQuestion*9;
while (!(theQuestion%2==0 && theQuestion%6==0 && theQuestion%7==0))
theQuestion ++;
System.out.println("The ultimate answer is " + theQuestion);/* No Changes to This line! */
}
}
This second solution not only respects the wishes of the code author, as well as Douglas Adams' (An ordinary number that could be divided by 2, 6 and 7).
Cheers,
Bernardo
Replace "int theAnswer = theQuestion * 9;" with
ReplyDelete"theQuestion = theQuestion * 9 - 12;", and that it =)
class Life{ /* No additional Semi-colons may be added to this class. */
ReplyDeletepublic static void main(String[] args) {
int theQuestion = 6; /* No Changes to This line! */
int theAnswer = theQuestion = ((5*9) - 3);
System.out.println("The ultimate answer is " + theQuestion);/* No Changes to This line! */
}
}
I put an equals sign immediately after theQuestion, then used the *9 with 5 to get 45, then subtracting 3 to get 42.
Hey, its not pretty, but it took Deep Thought millions of years to come up with the answer!
Line 11:
ReplyDeleteint theAnswer = theQuestion = theQuestion*7;
int theAnswer = (theQuestion*=7)*9;
ReplyDeletethis should do it ...
The output is:
ReplyDeleteThe ultimate answer is 6
In the println() statement, 'theQuestion' variable is used whose value is 6. 'theAnswer' variable is initiated with 54, but is not used in the println() statement.
6х9 = 42? Righ, I must forgot math completely.
ReplyDeleteAnyway to make this code print 42 is easy:
int theAnswer = theQuestion *= (9-2);
PS Good puzzle :)
changing the second line like this works for me:
ReplyDeleteint theAnswer = theQuestion = 6 * 9 - 12;
Or in order to actually calculate (without tweaking it as obviously) the answer we need to use base 13 (because this is the base our universe is using internally, obviously):
int theAnswer = theQuestion = Integer.parseInt(Integer.toString(Integer.parseInt(Integer.toString(theQuestion, 13)) * Integer.parseInt("9", 13), 13));
class Life{ /* No additional Semi-colons may be added to this class. */
ReplyDeletepublic static void main(String[] args) {
int theQuestion = 6; /* No Changes to This line! */
int theAnswer = theQuestion+=9+27;
System.out.println("The ultimate answer is " + theQuestion);/* No Changes to This line! */
}
}
public static void main(String[] args) {
ReplyDeleteint theQuestion = 6; /* No Changes to This line! */
int theAnswer = (theQuestion = 42) * 9;
System.out.println("The ultimate answer is " + theQuestion);/* No Changes to This line! */
}
In keeping with the no deletions (And expecting that commenting will be treated as deletions) I submit:
ReplyDeleteint theAnswer = theQuestion = theQuestion * (9 - 2);
But if I can comment....
/*int theAnswer*/ theQuestion = theQuestion * /*9*/ 7;
theAnswer = (theQuestion *= 9 - 2);
ReplyDeleteNot sure if that is what you wanted but it prints out 42
class Life { /* No additional Semi-colons may be added to this class. */
ReplyDeletepublic static void main(String[] args) {
int theQuestion = 6; /* No Changes to This line! */
int theAnswer = theQuestion = 42;
System.out.println("The ultimate answer is " + theQuestion);/*No to This line! */
}
}
public static void main(String[] args) {
ReplyDeleteint theQuestion = 6; /* No Changes to This line! */
int theAnswer = theQuestion *= 9-2;
System.out.println(
"The ultimate answer is " + theQuestion);/* No Changes to This line! */
}