BigIntegerMath binomial() function | Guava | Java
The binomial(int n, int k) method of Guava’s BigIntegerMath class returns n choose k, also known as the binomial coefficient of n and k, that is,
n! / (k! (n - k)!)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public static BigInteger binomial(int n, int k)
Parameters: This method takes the following parameters:
- n: The base for binomial expansion.
- k: The power for binomial expansion.
Return Value: This method returns the binomial coefficient of n and k.
Exceptions: This method throws IllegalArgumentException if n < 0, k < 0 or k > n.
Note: The result can take as much as O(k log n) space.
Below examples illustrates the BigIntegerMath.binomial() method:
Binomial Coefficient of 5 & 2 is: 10 Binomial Coefficient of 15 & 9 is: 5005
Exception: java.lang.IllegalArgumentException: k (7) > n (5)