Class BinomialCoefficient
java.lang.Object
org.apache.commons.numbers.combinatorics.BinomialCoefficient
Representation of the
binomial coefficient.
It is "
n choose k
", the number of k
-element subsets that
can be selected from an n
-element set.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
The maximum n that can be computed without overflow of a long for any m.private static final int
The maximum m that can be computed without overflow of a long.private static final int
The maximum n that can be computed without intermediate overflow for any m. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static int
checkBinomial
(int n, int k) Check binomial preconditions.static long
value
(int n, int k) Computes the binomial coefficient.
-
Field Details
-
MAX_M
private static final int MAX_MThe maximum m that can be computed without overflow of a long. C(68, 34) > 2^63.- See Also:
-
SMALL_N
private static final int SMALL_NThe maximum n that can be computed without intermediate overflow for any m. C(61, 30) * 30 invalid input: '<' 2^63.- See Also:
-
LIMIT_N
private static final int LIMIT_NThe maximum n that can be computed without overflow of a long for any m. C(66, 33) invalid input: '<' 2^63.- See Also:
-
-
Constructor Details
-
BinomialCoefficient
private BinomialCoefficient()Private constructor.
-
-
Method Details
-
value
public static long value(int n, int k) Computes the binomial coefficient.The largest value of
n
for which all coefficients can fit into along
is 66. Largern
may result in anArithmeticException
depending on the value ofk
.Any
min(k, n - k) >= 34
cannot fit into along
and will result in anArithmeticException
.- Parameters:
n
- Size of the set.k
- Size of the subsets to be counted.- Returns:
n choose k
.- Throws:
IllegalArgumentException
- ifn < 0
,k < 0
ork > n
.ArithmeticException
- if the result is too large to be represented by along
.
-
checkBinomial
static int checkBinomial(int n, int k) Check binomial preconditions.For convenience in implementations this returns the smaller of
k
orn - k
allowing symmetry to be exploited in computing the binomial coefficient.- Parameters:
n
- Size of the set.k
- Size of the subsets to be counted.- Returns:
- min(k, n - k)
- Throws:
IllegalArgumentException
- ifn < 0
.IllegalArgumentException
- ifk > n
ork < 0
.
-