This programming task, is to calculate ANY binomial coefficient.

However, it has to be able to output   






(


5
3


)





{\displaystyle {\binom {5}{3}}}

,   which is   10.

This formula is recommended:


 






(


n
k


)



=



n
!


(
n
−
k
)
!
k
!



=



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


k
(
k
−
1
)
(
k
−
2
)
…
1





{\displaystyle {\binom {n}{k}}={\frac {n!}{(n-k)!k!}}={\frac {n(n-1)(n-2)\ldots (n-k+1)}{k(k-1)(k-2)\ldots 1}}}





See Also:

 Combinations and permutations
 Pascal's triangle

 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



// Number of combinations nCr
00 0E  Go:    ENT  R0   // n
01 1E         ENT  R1   // r
02 2C         CLR  R2
03 2A  Loop:  ADD1 R2
04 0D         DEC  R0
05 1D         DEC  R1
06 C3         JNZ  Loop
07 3C         CLR  R3   // for result
08 3A         ADD1 R3
09 0A  Next:  ADD1 R0
0A 1A         ADD1 R1
0B 50         R5 = R0
0C 5D         DEC  R5
0D 63         R6 = R3
0E 46  Mult:  R4 = R6
0F 3A  Add:   ADD1 R3
10 4D         DEC  R4
11 CF         JNZ  Add
12 5D         DEC  R5
13 CE         JNZ  Mult
14 61  Divide:R6 = R1
15 5A         ADD1 R5
16 3D  Sub:   DEC  R3
17 9B         JZ   Exact
18 6D         DEC  R6
19 D6         JNZ  Sub
1A 94         JZ   Divide
1B 35  Exact: R3 = R5
1C 2D         DEC  R2
1D C9         JNZ  Next
1E 03         R0 = R3
1F 80         JZ   Go   // Display result