Hi friends,
As you all know pascal triangle looks like
1 n=0
1 1 n=1
1 2 1 n=2
1 3 3 1 n=3
1 4 6 4 1 n=4
1 5 10 10 5 1 n=5
............................................ so on
Observe some facts:
1)Number of element in ith row = (i+1)
2) ith element in nth row = sum of (i-1)th & (i)th element in (n-1)th row for 1<i<(n+1)
3)Also ,
For n=0: (1+x)^0=1
For n=1: (1+x)^1=1 + 1*(x)
For n=2: (1+x)^2=1 + 2*(x) + 1*(x*x)
For n=3: (1+x)^3=1 + 3*(x) + 3*(x*x) + 1*(x*x*x)
Observe the coefficients underlined, they belongs to each row of above triangle. For eg. when n=2,
coefficients are (1,2,1) which is row number 2 of pascal triangle. Similar situation occurs for n=3,here
coefficients are (1,3,3,1) which is row number 3 of pascal triangle.
4)Coefficients of (1+x)^n form sequence : (nC0,nC1,nC2,.....nCn)
where nCr=(n!)/(r! * (n-r)!)
So,by finding nth row of pascal triangle we are finding coefficients of (1+x)^n and hence indirectly above sequence of combinations.
Size of 1-D array for above triangle for some n = 1+2+3+....+(n+1) =
((n+1)*(n+2))/2
Starting index in this 1-D array for nth row of pascal triangle= 1+2+...n =
(n*(n+1))/2
{
if(n==0)
{
a[0]=1;
}
else
{
Form_Pascal_Triangle(a,n-1);
unsigned long int PREVTO_NTH_ROW_START_INDEX=(n*(n-1))/2;
unsigned long int CURRENT_NTH_ROW_START_INDEX=(n*(n+1))/2;
//initialise first and last element of nth row of pascal triangle
a[CURRENT_NTH_ROW_START_INDEX]=1;
a[CURRENT_NTH_ROW_START_INDEX+n]=1;
for(unsigned long int i=1;i<n;i++)
{
a[CURRENT_NTH_ROW_START_INDEX+i] =
a[PREVTO_NTH_ROW_START_INDEX+i-1] +
a[PREVTO_NTH_ROW_START_INDEX+i];
}
}
}
int main()
{
unsigned long int n;
cout<<"Enter n:";
cin>>n;
unsigned long int size=((n*(n+1))/2)+(n+1);
unsigned long int a[size];
Form_Pascal_Triangle(a,n);
return 0;
}
As you all know pascal triangle looks like
1 n=0
1 1 n=1
1 2 1 n=2
1 3 3 1 n=3
1 4 6 4 1 n=4
1 5 10 10 5 1 n=5
............................................ so on
Observe some facts:
1)Number of element in ith row = (i+1)
2) ith element in nth row = sum of (i-1)th & (i)th element in (n-1)th row for 1<i<(n+1)
3)Also ,
For n=0: (1+x)^0=1
For n=1: (1+x)^1=1 + 1*(x)
For n=2: (1+x)^2=1 + 2*(x) + 1*(x*x)
For n=3: (1+x)^3=1 + 3*(x) + 3*(x*x) + 1*(x*x*x)
Observe the coefficients underlined, they belongs to each row of above triangle. For eg. when n=2,
coefficients are (1,2,1) which is row number 2 of pascal triangle. Similar situation occurs for n=3,here
coefficients are (1,3,3,1) which is row number 3 of pascal triangle.
4)Coefficients of (1+x)^n form sequence : (nC0,nC1,nC2,.....nCn)
where nCr=(n!)/(r! * (n-r)!)
So,by finding nth row of pascal triangle we are finding coefficients of (1+x)^n and hence indirectly above sequence of combinations.
Now we will see how to generate above pascal triangle recursively for specified value of 'n' using 1-D array in c++.
Size of 1-D array for above triangle for some n = 1+2+3+....+(n+1) =
((n+1)*(n+2))/2
Starting index in this 1-D array for nth row of pascal triangle= 1+2+...n =
(n*(n+1))/2
//Actual c++ routine starts here:
void Form_Pascal_Triangle(unsigned long int a[],unsigned long int n){
if(n==0)
{
a[0]=1;
}
else
{
Form_Pascal_Triangle(a,n-1);
unsigned long int PREVTO_NTH_ROW_START_INDEX=(n*(n-1))/2;
unsigned long int CURRENT_NTH_ROW_START_INDEX=(n*(n+1))/2;
//initialise first and last element of nth row of pascal triangle
a[CURRENT_NTH_ROW_START_INDEX]=1;
a[CURRENT_NTH_ROW_START_INDEX+n]=1;
for(unsigned long int i=1;i<n;i++)
{
a[CURRENT_NTH_ROW_START_INDEX+i] =
a[PREVTO_NTH_ROW_START_INDEX+i-1] +
a[PREVTO_NTH_ROW_START_INDEX+i];
}
}
}
int main()
{
unsigned long int n;
cout<<"Enter n:";
cin>>n;
unsigned long int size=((n*(n+1))/2)+(n+1);
unsigned long int a[size];
Form_Pascal_Triangle(a,n);
return 0;
}
Please comment if any doubt and like if helpful.Thanks
No comments:
Post a Comment