Help:=proc(): print(`redu(pi), Cont1(pi,sig), Cont(pi,S)`): print(`A(n,S),SeqSN(S,N) `): end: with(combinat): #redu(pi): inputs a list of different integers, and outputs #the reduced form e.g. redu([3,5,2,4])=[2,4,1,3] redu:=proc(pi) local pi1,T,i: pi1:=sort(pi): for i from 1 to nops(pi1) do T[pi1[i]]:=i: od: [seq(T[pi[i]],i=1..nops(pi))]: end: #Cont1(pi,sig): inputs a perm. pi and a pattern sig #(a shorter perm.) returns true iff pi contains the #pattern sig Cont1:=proc(pi,sig) local n,k,S,i,s: k:=nops(sig): n:=nops(pi): S:=choose(n,k): for s in S do if redu([seq(pi[s[i]],i=1..k)])=sig then RETURN(true): fi: od: false: end: #Cont(pi,S): does pi contain (at least one) of the patterns in S #e.g. cont([3,1,2],{[1,2],[2,1]}); Cont:=proc(pi,S) local s: for s in S do if Cont1(pi,s) then RETURN(true): fi: od: false: end: #A(n,S): The set of perms of [1,n] avoiding all the patterns of #the set of patterns S A:=proc(n,S) local G,G1,pi,cand,i: option remember: if n=0 then RETURN({[]}): fi: G1:=A(n-1,S): G:={}: for pi in G1 do for i from 1 to n do cand:=[op(1..i-1,pi),n,op(i..n-1,pi)]: if not Cont(cand,S) then G:=G union {cand}: fi: od: od: G: end: #SeqSN(S,N): inputs a set of patterns S, and a pos. integer #N, outputs the first N terms in the counting sequence #for perms. of [1,n] avoiding the patterns of S SeqSN:=proc(S,N) local n: [seq(nops(A(n,S)),n=1..N)]: end: #stolen from OEIS count1324 := proc(n::nonnegint) if (n<4) then return n!; fi; if (n=4) then return 23; fi; return nodes([5, 5, 5, 5], n-5) + nodes([5, 3, 5, 5], n-5) + nodes([5, 4, 4, 5], n-5) + nodes([5, 5, 4, 5], n-5) + nodes([4, 3, 4], n-5) + nodes([5, 3, 4, 5], n-5); end: nodes := proc(p, h) option remember; local i, j, s, l; if (h=0) then return convert(p, `+`); fi; s := 0; for j to nops(p) do l := p[j]+1; for i from 2 to j do l := l, `min`(j+1, p[i]); od; for i from j+1 to p[j] do l := l, p[i-1]+1; od; s := s+nodes([l], h-1); od; return s; end: BeHead1:=proc(w) local i: redu([op(2..nops(w),w)]): end: BeHead:=proc(S) local s: {seq(BeHead1(s),s in S)}:end: