Problem 22: solved in Java