Task

Given non-negative integers    m    and    n,   generate all size    m    combinations   of the integers from    0   (zero)   to    n-1    in sorted order   (each combination is sorted and the entire table is sorted).



Example

3   comb    5      is:

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

If it is more "natural" in your language to start counting from    1   (unity) instead of    0   (zero),

the combinations can be of the integers from    1   to    n. 



See also


 The number of samples of size k from n objects.


With   combinations and permutations   generation tasks. 






 Order Unimportant

 Order Important


 Without replacement








(


n
k


)




=

n



C

k


=



n
(
n
−
1
)
…
(
n
−
k
+
1
)


k
(
k
−
1
)
…
1





{\displaystyle {\binom {n}{k}}=^{n}\operatorname {C} _{k}={\frac {n(n-1)\ldots (n-k+1)}{k(k-1)\dots 1}}}










n



P

k


=
n
⋅
(
n
−
1
)
⋅
(
n
−
2
)
⋯
(
n
−
k
+
1
)


{\displaystyle ^{n}\operatorname {P} _{k}=n\cdot (n-1)\cdot (n-2)\cdots (n-k+1)}




Task: Combinations

Task: Permutations


 With replacement








(



n
+
k
−
1

k


)




=

n
+
k
−
1



C

k


=



(
n
+
k
−
1
)
!


(
n
−
1
)
!
k
!





{\displaystyle {\binom {n+k-1}{k}}=^{n+k-1}\operatorname {C} _{k}={(n+k-1)! \over (n-1)!k!}}








n

k




{\displaystyle n^{k}}




Task: Combinations with repetitions

Task: Permutations with repetitions



*        Combinations              26/05/2016
COMBINE  CSECT
         USING  COMBINE,R13        base register
         B      72(R15)            skip savearea
         DC     17F'0'             savearea
         STM    R14,R12,12(R13)    prolog
         ST     R13,4(R15)         "
         ST     R15,8(R13)         "
         LR     R13,R15            "
         SR     R3,R3              clear
         LA     R7,C               @c(1)
         LH     R8,N               v=n
LOOPI1   STC    R8,0(R7)           do i=1 to n; c(i)=n-i+1
         LA     R7,1(R7)             @c(i)++
         BCT    R8,LOOPI1          next i
LOOPBIG  LA     R10,PG             big loop {------------------
         LH     R1,N               n
         LA     R7,C-1(R1)         @c(i)
         LH     R6,N               i=n
LOOPI2   IC     R3,0(R7)           do i=n to 1 by -1; r2=c(i)
         XDECO  R3,PG+80             edit c(i)
         MVC    0(2,R10),PG+90       output c(i)
         LA     R10,3(R10)           @pgi=@pgi+3
         BCTR   R7,0                 @c(i)--
         BCT    R6,LOOPI2          next i
         XPRNT  PG,80              print buffer
         LA     R7,C               @c(1)
         LH     R8,M               v=m
         LA     R6,1               i=1
LOOPI3   LR     R1,R6              do i=1 by 1; r1=i
         IC     R3,0(R7)             c(i)
         CR     R3,R8                while c(i)>=m-i+1 
         BL     ELOOPI3              leave i
         CH     R6,N                 if i>=n
         BNL    ELOOPBIG             exit loop
         BCTR   R8,0                 v=v-1
         LA     R7,1(R7)             @c(i)++
         LA     R6,1(R6)             i=i+1
         B      LOOPI3             next i
ELOOPI3  LR     R1,R6              i
         LA     R4,C-1(R1)         @c(i)
         IC     R3,0(R4)           c(i)
         LA     R3,1(R3)           c(i)+1
         STC    R3,0(R4)           c(i)=c(i)+1
         BCTR   R7,0               @c(i)--
LOOPI4   CH     R6,=H'2'           do i=i to 2 by -1
         BL     ELOOPI4            leave i
         IC     R3,1(R7)             c(i)
         LA     R3,1(R3)             c(i)+1
         STC    R3,0(R7)             c(i-1)=c(i)+1
         BCTR   R7,0                 @c(i)--
         BCTR   R6,0                 i=i-1
         B      LOOPI4             next i
ELOOPI4  B      LOOPBIG            big loop }------------------
ELOOPBIG L      R13,4(0,R13)       epilog 
         LM     R14,R12,12(R13)    "
         XR     R15,R15            "
         BR     R14                exit
M        DC     H'5'               <=input 
N        DC     H'3'               <=input 
C        DS     64X                array of 8 bit integers
PG       DC     CL92' '            buffer        
         YREGS
         END    COMBINE