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 :-))
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.
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).
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)
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.
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.
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.. :)