#C5.txt Help5:=proc(): print(`WtE(S,f,x), RF(x,n),LtoR(pi) `): print(`inv(pi), maj(pi) `): end: with(combinat): #maj(pi): The major index : The sum of the places where #pi[i]>pi[i+1] maj:=proc(pi) local n,i,co: co:=0: n:=nops(pi): for i from 1 to n-1 do if pi[i]>pi[i+1] then co:=co+i: fi: od: co: end: #inv(pi): The number of inversions of the permutation pi #For example inv([1,2,3])=0, inv([3,2,1])=3 inv:=proc(pi) local n,i,j,co: n:=nops(pi): co:=0: for i from 1 to n do for j from i+1 to n do if pi[i]>pi[j] then co:=co+1: fi: od: od: co: end: #LtoR(pi): The list places that are larger than anything #to the left pi=[2,1,4,3] LtoR:=proc(pi) local n,L,i,ma: n:=nops(pi): L:=[1]: #ma is the current world record ma:=pi[1]: for i from 2 to n do if pi[i]>ma then L:=[op(L), i ]: ma:=pi[i]: fi: od: L: end: #x*(x+1)*...*(x+n-1) RF:=proc(x,n) local i: mul(x+i,i=0..n-1): end: #WtE(S,f,x): add(x^f(s), s in S) WtE:=proc(S,f,x) local s: add(x^(f(s)), s in S) end: ##start old stuff from C3.txt #C3.txt, Jan. 29, 2026 Help3:=proc(): print(`FP(pi), Der(n), d(n) , ExtractCycle(pi,i) , CycDec(pi) `): end: #FP(pi): The number of fixed points of per. pi FP:=proc(pi) local n, i,co: n:=nops(pi): co:=0: for i from 1 to n do if pi[i]=i then co:=co+1: fi: od: co: end: FP([2,1,4,3,5,6]); # Der(n): The set of derangements (i.e. permutations w/o fixed points) of {1, ..., n} Der := proc(n) local S,pi,DE: S:=permute(n): DE:={}: for pi in S do if FP(pi)=0 then DE:=DE union {pi}: fi: od: DE: end: #a(n):=nops(permute(n)) # #Solutions SOl. #a(n)-n*a(n-1)=0, a(0)=1 HOMOG. linear (first-order) recurrence # #d(n):=|Der(n)| # d:=proc(n) option remember: if n=1 then 0 else n*d(n-1)+(-1)^n fi:end: #ExtractCycle(pi,i): The cycle corresponding to the member i of the permutation pi #For example ExtractCycle([2,1,4,3],1)=[1,2] ExtractCycle:=proc(pi,i) local C,ng: C:=[i]: ng:=pi[i]: while ng<>i do C:=[op(C),ng]: ng:=pi[ng]: od: C: end: #CycDec(pi): The full cyclic decomposition of pi CycDec:=proc(pi) local n,i,StillToDo,S,C,ng: n:=nops(pi): StillToDo:={seq(i,i=1..n)}: S:={}: while StillToDo<>{} do ng:=StillToDo[1]; C:=ExtractCycle(pi,ng): S:=S union {C}: #S\T : S minus T StillToDo:=StillToDo minus {op(C)}: od: S: end: