Skip to content

Commit 1869eab

Browse files
Add binomial coefficients (TheAlgorithms#2835)
1 parent fe7e5d8 commit 1869eab

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.maths;
2+
3+
/*
4+
* Java program for Binomial Cofficients
5+
* Binomial Cofficients: A binomial cofficient C(n,k) gives number ways
6+
* in which k objects can be chosen from n objects.
7+
* Wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient
8+
*
9+
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
10+
*
11+
* */
12+
13+
public class BinomialCoefficient {
14+
15+
/**
16+
* This method returns the number of ways in which k objects can be chosen from n objects
17+
*
18+
* @param total_objects Total number of objects
19+
* @param no_of_objects Number of objects to be chosen from total_objects
20+
* @return number of ways in which no_of_objects objects can be chosen from total_objects objects
21+
*/
22+
23+
static int binomialCoefficient(int total_objects, int no_of_objects) {
24+
25+
//Base Case
26+
if(no_of_objects > total_objects) {
27+
return 0;
28+
}
29+
30+
//Base Case
31+
if(no_of_objects == 0 || no_of_objects == total_objects) {
32+
return 1;
33+
}
34+
35+
//Recursive Call
36+
return binomialCoefficient(total_objects - 1, no_of_objects - 1)
37+
+ binomialCoefficient(total_objects - 1, no_of_objects);
38+
}
39+
40+
public static void main(String[] args) {
41+
System.out.println(binomialCoefficient(20,2));
42+
43+
//Output: 190
44+
}
45+
46+
}

0 commit comments

Comments
 (0)