Wednesday, February 11, 2015

Finding binomial coefficient

Today we will learn how to find the binomial coefficient of a number

since we know








Hence we will make use of this and will find the solution recursively

#include <iostream>

#include <vector>

using namespace std;

unsigned int bin (int n, int k, vector<vector<int> > table)

{

cout<<"\n n="<<n<<"\t k="<<k;

if (n==0)

return 0;

if (!table[n-1][k-1])

table[n-1][k-1] = bin (n-1, k-1, table);

if (!table[n-1][k])

table[n-1][k] = bin (n-1, k, table);

return table[n-1][k-1] + table[n-1][k];

}

int main() {

int n, k;

cin>>n>>k;

std::vector <std::vector<int> > table (

n+1,

std::vector<int> (k+1));

for (int i=0;i<=n ;i++)

table[i][0] = 1;

cout<<bin (n,k, table);

re
turn 0;}

No comments: