org.apache.commons.math3.util

## Class CombinatoricsUtils

• public final class CombinatoricsUtils
extends Object
Combinatorial utilities.
Since:
3.3
Version:
$Id: CombinatoricsUtils.html 908881 2014-05-15 07:10:28Z luc$
• ### Method Detail

• #### factorial

public static long factorial(int n)
throws NotPositiveException,
MathArithmeticException
Returns n!. Shorthand for n Factorial, the product of the numbers 1,...,n.

Preconditions:

• n >= 0 (otherwise IllegalArgumentException is thrown)
• The result is small enough to fit into a long. The largest value of n for which n! < Long.MAX_VALUE} is 20. If the computed value exceeds Long.MAX_VALUE an ArithMeticException is thrown.

Parameters:
n - argument
Returns:
n!
Throws:
MathArithmeticException - if the result is too large to be represented by a long.
NotPositiveException - if n < 0.
MathArithmeticException - if n > 20: The factorial value is too large to fit in a long.
• #### factorialDouble

public static double factorialDouble(int n)
throws NotPositiveException
Compute n!, the factorial of n (the product of the numbers 1 to n), as a double. The result should be small enough to fit into a double: The largest n for which n! < Double.MAX_VALUE is 170. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned.
Parameters:
n - Argument.
Returns:
n!
Throws:
NotPositiveException - if n < 0.
• #### combinationsIterator

public static Iterator<int[]> combinationsIterator(int n,
int k)
Returns an iterator whose range is the k-element subsets of {0, ..., n - 1} represented as int[] arrays.

The arrays returned by the iterator are sorted in descending order and they are visited in lexicographic order with significance from right to left. For example, combinationsIterator(4, 2) returns an Iterator that will generate the following sequence of arrays on successive calls to next():
[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]

If k == 0 an Iterator containing an empty array is returned and if k == n an Iterator containing [0, ..., n -1] is returned.
Parameters:
n - Size of the set from which subsets are selected.
k - Size of the subsets to be enumerated.
Returns:
an iterator over the k-sets in n.
Throws:
NotPositiveException - if n < 0.
NumberIsTooLargeException - if k > n.