user
This programming task, is to calculate ANY binomial coefficient.

However, it has to be able to output   

(

5
3

)
{\displaystyle {\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 {\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 {\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 {\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 {\displaystyle {\binom {n+k-1}{k}}=^{n+k-1}\operatorname {C} \_{k}={(n+k-1)! \over (n-1)!k!}}} | n  k {\displaystyle {\displaystyle n^{k}}} |
                | Task: Combinations with repetitions | Task: Permutations with repetitions |
assistant
(formerly Perl 6)
For a start, you can get the length of the corresponding list of combinations:

```
say combinations(5, 3).elems;

```

Output:

```
10
```

This method is efficient, as Raku will not actually compute each element of the list, since it actually uses an iterator with a defined count-only method. Such method performs computations in a way similar to the following infix operator:

```
sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. $^p }
say 5 choose 3;
```

A possible optimization would use a symmetry property of the binomial coefficient:

```
sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p) }
```

One drawback of this method is that it returns a Rat, not an Int. So we actually may want to enforce the conversion:

```
sub infix:<choose> { ([*] ($^n ... 0) Z/ 1 .. min($n - $^p, $p)).Int }
```

And *this* is exactly what the count-only method does.