#Python program for A368054 by John Rascoe 1/15/2024 from itertools import permutations, combinations from collections import Counter def A3680054(row_n): C,b = Counter(),[] p = list(permutations(list(range(1,(2*row_n)+1)))) #Sieves out perms that have alternating parity starting odd and puts them in list b for k in range(len(p)): c = 0 for j in range(len(p[k])): if p[k][j]%2 == (j+1)%2: c += 1 else: break if c == (2*row_n): b.append(p[k]) for k in range(len(b)): cx,X = 0,[] #internal loop for a given partition #first puts all pairs of terms with absolute difference > 1 in list X for j in range(1,len(b[k])): x,y = 0,0 if abs(b[k][j] - b[k][j-1]) > 1: x += b[k][j] y += b[k][j-1] X.append((y,x)) #first end point if y%2 != b[k][0]%2: if x > y and y < b[k][0] < x: cx += 1 elif x < b[k][0] < y: cx += 1 #last endpoint if x%2 != b[k][-1]%2: if x < y and x < b[k][-1] < y: cx += 1 elif y < b[k][-1] < x: cx += 1 #finds internal crossings if len(X) > 0: Xp = list(combinations(X,2)) for k1 in range(len(Xp)): a = Xp[k1][0][0] c = Xp[k1][1][0] e = min(Xp[k1][0]) f = max(Xp[k1][0]) g = min(Xp[k1][1]) h = max(Xp[k1][1]) if (a%2) == (c%2): if (g < e < h and h < f) or (g < f < h and e < g): cx += 1 C.update({cx}) #optional printout below for all partitions in a row and how many crossings for each #print("Partition",b[k],"has",cx,"crossings") zz = list(C[i] for i in range(0,max(C)+1)) return(zz) for i in range(5): print(A3680054(i))