Hi all !

You can do it in two steps? Find all combinations, then all permutations of each combination?

perm=: (A.~i.@!@#)

combbit=: 4 : 0
or=. 23 b.
lshift=. 33 b.
lrot=. 32 b.
shift=. 2 ((_2 lrot 2 | [) or _1 lshift ])/\ ]
n=. <:##:_1 (32 b.) 1
l=. >.y%n
p=. y->:i.>:d=.y-x
k=. <"_1(<.p%n)|."0 1((l-1)$0),"1 0 [2 ^&.x: n|p
z=. (d$<(l,0)$0),<(l,1)$0
for. i.x do.
  if. l=1 do.
    z=. (_1 lshift 1 {. ])&.> z
  else.
    z=. ((_1 lshift 0 { ]) , [: shift ])&.> z
  end.
  z=. ,.&.>/\. z
  z=. k (or)&.> z
end.
z=. >,.&.>/z
z=. <@:#:"1 z
z=. >,.&.>/z
z
)

sper=: 4 : ',/(perm i.x){"_ 1 (x combbit y)#i.y'

   3 sper 4
0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0
0 1 3
0 3 1
1 0 3
1 3 0
3 0 1
3 1 0
0 2 3
0 3 2
2 0 3
2 3 0
3 0 2
3 2 0
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
   2 sper 4
0 1
1 0
0 2
2 0
0 3
3 0
1 2
2 1
1 3
3 1
2 3
3 2
   1 sper 4
0
1
2
3

Cheers,

Erling Hellenäs

Den 2017-11-20 kl. 08:49, skrev 'Skip Cave' via Programming:
How to find the permutations of y objects taken x at a time?

     3 sper 4

0 1 2

0 1 3

0 2 1

0 2 3

0 3 1

0 3 2

1 0 2

1 0 3

1 2 0

1 2 3

1 3 0

1 3 2

2 0 1

2 0 3

2 1 0

2 1 3

2 3 0

2 3 1

3 0 1

3 0 2

3 1 0

3 1 2

3 2 0

3 2 1


     2 sper 4

0 1

0 2

0 3

1 0

1 2

1 3

2 0

2 1

2 3

3 0

3 1

3 2

     1 sper 4
0
1
2
3

Skip
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to