Reference Guide > M–N Routines > NCHSK Function
  

NCHSK Function
Computes binomial coefficients, often stated in words as "N choose K", which is the number of ways k elements can be drawn from a set of n elements, k < n, where the ordering of the k elements does not matter.
Usage
coeff = NCHSK(n,k)
Input Parameters
n—A scalar, non-negative integer defining the number of elements to choose from.
k—A scalar or array of non-negative integers defining the number of elements to choose from a set of n elements.
Returned Value
coeff—If k is scalar, coeff is an integer value specifying the number of ways k elements can be drawn from a set of n elements. If k is an array, then coeff is an array of integers where coeff(i) is the number of ways k(i) elements can be drawn from the set of n elements.
Keywords
Permutation—If set and nonzero, then the returned value, coeff, is the number of ways of choosing an ordered subset (the number of possible permutations) of k elements from a set of n elements.
Discussion
Notationally "N choose K", nCk, is:
where n! is "n factorial" and is equal to 1*2*....*(n-1)*n. Note that if the Permutation keyword is set, then the k! term in the denominator does not exist.
Examples
PM, NCHSK(4, [0,1,2,3,4], /Permutation)
; PV-WAVE prints:
;    1
;    4
;   12
;   24
;   24
This results in the following:
1   ; Number of ways of choosing nothing from 4 elements. 
4   ; Number of ways of choosing one element from four elements.
12  ; Number of ways of choosing two ordered elements from four
 ; elements.
24  ; Number of ways of choosing three ordered elements from four
 ; elements.
24  ; Number of ways of choosing four ordered elements from four
 ; elements.
It may seem odd that 24 is listed twice. However, to choose three items from a set of four items means that for the first choice there are four possibilities. Once a choice is made, for the second element chosen there are three remaining possibilities. Again a choice is made and there are now only 2 remaining possibilities for the last pick.
Thus, the number of possible combinations is 4*3*2 = 24. If we are to pick four elements from a set of four, then we have 4*3*2*1 = 24, since for the last choice there is only one possibility remaining, and thus the number of permutations of three elements drawn from four versus four from four is the same. This is true in general, i.e.:
WAVE> PM,NCHSK(9,8, /Permutation)
   362880
WAVE> PM,NCHSK(9,9, /Permutation)
   362880

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.