Re: Question about a "combinations" algorithm Message #10 Posted by Gilles Carpentier on 20 Sept 2012, 4:51 p.m., in response to message #1 by Namir
Hi Namir, if I understand well your problem, here is a 39gII program :
EXPORT Perm(k,s)
BEGIN
LOCAL a,b,j;
FOR j FROM 2 TO SIZE(s) DO
a:=(k MOD j)+1; b:=s(a); s(a):=s(j); s(j):=b;
k:=iquo(k,j);
END;
s;
END;
Usage :
Perm(5,{"a","b","c","d"})
Return the 5th permutation of the set. > {"d","b","c",a"}
As there are SIZE(s)! possible permutations,this program displays all the possible permutations
EXPORT PermAll(s)
BEGIN
LOCAL j;
FOR j FROM 1 TO SIZE(s)! DO
PRINT(Perm(j,s));
END
END;
With 6 elements in the list, there is 720 permutations
Of course the list may contain numbers (or matrix, or others list etc.)
PS for TIM :
A SWAP command would be welcome on the 39gII ;)
a:=(k MOD j)+1; b:=s(a); s(a):=s(j); s(j):=b;
>
SWAP(s((k MOD j)+1),s(j));
PS : I realise that perhaps you also want solutions like :
{ "a" } {"a" ,"b" } with {"a","b","c","d"} input ??
Edited: 20 Sept 2012, 6:02 p.m.
