Rolling up some integers

Michael Somos somos at grail.cba.csuohio.edu
Sun Mar 24 20:00:21 CET 2002


Seqfans,

Wouter Meeussen wrote :

> Ken Levasseur replied with an elegant permutation technique that can be
> thrown into the following one-liner:

> flatindex[n_]:=Ordering[Sort[Flatten[Outer[List,Range[n],Range[n]],1]//Rever
> se,Plus@@#1<=Plus@@#2&]/. {i_Integer,j_}->n(i-1)+j]
 
I think this is a bit more than one line and Mathematica encourages
baroque programming practices. However, I have better PARI-GP code :

? U(n,i,j) = 1-i+if(i+j<=n,binomial(i+j,2),n^2+n-binomial(2*(n+1)-i-j,2))
? print(matrix(4,4,i,j,U(4,i,j)))
[1, 3, 6, 10; 2, 5, 9, 13; 4, 8, 12, 15; 7, 11, 14, 16]
 
Shalom, Michael





More information about the SeqFan mailing list