Faster way to calculate triangles