Created
August 13, 2017 22:00
-
-
Save sdpatil/82dc0b510700b8d8f42490830b0d9d6b to your computer and use it in GitHub Desktop.
Compute the binomial coefficient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Problem: Design an efficient algorithm for computing(n/k) which has the property that it never | |
* overflows if the final result fits in the integer word size | |
*/ | |
public class BionomialCoefficient { | |
/** | |
* Bionomial coefficient is nothing but pascal's traingle basic idea is value of cell is sum of | |
* value of two cells from previous nodes (n-1) and n | |
*/ | |
public int computeBionomialCoefficient(int n, int k){ | |
if(k == 0 || k == n ) | |
return 1; | |
return computeBionomialCoefficient(n-1,k) + computeBionomialCoefficient(n-1,k-1); | |
} | |
public int computeXChooseY(int x, int y, int[][] xChooseY){ | |
if( y == 0 || x == y) | |
return 1; | |
if(xChooseY[x][y] == 0){ | |
int withoutY = computeXChooseY(x-1, y, xChooseY); | |
int withY = computeXChooseY(x-1, y-1, xChooseY); | |
xChooseY[x][y] = withY + withoutY; | |
} | |
return xChooseY[x][y]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment