Simple C++ solution for 47