Wednesday, June 17, 2009

Puzzle 34 – The Answer to Life, the Universe and Everything

Language – Java | Type – Concept | Last date 21-Jun-2009 12:00 p.m. IST | Points 3

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.

14 comments:

  1. 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;
    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

    ReplyDelete
  2. Replace "int theAnswer = theQuestion * 9;" with
    "theQuestion = theQuestion * 9 - 12;", and that it =)

    ReplyDelete
  3. 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 = ((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!

    ReplyDelete
  4. Line 11:
    int theAnswer = theQuestion = theQuestion*7;

    ReplyDelete
  5. int theAnswer = (theQuestion*=7)*9;

    this should do it ...

    ReplyDelete
  6. The output is:
    The 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.

    ReplyDelete
  7. 6х9 = 42? Righ, I must forgot math completely.

    Anyway to make this code print 42 is easy:

    int theAnswer = theQuestion *= (9-2);

    PS Good puzzle :)

    ReplyDelete
  8. changing the second line like this works for me:

    int 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));

    ReplyDelete
  9. 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+27;
    System.out.println("The ultimate answer is " + theQuestion);/* No Changes to This line! */
    }
    }

    ReplyDelete
  10. public static void main(String[] args) {
    int 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! */
    }

    ReplyDelete
  11. In keeping with the no deletions (And expecting that commenting will be treated as deletions) I submit:
    int theAnswer = theQuestion = theQuestion * (9 - 2);
    But if I can comment....
    /*int theAnswer*/ theQuestion = theQuestion * /*9*/ 7;

    ReplyDelete
  12. theAnswer = (theQuestion *= 9 - 2);

    Not sure if that is what you wanted but it prints out 42

    ReplyDelete
  13. 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 = 42;
    System.out.println("The ultimate answer is " + theQuestion);/*No to This line! */
    }
    }

    ReplyDelete
  14. public static void main(String[] args) {
    int 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! */
    }

    ReplyDelete

Solution for this question?