Update C++ Problem 31

We now use the same algorithm as in the Python problem.