Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1869eab

Browse files
Add binomial coefficients (TheAlgorithms#2835)
1 parentfe7e5d8 commit1869eab

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+
packagecom.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+
publicclassBinomialCoefficient {
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+
staticintbinomialCoefficient(inttotal_objects,intno_of_objects) {
24+
25+
//Base Case
26+
if(no_of_objects >total_objects) {
27+
return0;
28+
}
29+
30+
//Base Case
31+
if(no_of_objects ==0 ||no_of_objects ==total_objects) {
32+
return1;
33+
}
34+
35+
//Recursive Call
36+
returnbinomialCoefficient(total_objects -1,no_of_objects -1)
37+
+binomialCoefficient(total_objects -1,no_of_objects);
38+
}
39+
40+
publicstaticvoidmain(String[]args) {
41+
System.out.println(binomialCoefficient(20,2));
42+
43+
//Output: 190
44+
}
45+
46+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp