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)!)
Syntax:
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 BifIntegerMath.binomial() method:
Example 1:
// Java code to show implementation of // binomial(int n, int k) method // of Guava's BigIntegerMath class import java.math.*; import com.google.common.math.BigIntegerMath; class GFG { // Driver code public static void main(String args[]) { int n = 5; int k = 2; // Using binomial(int n, int k) method of // Guava's BigIntegerMath class BigInteger ans = BigIntegerMath.binomial(n, k); System.out.println("Binomial Coefficient of " + n + " & " + k + " is: " + ans); int n1 = 15; int k1 = 9; // Using binomial(int n, int k) method of // Guava's BigIntegerMath class BigInteger ans1 = BigIntegerMath.binomial(n1, k1); System.out.println("Binomial Coefficient of " + n1 + " & " + k1 + " is: " + ans1); } } |
Binomial Coefficient of 5 & 2 is: 10 Binomial Coefficient of 15 & 9 is: 5005
Example 2:
// Java code to show implementation of // binomial(int n, int k) method // of Guava's BigIntegerMath class import java.math.*; import com.google.common.math.BigIntegerMath; class GFG { // Driver code public static void main(String args[]) { try { int n = 5; int k = 7; // Using binomial(int n, int k) method of // Guava's BigIntegerMath class // This should raise "IllegalArgumentException" // as k > n BigInteger ans = BigIntegerMath.binomial(n, k); System.out.println("Binomial Coefficient of" + n + " & " + k + " is: " + ans); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.IllegalArgumentException: k (7) > n (5)
Recommended Posts:
- BigIntegerMath factorial() function | Guava | Java
- BigIntegerMath divide() function | Guava | Java
- BigIntegerMath log10() function | Guava | Java
- BigIntegerMath log2() function | Guava | Java
- BigIntegerMath isPowerOfTwo() function | Guava | Java
- BigIntegerMath floorPowerOfTwo() function | Guava | Java
- BigIntegerMath ceilingPowerOfTwo() function | Guava | Java
- BigIntegerMath sqrt() function | Guava | Java
- BigIntegerMath Class | Guava | Java
- Java Guava | binomial(int n, int k) of LongMath Class with Examples
- Java Guava | binomial() method of IntMath Class
- Ints max() function | Guava | Java
- Ints min() function | Guava | Java
- Ints contains() function | Guava | Java
- Ints concat() function | Guava | Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



