5
CJam, 45
q~_,@m>0@{@(:T+@T*@+}/\;La{\)_@+@@md@@j@+}jp;
Finally I found a good use of j. But this is slower than the program below.
How it works
Long ArrayList Block j executes the block which takes an integer as the parameter, and Long j will call this block recursively in the block. It will...