global function permute(integer n, sequence set) -- -- return the nth permute of the given set. -- n should be an integer in the range 1 to factorial(length(set)) -- sequence res integer w n -= 1 res = set for i=length(set) to 1 by -1 do w = remainder(n,i)+1 res[i] = set[w] set[w] = set[i] n = floor(n/i) end for return res end function