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 | » ]
Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

What is the meaning of life? (View in Weblogs)
Posted: Dec 22, 2011 2:54 AM
Reply to this message Reply
Summary
A couple of weeks ago, I sent out a little quiz to my readers of The Java Specialists' Newsletter. No one managed to figure out what the code does without running it. Some managed to explain the result once they had run it. Perfect quiz for weeding out those job applicants you don't like. Especially in the banking industry. Enough hints :-)
Advertisement

A few weeks ago I updated my age to be a factor of 2 and 5. It is the perfect age to reflect what life is all about. Some men don a leather jacket and ride around on a Harley. But as a geek I know exactly where to turn to - my beloved computer.

I needed a long-running method for the new concurrency course I am writing. Something that would take about 15 seconds and that would keep the CPU busy at 100%. Also, since life throws us random events, we would have to include a call to Math.random() in the calculation. In my next article I will explain why Math.random() is dead, long live Java 7, but for now we will call it in order to be super slow.

public class MeaningOfLife {
  public static String findOutWhatLifeIsAllAbout() {
    int meaning = 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++) {
            meaning += Math.random() + 1;
          }
        }
      }
    }
    return String.valueOf(meaning).replaceAll("0*$", "");
  }

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

Think about what the output should be before you run it. Then try it out, preferably with the -server switch. On my machine it takes 15 seconds to find the meaning of life with -server and 25 seconds with -client. Patience is apparently a virtue, though I would not be able to confirm or deny that. Never had patience, which is why I always want the fastest meanest computer that I can get.

The question is: Why is it giving this result? And why does it even compile?

(And yeah, my Artima profile picture seriously needs to be updated. And I now live on the sunny Island of Crete in the Mediterranean Sea. But at least I've blogged again on Artima :-))


Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 22, 2011 2:57 AM
Reply to this message Reply
Please don't post any spoilers until after Christmas :-) Give the good programmers out there a chance to try it out.

Marijn Meijles

Posts: 1
Nickname: marijn
Registered: Dec, 2011

Re: What is the meaning of life? Posted: Dec 22, 2011 6:46 AM
Reply to this message Reply
I'm sorry, but I fail to see why this is so hard. The only thing I can think of is because it has a few 'chained' tricks in it, but the tricks itself are nothing fancy. Even if you don't know DA you should grasp this in 30 sec.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 22, 2011 7:03 AM
Reply to this message Reply
Precisely what I thought Marijn when I wrote it. Which is why I found it so surprising when I ran it ... :-)

Now that you've posted your remark, could you perhaps confirm that it does what you expected it to?

(But please don't post the answer :-))

Andy P

Posts: 2
Nickname: andypaxo
Registered: Dec, 2011

Re: What is the meaning of life? Posted: Dec 22, 2011 9:49 AM
Reply to this message Reply
I believe I have the answer to this, but I'll have to wait until I'm at home with a Java compiler to try it out. However... the answer I have matches up nicely with the name of the problem so I'm fairly confident.

(Not even a Java developer primarily, but I'm assuming that the standard types and libraries work in a similar way to the .NET equivalents).

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 22, 2011 9:59 AM
Reply to this message Reply
Andy, I'd be willing to wager a beer that your answer will be wrong :-) But if you win, you need to come to Crete in Greece to collect the beer :-)

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 22, 2011 11:06 AM
Reply to this message Reply
Here's a Scala version for you Odersky fans out there :-)

def findOutWhatLifeIsAllAbout() = {
var meaning=0
for (i <- 1 to 10) {
for (j <- 1 to 20) {
for (k <- 1 to 300) {
for (m <- 1 to 7000) {
meaning = (meaning + scala.math.random + 1) toInt
}
}
}
}
meaning.toString.replaceAll("0*$", "")
}
println(findOutWhatLifeIsAllAbout)

(Disclaimer: I don't know Scala, so please forgive any style mistakes in the code above)

David Leppik

Posts: 2
Nickname: dleppik
Registered: Feb, 2011

Re: What is the meaning of life? Posted: Dec 23, 2011 2:08 PM
Reply to this message Reply
Cute. I'm pretty sure I know the answer without running it. And I'd wager that it gives exactly the same answer every time it runs. You don't need to know anything about pseudorandom numbers, just read the Javadoc for Math.random() very carefully.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 23, 2011 3:46 PM
Reply to this message Reply
:-) have fun running it

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 23, 2011 3:49 PM
Reply to this message Reply
No hint, just a warning: I've sent this to 50.000 of my Java Specialists Newsletter subscribers. So far, no one has sent me the correct answer without running it. The people on my newsletter are the smartest in the Java ecosystem :-) So it's not obvious.

Andy P

Posts: 2
Nickname: andypaxo
Registered: Dec, 2011

Re: What is the meaning of life? Posted: Dec 23, 2011 6:06 PM
Reply to this message Reply
Heinz,

I was indeed wrong. I look forward to seeing the explanation!

I'll buy you that beer when I'm next in Crete... you might be waiting a long time :)

Kristof Jozsa

Posts: 1
Nickname: dyn
Registered: Oct, 2005

Re: What is the meaning of life? Posted: Dec 23, 2011 7:11 PM
Reply to this message Reply
Heinz, without any offense, if none of your 50k specialists can add two values in Java, I'd rather consider launching some other type of newsletter for them in the future.. :)

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 24, 2011 12:14 AM
Reply to this message Reply
No offense taken. I presume you figured out the answer without running the code?

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 24, 2011 12:23 AM
Reply to this message Reply
If you want you can send me your answer here:

http://www.javaspecialists.eu/archive/Issue197.html

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 24, 2011 4:01 AM
Reply to this message Reply
Sorry Sam, it is not Christmas yet so I've deleted your comment as it contained a spoiler.

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