mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Add test case to Binomial Coefficient Algorithm (#3179)
Co-authored-by: Yang Libin <contact@yanglibin.info>
This commit is contained in:
@ -11,36 +11,29 @@ package com.thealgorithms.maths;
|
||||
* */
|
||||
|
||||
public class BinomialCoefficient {
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* This method returns the number of ways in which k objects can be chosen from n objects
|
||||
*
|
||||
* @param total_objects Total number of objects
|
||||
* @param no_of_objects Number of objects to be chosen from total_objects
|
||||
* @param totalObjects Total number of objects
|
||||
* @param numberOfObjects Number of objects to be chosen from total_objects
|
||||
* @return number of ways in which no_of_objects objects can be chosen from total_objects objects
|
||||
*/
|
||||
|
||||
static int binomialCoefficient(int total_objects, int no_of_objects) {
|
||||
|
||||
//Base Case
|
||||
if(no_of_objects > total_objects) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Base Case
|
||||
if(no_of_objects == 0 || no_of_objects == total_objects) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
//Recursive Call
|
||||
return binomialCoefficient(total_objects - 1, no_of_objects - 1)
|
||||
+ binomialCoefficient(total_objects - 1, no_of_objects);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(binomialCoefficient(20,2));
|
||||
|
||||
//Output: 190
|
||||
}
|
||||
|
||||
public static int binomialCoefficient(int totalObjects, int numberOfObjects) {
|
||||
|
||||
// Base Case
|
||||
if (numberOfObjects > totalObjects) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Base Case
|
||||
if (numberOfObjects == 0 || numberOfObjects == totalObjects) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Recursive Call
|
||||
return binomialCoefficient(totalObjects - 1, numberOfObjects - 1)
|
||||
+ binomialCoefficient(totalObjects - 1, numberOfObjects);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user