return BigInteger.valueOf(LongMath.binomial(n, k));
public void testBinomialNegative() { for (int n : NEGATIVE_INTEGER_CANDIDATES) { try { LongMath.binomial(n, 0); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } }
public void testBinomialOutside() { for (int n = 0; n <= 50; n++) { try { LongMath.binomial(n, -1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } try { LongMath.binomial(n, n + 1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } }
return BigInteger.valueOf(LongMath.binomial(n, k));
return BigInteger.valueOf(LongMath.binomial(n, k));
@GwtIncompatible // Slow public void testBinomial_exhaustiveNotOverflowing() { // Tests all of the inputs to LongMath.binomial that won't cause it to overflow, that weren't // tested in the previous method, for k >= 3. for (int k = 3; k < LongMath.biggestBinomials.length; k++) { for (int n = 70; n <= LongMath.biggestBinomials[k]; n++) { assertEquals(BigIntegerMath.binomial(n, k).longValue(), LongMath.binomial(n, k)); } } }
public void testBinomial() { for (int n = 0; n <= 70; n++) { for (int k = 0; k <= n; k++) { BigInteger expectedBig = BigIntegerMath.binomial(n, k); long expectedLong = fitsInLong(expectedBig) ? expectedBig.longValue() : Long.MAX_VALUE; assertEquals(expectedLong, LongMath.binomial(n, k)); } } }
/** * Computes an upper bound for the length of a splitting word. Based on * <p> * I.V. Kogan. "Estimated Length of a Minimal Simple Conditional Diagnostic Experiment". In: Automation and Remote * Control 34 (1973) * * @param n * the size of the automaton (number of states) * @param i * the number of states that should be distinguished by the current splitting word * @param m * the number of states that should originally be distinguished * * @return upper bound for the length of a splitting word */ public static long computeMaximumSplittingWordLength(final int n, final int i, final int m) { if (m == 2) { return n; } return LongMath.binomial(n, i) - LongMath.binomial(m - 1, i - 1) - 1; }
/** * Computes an upper bound for the length of a splitting word. Based on * <p> * I.V. Kogan. "Estimated Length of a Minimal Simple Conditional Diagnostic Experiment". In: Automation and Remote * Control 34 (1973) * * @param n * the size of the automaton (number of states) * @param i * the number of states that should be distinguished by the current splitting word * @param m * the number of states that should originally be distinguished * * @return upper bound for the length of a splitting word */ public static long computeMaximumSplittingWordLength(final int n, final int i, final int m) { if (m == 2) { return n; } return LongMath.binomial(n, i) - LongMath.binomial(m - 1, i - 1) - 1; }
@Override public long binomialCoefficient(int n, int k) { return LongMath.binomial(n, k); }
public static long kCombinationCount(int n, int k) { long result; if(k > n) { result = 0; } else { //result = LongMath.factorial(n) / LongMath.factorial(n - k); //The formula below should be equal to the one above long combinationCount = LongMath.binomial(n, k); long permutationCount = LongMath.factorial(k); result = combinationCount * permutationCount; } return result; }
/** * Returns {@code n} choose {@code k}, also known as the binomial coefficient of {@code n} and * {@code k}, that is, {@code n! / (k! (n - k)!)}. * * <p><b>Warning</b>: the result can take as much as <i>O(k log n)</i> space. * * @throws IllegalArgumentException if {@code n < 0}, {@code k < 0}, or {@code k > n} */ public static BigInteger binomial(int n, int k) { checkNonNegative("n", n); checkNonNegative("k", k); checkArgument(k <= n, "k (%s) > n (%s)", k, n); if (k > (n >> 1)) { k = n - k; } if (k < LongMath.BIGGEST_BINOMIALS.length && n <= LongMath.BIGGEST_BINOMIALS[k]) { return BigInteger.valueOf(LongMath.binomial(n, k)); } BigInteger result = BigInteger.ONE; for (int i = 0; i < k; i++) { result = result.multiply(BigInteger.valueOf(n - i)); result = result.divide(BigInteger.valueOf(i + 1)); } return result; }
public void testBinomialOutside() { for (int n = 0; n <= 50; n++) { try { LongMath.binomial(n, -1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) {} try { LongMath.binomial(n, n + 1); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) {} } }
@Benchmark int binomial(int reps) { int tmp = 0; for (int i = 0; i < reps; i++) { int j = i & ARRAY_MASK; tmp += LongMath.binomial(binomialArguments[j][0], binomialArguments[j][1]); } return tmp; }
public void testBinomialNegative() { for (int n : NEGATIVE_INTEGER_CANDIDATES) { try { LongMath.binomial(n, 0); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) {} } }
@GwtIncompatible // Slow public void testBinomial_exhaustiveNotOverflowing() { // Tests all of the inputs to LongMath.binomial that won't cause it to overflow, that weren't // tested in the previous method, for k >= 3. for (int k = 3; k < LongMath.biggestBinomials.length; k++) { for (int n = 70; n <= LongMath.biggestBinomials[k]; n++) { assertEquals(BigIntegerMath.binomial(n, k).longValue(), LongMath.binomial(n, k)); } } }
public void testBinomial() { for (int n = 0; n <= 70; n++) { for (int k = 0; k <= n; k++) { BigInteger expectedBig = BigIntegerMath.binomial(n, k); long expectedLong = fitsInLong(expectedBig) ? expectedBig.longValue() : Long.MAX_VALUE; assertEquals(expectedLong, LongMath.binomial(n, k)); } } }
long numberOfTerms = LongMath.binomial(n + k - 1, n); if (numberOfTerms > Integer.MAX_VALUE) { throw new ArithmeticException("");