Problem 23: solved in C++