Reputation: 3
I'm trying to print out the pascal triangle.
and this is a normal one
int pascal(int l, int n) {
if (l == n || n == 0)
return 1;
else
return pascal(l - 1, n) + pascal(l - 1, n - 1);
}
but I want to use only one pascal function for recursion like
int pascal(int l, int n) {
return pascal();
}
Is there any solution for given condition?
Upvotes: 0
Views: 119
Reputation: 601
Yes, you can do by memoization because there is an overlapping subproblem.
int pascalTriangle(int row, int col) {
if (col == 0)
return 1;
if (row == 0)
return col;
return row * pascalTriangle(row - 1, col - 1) / col;
}
Upvotes: 2