Total number of groupings in a $2n$-gon: \[ P_n = \frac{(2n)!}{2^n \cdot n!} \] $n$-th Catalan number (Total numer of groupings in a $2n$-gon that results in a sphere): \[ C_n = \frac{1}{n+1}{2n \choose n} = \frac{(2n)!}{(n+1)!n!} \] Total number of groupings in a $2n$-gon that results in a torus ($n \geq 3$): \begin{align*} T_n & = C_{n-2}\sum_{i=1}^{2n-3} i(2n-2-i) \\ & = \frac{1}{6} \frac{(2n-4)!}{(n-2)!(n-1)!} (2n-3)(2n-2)(2n-1) \\ & = \frac{1}{6} \frac{(2n-1)!}{(n-2)!(n-1)!} \end{align*} You can test some of the functions here:
Edge Grouping:
You should enter an array of length $2n$ with value $1, 2, ..., n$ each appears twice
Number of edges in the polygon:
You should enter an even number.
Number of trials: