BigIntegerMath binomial() function | Guava | Java Last Updated : 03 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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 BigIntegerMath.binomial() method: Example 1: Java // 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); } } Output: Binomial Coefficient of 5 & 2 is: 10 Binomial Coefficient of 15 & 9 is: 5005 Example 2: Java // 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); } } } Output: Exception: java.lang.IllegalArgumentException: k (7) > n (5) Reference: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#binomial-int-int- Comment More infoAdvertise with us Next Article BigIntegerMath binomial() function | Guava | Java S Sahil_Bansall Follow Improve Article Tags : Java java-guava Java-BigInteger Practice Tags : JavaJava-BigInteger Similar Reads BigIntegerMath divide() function | Guava | Java The method divide(BigInteger p, BigInteger q, RoundingMode mode) of Guava's BigIntegerMath class returns the result of dividing p by q, rounding using the specified RoundingMode. Syntax: public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) Parameters: This method takes the 3 min read BigIntegerMath sqrt() function | Guava | Java The method sqrt(BigInteger x, RoundingMode mode) of Guava's BigIntegerMath class returns the square root of x, rounded with the specified rounding mode. Syntax: public static BigInteger sqrt(BigInteger x, RoundingMode mode) Parameters: This method takes the following parameters: x : The BigInteger n 3 min read BigIntegerMath log2() function | Guava | Java The method log2(BigInteger x, RoundingMode mode) of Guava's BigIntegerMath class returns the base-2 logarithm of x, rounded according to the specified rounding mode. Syntax: public static int log2(BigInteger x, RoundingMode mode) Parameters: This method takes the following parameters: x: The BigInte 3 min read BigIntegerMath log10() function | Guava | Java The method log10(BigInteger x, RoundingMode mode) of Guava's BigIntegerMath class returns the base-10 logarithm of x, rounded according to the specified rounding mode. Syntax: public static int log10(BigInteger x, RoundingMode mode) Parameters: This method takes the following parameters: x: The BigI 3 min read BigIntegerMath ceilingPowerOfTwo() function | Guava | Java The ceilingPowerOfTwo(BigInteger x) method of Guava's BigIntegerMath class returns the smallest power of two greater than or equal to x. This is equivalent to BigInteger.valueOf(2).pow(log2(x, CEILING)). Syntax: public static BigInteger ceilingPowerOfTwo(BigInteger x) Parameters: This method takes t 2 min read Like