P172: Added explanation and comments.
P172: Added Mathematica solution, tweaked Java solution.