C++ solution for problem 13