Binomial Series has Very important role in mathematics .
Today we are going to discuss different approach of finding binomial coefficient .
Lets Start With Basic Approach
Directly implement this mathematical formula to find Binomial Coefficient
C-Program:
Please Suggest if Found Any Bug
Thank U
Today we are going to discuss different approach of finding binomial coefficient .
Lets Start With Basic Approach
Mathematical Formula to find binomial Coefficient |
Directly implement this mathematical formula to find Binomial Coefficient
C-Program:
- //Direct Approach to find Binomial Coefficient
- //Beginner 2 Computer Science
- #include<stdio.h>
- #include <string.h>
- int BinomialCoeff(int n,int r){
- /*
- Direct Approach
- coeff=n!/((n-r)! * r!)
- !-> stands for factorial
- ie. 5!= 5*4*3*2*1
- */
- int max(int a,int b){
- if(a>b)
- return a;
- else
- return b;
- }
- int min(int a,int b){
- if(a<b)
- return a;
- else
- return b;
- }
- int i,mx,mn;
- int coeff=1;
- //nC0 & nCn
- if(r==0 ||n==r)
- return 1;
- mx=max((n-r),r); //max of r & n-r
- mn=min((n-r),r); //min of r & n-r
- //calculating coeff=n!/mx!
- for(i=mx+1;i<=n;i++){
- coeff=coeff*i;
- }
- //calculate coeff=coeff/mn!
- for(i=2;i<=mn;i++){
- coeff=coeff/i;
- }
- return coeff;
- }
- int main()
- {
- int n=5,r=3;
- printf("==> %d\n",BinomialCoeff(n,r));
- return 0;
- }
Recursive Approach :
Recursive Approach to calculate binomial Coefficient |
Recursion Tree for Binomial Coefficient |
C-Program for Finding Binomial Coefficient using recursive Approach :
- //Recursive Approach for finding Binomial Coefficient
- //C99 Program
- #include<stdio.h>
- #include <string.h>
- int BinomialCoeff_Recur(int n,int r){
- /*
- recursive Approach
- when r=0 or r==n return 1 //base case
- else coeff=coeff=coeff+BinomialCoeff_Recur(n-1,r)+BinomialCoeff_Recur(n-1,r-1);
- return coeff
- */
- int coeff=0;
- if(r<=0 || r==n){ //termination case
- return 1;
- }
- if(r<n ){
- //recursive call
- coeff=coeff+BinomialCoeff_Recur(n-1,r)+BinomialCoeff_Recur(n-1,r-1);
- }
- return coeff;
- }
- int main()
- {
- int n=5,r=3;
- printf("==> %d\n",BinomialCoeff_Recur(n,r));
- return 0;
- }
Dynamic Programming Approach:
We gonna use bottom top approach & store result which may need in future .
Dynamic Programming Approach for finding Binomial Coefficient |
C-Program for finding Binomial Coefficient using Dynamic Programming Approach:
- //Dynamic Programming Approach for finding Binomial Coefficient
- // C Program
- //Beginner 2 Computer Science
- #include<stdio.h>
- #include<string.h>
- int BinomialCoeff_DynamicProgramming(int n,int r){
- /*
- **** this code is for C99
- ******Need <string.h>
- Need To fallow bottom top approch
- & Store value of each calcution may need in future
- */
- int min(int a,int b){
- if(a<b)
- return a;
- else
- return b;
- }
- int mtrx[n+1][r+1];
- int i,j;
- if(n==r || r==0){ //Special Case
- return 1;
- }
- else if(r>n){
- return 0;
- }
- //This is Special Fuction Supported in C99
- memset(mtrx,0,sizeof(mtrx)); //now for all i,y mtrx[i][j]=0
- //Calculation for binomial coefficient
- for(i=0;i<=n;i++){
- for(j=0;j<=min(i,r);j++){
- if(j==0 || j==i)
- mtrx[i][j]=1;
- else
- mtrx[i][j]=mtrx[i-1][j-1]+mtrx[i-1][j];
- }
- }
- return mtrx[n][r];
- }
- int main()
- {
- int n=5,r=3;
- printf("==> %d\n",BinomialCoeff_DynamicProgramming(n,r));
- return 0;
- }
Please Suggest if Found Any Bug
Thank U
I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Data Science with Python , kindly contact us http://www.maxmunus.com/contact
ReplyDeleteMaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us.
Sangita Mohanty
MaxMunus
E-mail: sangita@maxmunus.com
Skype id: training_maxmunus
Ph:(0) 9738075708 / 080 - 41103383
http://www.maxmunus.com/