Discuss this pageDisclaimer: 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. Laban Henrik Torstensson Tue 7-Jun-2011 22:56 | *Spoiler alert* Here is my solution: http://pastebin.com/DpNHxAA8 | Anonymous Wed 13-Jul-2011 17:29 | 1.This is my answer to Linus Åkesson's Java challenge <http://www.linusakesson.net/programming/what/java1.php>. 2. 3.The Java program multiplies two numbers given as arguments. Example: 4. 5. laban@femto:~/tmp$ java Main 238 13 6. 3094 7. 8.The multiplication is done using "Ancient Egyptian multiplication", see <http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication>. 9. 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. 11. 12.The exception from the example above looks as follows: 13. java.lang.ArithmeticException: / by zero 14. at Main.recurse(Main.java:5) 15. at Main.recurse(Main.java:7) 16. at Main.recurse(Main.java:5) 17. at Main.recurse(Main.java:5) 18. at Main.recurse(Main.java:7) 19. at Main.recurse(Main.java:7) 20. at Main.recurse(Main.java:7) 21. at Main.recurse(Main.java:7) 22. at Main.recurse(Main.java:7) 23. at Main.recurse(Main.java:7) 24. at Main.recurse(Main.java:7) 25. at Main.recurse(Main.java:7) 26. at Main.recurse(Main.java:7) 27. at Main.recurse(Main.java:7) 28. at Main.recurse(Main.java:7) 29. at Main.recurse(Main.java:7) 30. at Main.recurse(Main.java:7) 31. at Main.recurse(Main.java:7) 32. at Main.recurse(Main.java:7) 33. at Main.recurse(Main.java:7) 34. at Main.recurse(Main.java:7) 35. at Main.recurse(Main.java:7) 36. at Main.recurse(Main.java:7) 37. at Main.recurse(Main.java:7) 38. at Main.recurse(Main.java:7) 39. at Main.recurse(Main.java:7) 40. at Main.recurse(Main.java:7) 41. at Main.recurse(Main.java:7) 42. at Main.recurse(Main.java:7) 43. at Main.recurse(Main.java:7) 44. at Main.recurse(Main.java:7) 45. at Main.main(Main.java:12) 46. 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. :) 48. 49.The catch block in the code will perform the multiplication according to the "Ancient Egyptian" algorithm. | Oscar2 Wed 25-Jan-2012 02:58 | It does the same thing on both sides of the if:
if(0 != (x & 1073741824)) recurse(x * 2, n - n / n); else 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. |
|