Combination
In mathematics, a combination is a selection of items from a set that has distinct members, such that the order of selection does not matter (unlike permutations). For example, given three fruits, say an apple, an orange and a pear, there are three combinations of two that can be drawn from this set: an apple and a pear; an apple and an orange; or a pear and an orange. More formally, a k-combination of a set S is a subset of k distinct elements of S. So, two combinations are identical if and only if each combination has the same members. (The arrangement of the members in each set does not matter.) If the set has n elements, the number of k-combinations, denoted by 
  
    
      
        C
        (
        n
        ,
        k
        )
      
    
    {\displaystyle C(n,k)}
   or 
  
    
      
        
          C
          
            k
          
          
            n
          
        
      
    
    {\displaystyle C_{k}^{n}}
  , is equal to the binomial coefficient

which can be written using factorials as 
  
    
      
        
          
            
              
                n
                !
              
              
                k
                !
                (
                n
                −
                k
                )
                !
              
            
          
        
      
    
    {\displaystyle \textstyle {\frac {n!}{k!(n-k)!}}}
   whenever 
  
    
      
        k
        ≤
        n
      
    
    {\displaystyle k\leq n}
  , and which is zero when 
  
    
      
        k
        >
        n
      
    
    {\displaystyle k>n}
  . This formula can be derived from the fact that each k-combination of a set S of n members has 
  
    
      
        k
        !
      
    
    {\displaystyle k!}
   permutations so 
  
    
      
        
          P
          
            k
          
          
            n
          
        
        =
        
          C
          
            k
          
          
            n
          
        
        ×
        k
        !
      
    
    {\displaystyle P_{k}^{n}=C_{k}^{n}\times k!}
   or 
  
    
      
        
          C
          
            k
          
          
            n
          
        
        =
        
          P
          
            k
          
          
            n
          
        
        
          /
        
        k
        !
      
    
    {\displaystyle C_{k}^{n}=P_{k}^{n}/k!}
  . The set of all k-combinations of a set S is often denoted by 
  
    
      
        
          
            
              
                (
              
              
                S
                k
              
              
                )
              
            
          
        
      
    
    {\displaystyle \textstyle {\binom {S}{k}}}
  .
A combination is a combination of n things taken k at a time without repetition. To refer to combinations in which repetition is allowed, the terms k-combination with repetition, k-multiset, or k-selection, are often used. If, in the above example, it were possible to have two of any one kind of fruit there would be 3 more 2-selections: one with two apples, one with two oranges, and one with two pears.
Although the set of three fruits was small enough to write a complete list of combinations, this becomes impractical as the size of the set increases. For example, a poker hand can be described as a 5-combination (k = 5) of cards from a 52 card deck (n = 52). The 5 cards of the hand are all distinct, and the order of cards in the hand does not matter. There are 2,598,960 such combinations, and the chance of drawing any one hand at random is 1 / 2,598,960.

Number of k-combinations
The number of k-combinations from a given set S of n elements is often denoted in elementary combinatorics texts by 
  
    
      
        C
        (
        n
        ,
        k
        )
      
    
    {\displaystyle C(n,k)}
  , or by a variation such as 
  
    
      
        
          C
          
            k
          
          
            n
          
        
      
    
    {\displaystyle C_{k}^{n}}
  ,  
  
    
      
        
          

          
          
            n
          
        
        
          C
          
            k
          
        
      
    
    {\displaystyle {}_{n}C_{k}}
  , 
  
    
      
        
          

          
          
            n
          
        
        
          C
          
            k
          
        
      
    
    {\displaystyle {}^{n}C_{k}}
  , 
  
    
      
        
          C
          
            n
            ,
            k
          
        
      
    
    {\displaystyle C_{n,k}}
   or even 
  
    
      
        
          C
          
            n
          
          
            k
          
        
      
    
    {\displaystyle C_{n}^{k}}
   (the last form is standard in French, Romanian, Russian, Chinese and Polish texts).  The same number however occurs in many other mathematical contexts, where it is denoted by 
  
    
      
        
          
            
              
                (
              
              
                n
                k
              
              
                )
              
            
          
        
      
    
    {\displaystyle {\tbinom {n}{k}}}
   (often read as "n choose k"); notably it occurs as a coefficient in the binomial formula, hence its name binomial coefficient. One can define 
  
    
      
        
          
            
              
                (
              
              
                n
                k
              
              
                )
              
            
          
        
      
    
    {\displaystyle {\tbinom {n}{k}}}
   for all natural numbers k at once by the relation

from which it is clear that

and further,

for k > n.
To see that these coefficients count k-combinations from S, one can first consider a collection of n distinct variables Xs labeled by the elements s of S, and expand the product over all elements of S:

it has 2n distinct terms corresponding to all the subsets of S, each subset giving the product of the corresponding variables Xs. Now setting all of the Xs equal to the unlabeled variable X, so that the product becomes (1 + X)n, the term for each k-combination from S becomes Xk, so that the coefficient of that power in the result equals the number of such k-combinations.
Binomial coefficients can be computed explicitly in various ways. To get all of them for the expansions up to (1 + X)n, one can use (in addition to the basic cases already given) the recursion relation

for 0 < k < n, which follows from (1 + X)n = (1 + X)n − 1(1 + X); this leads to the construction of Pascal's triangle.
For determining an individual binomial coefficient, it is more practical to use the formula

The numerator gives the number of k-permutations of n, i.e., of sequences of k distinct elements of S, while the denominator gives the number of such k-permutations that give the same k-combination when the order is ignored.
When k exceeds n/2, the above formula contains factors common to the numerator and the denominator, and canceling them out gives the relation

for 0 ≤ k ≤ n. This expresses a symmetry that is evident from the binomial formula, and can also be understood in terms of k-combinations by taking the complement of such a combination, which is an (n − k)-combination.
Finally there is a formula which exhibits this symmetry directly, and has the merit of being easy to remember:

where n! denotes the factorial of n. It is obtained from the previous formula by multiplying denominator and numerator by (n − k)!, so it is certainly computationally less efficient than that formula.
The last formula can be understood directly, by considering the n! permutations of all the elements of S. Each such permutation gives a k-combination by selecting its first k elements. There are many duplicate selections: any combined permutation of the first k elements among each other, and of the final (n − k) elements among each other produces the same combination; this explains the division in the formula.
From the above formulas follow relations between adjacent numbers in Pascal's triangle in all three directions:

Together with the basic cases 
  
    
      
        
          
            
              
                (
              
              
                n
                0
              
              
                )
              
            
          
        
        =
        1
        =
        
          
            
              
                (
              
              
                n
                n
              
              
                )
              
            
          
        
      
    
    {\displaystyle {\tbinom {n}{0}}=1={\tbinom {n}{n}}}
  , these allow successive computation of respectively all numbers of combinations from the same set (a row in Pascal's triangle), of k-combinations of sets of growing sizes, and of combinations with a complement of fixed size n − k.

Example of counting combinations
As a specific example, one can compute the number of five-card hands possible from a standard fifty-two card deck as:
Alternatively one may use the formula in terms of factorials and cancel the factors in the numerator against parts of the factors in the denominator, after which only multiplication of the remaining factors is required:

Another alternative computation, equivalent to the first, is based on writing

which gives

When evaluated in the following order, 52 ÷ 1 × 51 ÷ 2 × 50 ÷ 3 × 49 ÷ 4 × 48 ÷ 5, this can be computed using only integer arithmetic. The reason is that when each division occurs, the intermediate result that is produced is itself a binomial coefficient, so no remainders ever occur.
Using the symmetric formula in terms of factorials without performing simplifications gives a rather extensive calculation:

Enumerating k-combinations
One can enumerate all k-combinations of a given set S of n elements in some fixed order, which establishes a bijection from an interval of 
  
    
      
        
          
            
              
                (
              
              
                n
                k
              
              
                )
              
            
          
        
      
    
    {\displaystyle {\tbinom {n}{k}}}
   integers with the set of those k-combinations. Assuming S is itself ordered, for instance S = { 1, 2, ..., n }, there are two natural possibilities for ordering its k-combinations: by comparing their smallest elements first (as in the illustrations above) or by comparing their largest elements first. The latter option has the advantage that adding a new largest element to S will not change the initial part of the enumeration, but just add the new k-combinations of the larger set after the previous ones. Repeating this process, the enumeration can be extended indefinitely with k-combinations of ever larger sets. If moreover the intervals of the integers are taken to start at 0, then the k-combination at a given place i in the enumeration can be computed easily from i, and the bijection so obtained is known as the combinatorial number system. It is also known as "rank"/"ranking" and "unranking" in computational mathematics.There are many ways to enumerate k combinations. One way is to visit all the binary numbers less than 2n. Choose those numbers having k nonzero bits, although this is very inefficient even for small n (e.g. n = 20 would require visiting about one million numbers while the maximum number of allowed k combinations is about 186 thousand for k = 10). The positions of these 1 bits in such a number is a specific k-combination of the set { 1, ..., n }. Another simple, faster way is to track k index numbers of the elements selected, starting with {0 .. k−1} (zero-based) or {1 .. k} (one-based) as the first allowed k-combination and then repeatedly moving to the next allowed k-combination by incrementing the last index number if it is lower than n-1 (zero-based) or n (one-based) or the last index number x that is less than the index number following it minus one if such an index exists and resetting the index numbers after x to {x+1, x+2, ...}.

Number of combinations with repetition
A k-combination with repetitions, or k-multicombination, or multisubset of size k from a set S of size n is given by a set of k not necessarily distinct elements of S, where order is not taken into account: two sequences define the same multiset if one can be obtained from the other by permuting the terms. In other words, it is a sample of k elements from a set of n elements allowing for duplicates (i.e., with replacement) but disregarding different orderings (e.g. {2,1,2} = {1,2,2}). Associate an index to each element of S and think of the elements of S as types of objects, then we can let 
  
    
      
        
          x
          
            i
          
        
      
    
    {\displaystyle x_{i}}
   denote the number of elements of type i in a multisubset. The number of multisubsets of size k is then the number of nonnegative integer (so allowing zero) solutions of the Diophantine equation:
If S has n elements, the number of such k-multisubsets is denoted by

a notation that is analogous to the binomial coefficient which counts k-subsets. This expression, n multichoose k, can also be given in terms of binomial coefficients:

This relationship can be easily proved using a representation known as stars and bars.

As with binomial coefficients, there are several relationships between these multichoose expressions. For example, for 
  
    
      
        n
        ≥
        1
        ,
        k
        ≥
        0
      
    
    {\displaystyle n\geq 1,k\geq 0}
  ,

This identity follows from interchanging the stars and bars in the above representation.

Example of counting multisubsets
For example, if you have four types of donuts (n = 4) on a menu to choose from and you want three donuts (k = 3), the number of ways to choose the donuts with repetition can be calculated as

This result can be verified by listing all the 3-multisubsets of the set S = {1,2,3,4}. This is displayed in the following table. The second column lists the donuts you actually chose, the third column shows the nonnegative integer solutions 
  
    
      
        [
        
          x
          
            1
          
        
        ,
        
          x
          
            2
          
        
        ,
        
          x
          
            3
          
        
        ,
        
          x
          
            4
          
        
        ]
      
    
    {\displaystyle [x_{1},x_{2},x_{3},x_{4}]}
   of the equation 
  
    
      
        
          x
          
            1
          
        
        +
        
          x
          
            2
          
        
        +
        
          x
          
            3
          
        
        +
        
          x
          
            4
          
        
        =
        3
      
    
    {\displaystyle x_{1}+x_{2}+x_{3}+x_{4}=3}
   and the last column gives the stars and bars representation of the solutions.

Number of k-combinations for all k
The number of k-combinations for all k is the number of subsets of a set of n elements. There are several ways to see that this number is 2n. In terms of combinations, 
  
    
      
        
          ∑
          
            0
            ≤
            
              k
            
            ≤
            
              n
            
          
        
        
          
            
              (
            
            
              n
              k
            
            
              )
            
          
        
        =
        
          2
          
            n
          
        
      
    
    {\textstyle \sum _{0\leq {k}\leq {n}}{\binom {n}{k}}=2^{n}}
  , which is the sum of the nth row (counting from 0) of the binomial coefficients in Pascal's triangle. These combinations (subsets) are enumerated by the 1 digits of the set of base 2 numbers counting from 0 to 2n − 1, where each digit position is an item from the set of n.
Given 3 cards numbered 1 to 3, there are 8 distinct combinations (subsets), including the empty set:

Representing these subsets (in the same order) as base 2 numerals:

0 – 000
1 – 001
2 – 010
3 – 011
4 – 100
5 – 101
6 – 110
7 – 111

Probability: sampling a random combination
There are various algorithms to pick out a random combination from a given set or list. Rejection sampling is extremely slow for large sample sizes. One way to select a k-combination efficiently from a population of size n is to iterate across each element of the population, and at each step pick that element with a dynamically changing probability of 
  
    
      
        
          
            
              k
              −
              #
              
                samples chosen
              
            
            
              n
              −
              #
              
                samples visited
              
            
          
        
      
    
    {\textstyle {\frac {k-\#{\text{samples chosen}}}{n-\#{\text{samples visited}}}}}
   (see Reservoir sampling). Another is to pick a random non-negative integer less than 
  
    
      
        
          
            
              
                (
              
              
                n
                k
              
              
                )
              
            
          
        
      
    
    {\displaystyle \textstyle {\binom {n}{k}}}
   and convert it into a combination using the combinatorial number system.

Number of ways to put objects into bins
A combination can also be thought of as a selection of two sets of items: those that go into the chosen bin and those that go into the unchosen bin. This can be generalized to any number of bins with the constraint that every item must go to exactly one bin. The number of ways to put objects into bins is given by the multinomial coefficient

where n is the number of items, m is the number of bins, and 
  
    
      
        
          k
          
            i
          
        
      
    
    {\displaystyle k_{i}}
   is the number of items that go into bin i.
One way to see why this equation holds is to first number the objects arbitrarily from 1 to n and put the objects with numbers 
  
    
      
        1
        ,
        2
        ,
        …
        ,
        
          k
          
            1
          
        
      
    
    {\displaystyle 1,2,\ldots ,k_{1}}
   into the first bin in order, the objects with numbers 
  
    
      
        
          k
          
            1
          
        
        +
        1
        ,
        
          k
          
            1
          
        
        +
        2
        ,
        …
        ,
        
          k
          
            2
          
        
      
    
    {\displaystyle k_{1}+1,k_{1}+2,\ldots ,k_{2}}
   into the second bin in order, and so on. There are 
  
    
      
        n
        !
      
    
    {\displaystyle n!}
   distinct numberings, but many of them are equivalent, because only the set of items in a bin matters, not their order in it. Every combined permutation of each bins' contents produces an equivalent way of putting items into bins. As a result, every equivalence class consists of 
  
    
      
        
          k
          
            1
          
        
        !
        
        
          k
          
            2
          
        
        !
        ⋯
        
          k
          
            m
          
        
        !
      
    
    {\displaystyle k_{1}!\,k_{2}!\cdots k_{m}!}
   distinct numberings, and the number of equivalence classes is 
  
    
      
        
          
            
              
                n
                !
              
              
                
                  k
                  
                    1
                  
                
                !
                
                
                  k
                  
                    2
                  
                
                !
                ⋯
                
                  k
                  
                    m
                  
                
                !
              
            
          
        
      
    
    {\displaystyle \textstyle {\frac {n!}{k_{1}!\,k_{2}!\cdots k_{m}!}}}
  .
The binomial coefficient is the special case where k items go into the chosen bin and the remaining 
  
    
      
        n
        −
        k
      
    
    {\displaystyle n-k}
   items go into the unchosen bin:

See also
Notes
References
Benjamin, Arthur T.; Quinn, Jennifer J. (2003), Proofs that Really Count: The Art of Combinatorial Proof, The Dolciani Mathematical Expositions 27, The Mathematical Association of America, ISBN 978-0-88385-333-7
Brualdi, Richard A. (2010), Introductory Combinatorics (5th ed.), Pearson Prentice Hall, ISBN 978-0-13-602040-0
Erwin Kreyszig, Advanced Engineering Mathematics, John Wiley & Sons, INC, 1999.
Mazur, David R. (2010), Combinatorics: A Guided Tour, Mathematical Association of America, ISBN 978-0-88385-762-5
Ryser, Herbert John (1963), Combinatorial Mathematics, The Carus Mathematical Monographs 14, Mathematical Association of America
Uspensky, James (1937), Introduction to Mathematical Probability, McGraw-Hill

External links
Topcoder tutorial on combinatorics  
C code to generate all combinations of n elements chosen as k
Many Common types of permutation and combination math problems, with detailed solutions
The Unknown Formula For combinations when choices can be repeated and order does not matter
Combinations with repetitions (by: Akshatha AG and Smitha B)
The dice roll with a given sum problem An application of the combinations with repetition to rolling multiple dice