Search code examples
c++recursionparameterscoutpascals-triangle

Is there any other way to make pascal triangle?


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?


Solution

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