Hop wrote on July 12:
Quote:
> Given a number n, the algorithm calculate the nth permutation of an
> ordered tuple ie the nth tuple of all permutation in lexical order.
From <http://chilton.com/~jimw/permute.html>:
{del} Z{<-}J LEXPERM1 N;C;I
[3] Z{<-},1
[4] C{<-}1+({reverse}1+{iota}N-1){represent}J
[5] I{<-}N
[6] L1:{->}(0=I{<-}I-1)/0
[7] Z{<-}C[I],Z+Z{>=}C[I]
[8] {->}L1
{del}
{del} Z{<-}J LEXPERM N;C;I
+} {rho}Z {<-}{->} ({rho}J),N
[4] Z{<-}(1,{times}/{rho}J){rho}1
[5] C{<-}1+({reverse}1+{iota}N-1){represent},{transpose}J
[6] I{<-}N
[7] L1:{->}(0=I{<-}I-1)/L2
[8] Z{<-}C[I;]{commabar}Z+Z{>=}({rho}Z){rho}C[I;]
[9] {->}L1
[10] L2:Z{<-}{transpose}((1{take}{rho}Z),{reverse}{rho}J){rho}Z
{del}
1 2 3 4 5 7 6
P{<-}0 1000 5039 LEXPERM 7
P
I found Lehmer's algorithm in the paper "Permutation Generation
Methods", by Robert Sedgewick, in _Computing Surveys_, Vol. 9, No. 2,
June 1977.
Jim