Improve performance of C++/Go's problem 12.