Skip to content

Add binomial coefficient implementation #2835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/java/com/thealgorithms/maths/BinomialCoefficient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.thealgorithms.maths;

/*
* Java program for Binomial Cofficients
* Binomial Cofficients: A binomial cofficient C(n,k) gives number ways
* in which k objects can be chosen from n objects.
* Wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient
*
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
*
* */

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
* @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
}

}