Help:=proc(): print(`IsGood1(pi,pat), Red1(p), `): print(`IsGood(pi,patS), Wilf(n, SetP)`): end: with(combinat): #Red1(p): Inputs a perm p of length 3 #outputs the equiv. using 1,2,3 Red1:=proc(p) local p1,a,b,c: p1:=sort(p): a:=p1[1]: b:=p1[2]: c:=p1[3]: subs({a=1,b=2,c=3},p): end: ####Begin slow version (Only for testing!, do not use) #Does the perm pi contain the pattern #of length 3 pat IsGood1S:=proc(pi,pat) local n,i1,i2,i3: n:=nops(pi): for i1 from 1 to n-2 do for i2 from i1+1 to n-1 do for i3 from i2+1 to n do if Red1([pi[i1],pi[i2],pi[i3]])=pat then RETURN(false): fi: od: od: od: true: end: IsGoodS:=proc(pi,SetP) local i: evalb({seq(IsGood1S(pi,SetP[i]),i=1..nops(SetP))}={true}): end: #WilfS(n,SetP): slow version of Wilf(n,SetP) WilfS:=proc(n,SetP) local S1,S2,i: S1:=permute(n): S2:={}: for i from 1 to nops(S1) do if IsGoodS(S1[i],SetP) then S2:=S2 union {S1[i]}: fi: od: S2: end: ######End Slow version #Begin faster version #Does the perm pi contain the pattern #of length 3 pat assuming that the chopped #version is good IsGood1:=proc(pi,pat) local n,i1,i2,i3: n:=nops(pi): for i1 from 1 to n-2 do for i2 from i1+1 to n-1 do for i3 from n to n do if Red1([pi[i1],pi[i2],pi[i3]])=pat then RETURN(false): fi: od: od: od: true: end: IsGood:=proc(pi,SetP) local i: evalb({seq(IsGood1(pi,SetP[i]),i=1..nops(SetP))}={true}): end: #Append1(pi,i): inputs a permutation and outputs # a permutation of length one more whose last #entry is i, and such that the reduction of the #first nops(pi) entries reduces to pi Append1:=proc(pi,i) local j,n: n:=nops(pi): #subs({i=i+1,i+1=i+2 , ..., i+n-i=i+n-i+1},pi) [ op(subs({seq(i+j=i+j+1,j=0..n-i)},pi)), i ]: end: Wilf:=proc(n,SetP) local S1,S2,ca,j,i: option remember: if n=0 then RETURN({[]}): fi: S1:=Wilf(n-1,SetP): S2:={}: for i from 1 to nops(S1) do for j from 1 to n do ca:=Append1(S1[i],j): if IsGood(ca,SetP) then S2:=S2 union {ca}: fi: od: od: S2: end: