Solves Problem 17 in C++