Java Guava | binomial() method of IntMath Class Last Updated : 23 Jan, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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 // 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- Comment More infoAdvertise with us Next Article Java Guava | binomial() method of IntMath Class B bansal_rtk_ Follow Improve Article Tags : Misc Java Java-Functions java-guava Practice Tags : JavaMisc Similar Reads Java Guava | mod() method of IntMath Class The mod(int x, int m) method of Guava's IntMath class accepts two parameters x and m and used to calculate the value of x modulus under m. Syntax : public static int mod(int x, int m) Parameters: This method accepts two parameters x and m which are of integer types and calculate x modulo m. Return V 2 min read Java Guava | ceilingPowerOfTwo() method of IntMath Class The ceilingPowerOfTwo(int x) method of Guava's IntMath class accepts a parameter and calculates the smallest power of two greater than the values passed in the parameter. This method is equivalent to checkedPow(2, log2(x, CEILING)). Syntax : public static int ceilingPowerOfTwo(int x) Parameter: This 2 min read Java Guava | isPrime() method of IntMath Class The isPrime(int n) method of Guava's IntMath class is used to check whether the parameter passed to it is a prime number or not. If the parameter passed to it is prime, then it returns True otherwise it returns False. A number is said to be Prime if it is divisible only by 1 and the number itself. S 2 min read Java Guava | pow(int b, int k) method of IntMath Class The method pow(int b, int k) of Guava's IntMath class returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).intValue(). This implementation runs in O(log k) time. Syntax: public static int pow(int b, int k) Exception: The method pow(int b, int k) 2 min read Java Guava | gcd(int a, int b) method of IntMath Class The method gcd(int a, int b) of Guava's IntMath class returns the greatest common divisor of a, b. Syntax : public static int gcd(int a, int b) Where a and b are integers. Return Value : Greatest common divisor of integers a and b. Exceptions : The method gcd(int a, int b) throws IllegalArgumentExce 2 min read Like