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
// 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);
}
} |
Output:
Example 2 :Binomial Coefficient of 4 and 3 is : 4 Binomial Coefficient of 20 and 4 is : 4845
// 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);
}
}
} |
Output:
Reference :https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/LongMath.html#binomial-int-int-java.lang.IllegalArgumentException: k (7) > n (5)