Solve problem 41 using Java.