Friday, September 5, 2025
HomeLanguagesJavaJava Guava | binomial() method of IntMath Class

Java Guava | binomial() method of IntMath Class

The binomial(int n, int k) method of Guava’s IntMath class accepts two parameters n and k and calculate the value of the binomial coefficient {n}\choose{k}.

If the calculated value overflows the maximum value of an integer than the method returns Integer.MAX_VALUE or in other words the maximum value of an integer.

Syntax :

public static int binomial(int n, int k)

Parameter: This method accepts two parameters n and k and calculate the value of the binomial coefficient {n}\choose{k}.

Return Value : The method returns the binomial coefficient of n and k.

Exceptions : The method binomial(int n, int k) throws IllegalArgumentException if n < 0, k < 0 or k > n.

Below examples illustrate the binomial() method of IntMath class:

Example 1 :




// Java code to show implementation of
// binomial(int n, int k) method of Guava's
// IntMath class
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
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 IntMath class
        int ans = IntMath.binomial(n, k);
  
        System.out.println("Binomial Coefficient of " + n 
                              + " and " + k + " is : " + ans);
  
        int n1 = 15;
        int k1 = 9;
  
        // Using binomial(int n, int k) method of
        // Guava's IntMath class
        int ans1 = IntMath.binomial(n1, k1);
  
        System.out.println("Binomial Coefficient of " + n1 
                             + " and " + k1 + " is : " + ans1);
    }
}


Output:

Binomial Coefficient of 5 and 2 is : 10
Binomial Coefficient of 15 and 9 is : 5005

Example 2 :




// Java code to show implementation of
// binomial(int n, int k) method of Guava's
// IntMath class
import java.math.RoundingMode;
import com.google.common.math.IntMath;
  
class GFG {
  
    static int findBinomial(int n, int k)
    {
  
        try {
            // Using binomial(int n, int k)
            // method of Guava's IntMath class
            // This should throw "IllegalArgumentException"
            // as k < 0
            int ans = IntMath.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);
        }
    }
}


Output:

java.lang.IllegalArgumentException: k (7) > n (5)

Reference :
https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#binomial-int-int-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS