The binomial(int n, int k) method of Guava’s LongMath Class accepts two parameters n and k and calculate the value of the binomial coefficient
. If the calculated value overflows the maximum value of a long, then the method returns Long.MAX_VALUE i.e, the maximum value of a long.
Syntax :
public static long binomial(int n, int k)
Parameters: This method accepts two parameters n and k and calculate the value of the binomial coefficient
.
Return Value : The method returns the binomial coefficient of n and k.
Exceptions : The method binomial(int n, int k) throws IllegalArgumentException if n is negative, or k is negative or k is greater than n.
Below examples illustrate the binomial() method of LongMath class:
Example 1 :
// Java code to show implementation of // binomial(int n, int k) method of Guava's // LongMath class import java.math.RoundingMode; import com.google.common.math.LongMath; class GFG { // Driver code public static void main(String args[]) { int n = 4; int k = 3; // Using binomial(int n, int k) method of // Guava's LongMath class long ans = LongMath.binomial(n, k); System.out.println("Binomial Coefficient of " + n + " and " + k + " is : " + ans); int n1 = 20; int k1 = 4; // Using binomial(int n, int k) method of // Guava's LongMath class long ans1 = LongMath.binomial(n1, k1); System.out.println("Binomial Coefficient of " + n1 + " and " + k1 + " is : " + ans1); } } |
Binomial Coefficient of 4 and 3 is : 4 Binomial Coefficient of 20 and 4 is : 4845
Example 2 :
// Java code to show implementation of // binomial(int n, int k) method of Guava's // LongMath class import java.math.RoundingMode; import com.google.common.math.LongMath; class GFG { static long findBinomial(int n, int k) { try { // Using binomial(int n, int k) // method of Guava's LongMath class // This should throw "IllegalArgumentException" // as k < 0 long ans = LongMath.binomial(n, k); // Return the answer return ans; } catch (Exception e) { System.out.println(e); return -1; } } // Driver code public static void main(String args[]) { int n = 5; int k = 7; try { // Function calling findBinomial(n, k); } catch (Exception e) { System.out.println(e); } } } |
java.lang.IllegalArgumentException: k (7) > n (5)
Reference : https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/LongMath.html#binomial-int-int-
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Guava | pow(long b, int k) of LongMath Class with Examples
- Java Guava | isPowerOfTwo(long x) of LongMath Class with Examples
- Java Guava | log2(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | sqrt(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | mod(long x, long m) of LongMath Class with Examples
- Java Guava | mean(long x, long y) of LongMath Class with Examples
- Java Guava | gcd(long a, long b) of LongMath Class with Examples
- LongMath Class | Guava | Java
- Java Guava | LongMath.checkedMultiply(int a, int b) method with Examples
- Java Guava | LongMath.checkedPow(long b, int k) method with Examples
- Java Guava | LongMath log10(long x, RoundingMode mode) method with Examples
- Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
- Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
- Java Guava | factorial(int n) method of IntMath Class with Examples
- Java Guava | compare() method of Short Class with Examples
- Ordering Class | Guava | Java
- Booleans Class | Guava | Java
- Doubles Class | Guava | Java
- Shorts Class | Guava | Java
- Bytes Class | 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.

