Here's a cool alternative to solve the last problem: interpreting the problem as choosing 3 edges from n different colours with repetition. It turns out that for any given combination of 3 out of the n colours, that represents exactly 1 configuration (thanks to the symmetries of the given reflections/rotations in 3D space). This means you can solve this problem as a combinations with repetition problem using multichoose: (n + 3 - 1)C(3) = (n + 2)C(3) or also (n + 3 - 1)C(n + 3 - 1 - 3) = (n + 2)C(n - 1)