solution to the CountTriangles problem in C++