The Artima Developer Community
Sponsored Link

Weblogs Forum
What is the meaning of life?

57 replies on 4 pages. Most recent reply: Nov 14, 2012 10:21 AM by Heinz Kabutz

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 57 replies on 4 pages [ « | 1 2 3 4 ]
Lyndon Samson

Posts: 2
Nickname: samsonl
Registered: Jan, 2012

Re: What is the meaning of life? Posted: Jan 31, 2012 6:54 PM
Reply to this message Reply
Advertisement
Fixed :-) With the added avantage that it now runs much much more slowly...

Out of curiousity, has anyone run it on a non intel x86 fpu?

import java.math.BigDecimal;
 
public class MeaningOfLife {
  public static String findOutWhatLifeIsAllAbout() {
    int meaning = 0;
    for (int i = 0; i < 10; i++) {
      System.out.println("Running"+i);
      for (int j = 0; j < 20; j++) {
        System.out.println("  Running : "+j);
        for (int k = 0; k < 300; k++) {
          for (int m = 0; m < 7000; m++) {
            meaning = (new BigDecimal(meaning).add(new BigDecimal(Math.random())).add(BigDecimal.ONE)).intValue();
          }
        }
      }
    }
    return String.valueOf(meaning).replaceAll("0*$", "");
  }
 
  public static void main(String[] args) {
    System.out.println(findOutWhatLifeIsAllAbout());
  }
}

Lyndon Samson

Posts: 2
Nickname: samsonl
Registered: Jan, 2012

Re: What is the meaning of life? Posted: Jan 31, 2012 6:58 PM
Reply to this message Reply
Old but interesting article, wonder if the issues are still extant? http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf

Keith Brings

Posts: 3
Nickname: keithnoizu
Registered: Mar, 2012

Re: What is the meaning of life? Posted: Mar 27, 2012 3:14 PM
Reply to this message Reply
With out running the code or reading the comments i'll take a guess that its 42.

1. The Douglas Adams Reference.
2. I havent done java in a long time but I believe Math.Random() returns an objects. The next part baffles me but im assuming either because of the static address space or because the object is never references that Math.Random() + 1 always equals 1. So this has us adding up a large power of 10 of 42 and then stripping out the trailing zeros.

If I am write its entirely due to the title of the article.

Keith Brings

Posts: 3
Nickname: keithnoizu
Registered: Mar, 2012

Re: What is the meaning of life? Posted: Mar 27, 2012 3:15 PM
Reply to this message Reply
Oh man, the typos and word choice mistakes start pouring in after pulling an all nighter >_<

Keith Brings

Posts: 3
Nickname: keithnoizu
Registered: Mar, 2012

Re: What is the meaning of life? Posted: Mar 27, 2012 3:16 PM
Reply to this message Reply
Ahh not even close. ^_^.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Mar 28, 2012 12:58 AM
Reply to this message Reply
Nope :-)

philippos papadatos

Posts: 6
Nickname: firesoft
Registered: Mar, 2012

Re: What is the meaning of life? Posted: Mar 28, 2012 11:25 PM
Reply to this message Reply
package meaningOfLife;

public class MeaningOfLife {
public static String findOutWhatLifeIsAllAbout() {
double meaning = 0;
int temp = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 20; j++) {
for (int k = 0; k < 300; k++) {
for (int m = 0; m < 7000; m++) {
temp = (int)Math.random();
meaning += temp + 1;
}
}
}
}
return String.valueOf(meaning).replaceAll("0*$", "");
}

public static void main(String[] args) {
System.out.println(findOutWhatLifeIsAllAbout());
}
}

the temp value is usefull for debuging purposes, if you change both variables temp,meaning to type double the results are correct and as expected.

the value of temp is always zero because the decimal places are thrown away when it is declared as int.

Luke DeStevens

Posts: 1
Nickname: jtempest
Registered: Apr, 2012

Re: What is the meaning of life? Posted: Apr 9, 2012 9:49 AM
Reply to this message Reply
"No one managed to figure out what the code does without running it."

Challenge accepted (though I'm sure everyone has already done so by now, since it was December).

Nice puzzle! Took me about a minute. I believe it prints out 42.

Here is my analysis:
Each of the four nested loops results in "meaning += Math.random() +1" being run 20*30*700*2000 times, or 420000000 times. So, "meaning" gets Math.random() + 1 added to it 420000000 times. Since Math.random() returns a double (or maybe it was a float) greater than or equal to 0, but less than 1, and since "meaning" is an integer, the Math.random() value gets dropped every time. As such, "meaning" has a final value of 420000000. The String.replaceAll() call reduces the string to 42.

This is a reference to Hitchhiker's Guide to the Galaxy; 42 is the answer to the ultimate question of life, the universe, and everything. The real question, though, is "what is the question?"

I almost missed realizing that the result of Math.random() gets dropped, and I must admit that I don't get why "*$" is in the replaceAll method replace string. I assume it to be a regular expression, which I am not familiar enough with.

I haven't run it yet, so if I'm wrong or have missed something, that'd explain why.

This reminds me of another problem I saw posted somewhere a long time ago, which was basically "Given the lowest common multiple of all integers between 1 and 10 (inclusive), write a piece of code that finds the lowest common multiple of all integers between 1 and 20 (inclusive)".

Fun times.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Apr 9, 2012 10:14 AM
Reply to this message Reply
Thanks for trying, Luke. Unfortunately it is still true that "No one managed to figure out what the code does without running it."

But don't worry, I include myself in that. My intention was to get "42", but the result differed, which made me rather curious.

Heinz

philippos papadatos

Posts: 6
Nickname: firesoft
Registered: Mar, 2012

Re: What is the meaning of life? Posted: May 5, 2012 11:16 PM
Reply to this message Reply
anyway there is no point, you always must run and test your programming concepts its like singing but only in you head,trying to solve a programming puzzle without a computer,you have to make sound in order to sing...

Girish Kolanthra

Posts: 1
Nickname: girishkola
Registered: Sep, 2012

Re: What is the meaning of life? Posted: Sep 27, 2012 3:27 PM
Reply to this message Reply
public class MeaningOfLife {
public static String findOutWhatLifeIsAllAbout() {
int meaning = 0;
int oldMeaning = 0;
int deviation = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 20; j++) {
for (int k = 0; k < 300; k++) {
for (int m = 0; m < 7000; m++) {
oldMeaning = meaning;
meaning += Math.random() + 1;

if((meaning - oldMeaning) > 1) {
deviation++;
}
}
}
}
}
System.out.println(deviation);
return String.valueOf(meaning).replaceAll("0*$", "");
}

public static void main(String[] args) {
System.out.println(findOutWhatLifeIsAllAbout());
}
}

wang Anna

Posts: 1
Nickname: annawang
Registered: Nov, 2012

Re: What is the meaning of life? Posted: Nov 14, 2012 3:42 AM
Reply to this message Reply
I am really confused about this article.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Nov 14, 2012 10:21 AM
Reply to this message Reply
I think most people are confused by what life is all about ;-)

Flat View: This topic has 57 replies on 4 pages [ « | 1  2  3  4 ]
Topic: Service Locator Pattern Revisited, Part 2 Previous Topic   Next Topic Topic: Style is Substance


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us