Is there any other way to make pascal triangle?

109 views Asked by At

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?

1

There are 1 answers

5
Roshan M On BEST ANSWER

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;
}