Created
August 31, 2011 15:10
-
-
Save rohit-nsit08/1183795 to your computer and use it in GitHub Desktop.
nCr using dynamic programming
This file contains hidden or 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
| // calculates binomial coefficient using dynamic programming | |
| #include <stdio.h> | |
| #define MAX 100 | |
| int cache[MAX+1][MAX+1]; | |
| int ncr(int n,int r) | |
| { | |
| if(cache[n][r]==-1) | |
| cache[n][r] = ncr(n-1,r)+ncr(n-1,r-1); | |
| return cache[n][r]; | |
| } | |
| int get_ncr(int n,int r) | |
| { | |
| int i,j; | |
| for(i=0;i<=n;i++) | |
| { | |
| for(j=0;j<=n;j++) | |
| { | |
| cache[i][j]=-1; | |
| } | |
| } | |
| for(i=1;i<=n;i++) // initialize the cache | |
| { | |
| cache[i][0]=1; | |
| cache[i][i]=1; | |
| } | |
| return ncr(n,r); | |
| } | |
| int main(int argc, char const *argv[]) | |
| { | |
| int n,r; | |
| printf("enter the numbers n and r "); | |
| scanf("%d %d",&n,&r); | |
| printf("the ncr is %d ",get_ncr(n,r)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment