Wednesday, July 1, 2009

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

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

Everyone seems to have their own opinion on life. The correct answer as we all know (Check Puzzle 34 for Details) is 42 – so it looks like some one has made a typo in the code snippet below. Let’s correct this code shall we?

/* Code Snippet */
int theAnswer = 52;
System.out.println(
"The answer to life is " + theAnswer);

I was about to delete the 5 and replace it with a 4 when one of my friends (she’s imaginary, but that's beside the point) pointed out that it's pretty impolite to delete someone else's code. She also tells me that I should make minimum changes to get this code to work. So I am stuck trying to figure out what is the minimum number of characters I can add (no deletes allowed) to get this code to work.

Think you could help me with this? Leave an answer here.

10 comments:

  1. This must be the only case where octal notation actually is solving and not causing the problem!

    int theAnswer = 052;

    ReplyDelete
  2. int theAnswer = 052;

    octal, you know ;)

    ReplyDelete
  3. Cool puzzle :)
    It can be resolved using octal number:

    int theAnswer = 052;

    ReplyDelete
  4. Nice one!
    52 octal is 42 and octal literals use the '0' prefix:

    so
    int theAnswer = 052;

    yields '42' as the answer and is only one character added, which is provable the minimum number of characters that can be added to get the correct answer.

    ReplyDelete
  5. Just one character : 0

    int theAnswer = 052;

    ReplyDelete
  6. Base10(52) == octal(42) so add a 0 before the number 52 = 1 char addition:
    /* Code Snippet */
    int theAnswer = 052;
    System.out.println("The answer to life is " + theAnswer);

    ReplyDelete
  7. Change line 2 to:
    int theAnswer = 52 - 10;

    ReplyDelete
  8. I added 3 characters:

    int theAnswer = 52-10;
    System.out.println("The answer to life is " + theAnswer)

    ReplyDelete
  9. Change it to 3 System.out.println("The answer to life is " + theAnswer-10);

    ReplyDelete
  10. int theAnswer = 5/2;
    System.out.println("The answer to life is 4" + theAnswer);

    ReplyDelete

Solution for this question?