Java challenge

What does the following code do — and how?

Beware of spoilers in the comments... or be the first one to put them there.

public class Main {
        private static void recurse(int x, int n) {
                if(0 != (x & 1073741824))
                        recurse(x * 2, n - n / n);
                        recurse(x * 2, n - n / n);
        public static void main(String[] args) {
                int x = 0, y = Integer.parseInt(args[0]);
                try {
                        recurse(Integer.parseInt(args[1]), 30);
                } catch (Exception e) {
                        StringWriter s = new StringWriter();
                        e.printStackTrace(new PrintWriter(s));
                        for(byte b : s.toString().getBytes()) switch(b) {
                                case 53: x += y;
                                case 55: y += y;

Download the program

Honorary mention to Magnus Andersson who was the first to decipher this code, among worthy opponents.

More obfuscated programming

Posted Tuesday 7-Jun-2011 19:27

Discuss this page

Disclaimer: I am not responsible for what people (other than myself) write in the forums. Please report any abuse, such as insults, slander, spam and illegal material, and I will take appropriate actions. Don't feed the trolls.

Jag tar inget ansvar för det som skrivs i forumet, förutom mina egna inlägg. Vänligen rapportera alla inlägg som bryter mot reglerna, så ska jag se vad jag kan göra. Som regelbrott räknas till exempel förolämpningar, förtal, spam och olagligt material. Mata inte trålarna.

Henrik Torstensson
Wed 8-Jun-2011 00:56
*Spoiler alert*
Here is my solution:
Wed 13-Jul-2011 19:29
1.This is my answer to Linus Åkesson's Java challenge <>.
3.The Java program multiplies two numbers given as arguments. Example:
5. laban@femto:~/tmp$ java Main 238 13
6. 3094
8.The multiplication is done using "Ancient Egyptian multiplication", see <>.
10.The recurse() function is always called 30 times. The last time it will raise a java.lang.ArithmeticException because a division by zero occurs when n = 0.
12.The exception from the example above looks as follows:
13. java.lang.ArithmeticException: / by zero
14. at Main.recurse(
15. at Main.recurse(
16. at Main.recurse(
17. at Main.recurse(
18. at Main.recurse(
19. at Main.recurse(
20. at Main.recurse(
21. at Main.recurse(
22. at Main.recurse(
23. at Main.recurse(
24. at Main.recurse(
25. at Main.recurse(
26. at Main.recurse(
27. at Main.recurse(
28. at Main.recurse(
29. at Main.recurse(
30. at Main.recurse(
31. at Main.recurse(
32. at Main.recurse(
33. at Main.recurse(
34. at Main.recurse(
35. at Main.recurse(
36. at Main.recurse(
37. at Main.recurse(
38. at Main.recurse(
39. at Main.recurse(
40. at Main.recurse(
41. at Main.recurse(
42. at Main.recurse(
43. at Main.recurse(
44. at Main.recurse(
45. at Main.main(
47.Note the line numbers in the stack trace. The line number is 5 or 7 depending on if the 31st least significant bit in x is set or not. If you add debug code in the recurse() function, the line numbers might change and the program will stop working. :)
49.The catch block in the code will perform the multiplication according to the "Ancient Egyptian" algorithm.
Wed 25-Jan-2012 03:58
It does the same thing on both sides of the if:

if(0 != (x & 1073741824))
recurse(x * 2, n - n / n);
recurse(x * 2, n - n / n);

Did you mean to do that?
I don't see this thing doing the Egyptian multiplication described in the previous post.
Wed 29-Feb-2012 23:33
It's called a recursive function. Hence the name he gave the function.
The function will continue while the result is not zero, and finish off any remaining recursive function calls.

I don't plan to look too much at the code or to see what it does. Not my thing.
Sun 31-Aug-2014 10:02

They have different line numbers. When the exception happens the exception will say which line number it happened on. The program reads the text of the exception to see which line it had the exception on.