Solves Problem 14 in C++