// permutations of !n: / pierre (k4): k)p:{(,/@\:)/,\(,<<:)'x=\:x:!x} / roger? (from http://www.langreiter.com/space/k-notes) p:{:[1:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]} // combinations of k taken from n values / herr doktor professor bierly: c:{,/x{n,''(+\#:'x)#\:,/1+x}/n:|!y-x-:1} c:{a@\:&&/>':a:y _vs!*/x#y} / arthur: c:{(,!0){,/(|!#y),''y#\:1+x}/x+\\(y-x-:1)#1} / integer partition part:{[v] if[0>i:-1+(v>1)?0;:v] / index of smallest part>1 k:-1+v i / that part-1 p:i#v / prefix s:+/i _ v / sum of suffix if[~r:s!k;r:!0] / remainder if not zero d:(_ s%k)#k / distribute components p,d,r}\,: / all / e.g. part 5 / partitions of n of length k fixpart:{[n;k]{[n;v] if[(#v)=i:(v<-1+*v)?1;:v] v[1+!i]:1+v i v[0]:n-+/1_ v v}[n]\{x,(n-x)#1}1+n-k} / e.g. fixpart[10;4] take:{[m;v]v,\:&m-#*v} drop:{[n;v]v@&&/'v0} fixparts:{[k;n;m],/(fill drop[n]take[m]fixpart[k]@)'1+!m} \ p 3 (0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0) c[2;5] (0 1 0 2 0 3 0 4 1 2 1 3 1 4 2 3 2 4 3 4)