1 /*
2 * Copyright (C) 2012-2015 Christian Sterzl <christian.sterzl@gmail.com>
3 *
4 * This file is part of Bittwiddling.
5 *
6 * Bittwiddling is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Bittwiddling is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Bittwiddling. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.vcollaborate.math;
21
22 import com.google.common.math.BigIntegerMath;
23
24 public final class CombinatoricUtils {
25
26 private CombinatoricUtils() {
27 }
28
29 /**
30 * Calculates the possible combinations of chosen types (r) from a list of types (n) or n over r.
31 * <p>
32 * Mathematical it is following formula: <br>
33 * \(\binom{n}{r} = \frac{n!}{r!(n-r)!}\)
34 */
35 public static final long combinations(final int n, final int r) {
36 return BigIntegerMath.factorial(n)
37 .divide(BigIntegerMath.factorial(r).multiply(BigIntegerMath.factorial(n - r)))
38 .longValue();
39 }
40 }