Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Pascal's triangle

I'm looking for an explanation for how the recursive version of pascal's triangle works

The following is the recursive return line for pascal's triangle.

int get_pascal(const int row_no,const int col_no)
{
    if (row_no == 0)
    {
        return 1;
    }
    else if (row_no == 1)
    {
        return 1;
    }
    else if (col_no == 0)
    {
        return 1;
    }
    else if (col_no == row_no)
    {
        return 1;
    }
    else
    {
        return(get_pascal(row_no-1,col_no-1)+get_pascal(row_no-1,col_no));
    }
}

I get how the algorithm works What I wonder is how the recursion does the work.

like image 964
starcorn Avatar asked Nov 19 '09 15:11

starcorn


People also ask

What is Pascal's triangle in C?

Pascal's triangle is one of the classic example taught to engineering students. It has many interpretations. One of the famous one is its use with binomial equations. All values outside the triangle are considered zero (0). The first row is 0 1 0 whereas only 1 acquire a space in pascal's triangle, 0s are invisible.

Is triangle code in C?

Algorithm. Step 1: Declare three sides of triangle. Step 2: Enter three sides at run time. Step 3: If side1 == side2 && side2 == side3 Go to step 6 Step 4: If side1 == side2 || side2 == side3 || side3 == side1 Go to Step 7 Step 5: Else Go to step 8 Step 6: Print the triangle is equilateral.

What is Pascals triangle formula?

Pascal's formula is used to find the element in the Pascal triangle. The formula for Pascal's triangle is nCm = n-1Cm-1 + n-1Cm.


3 Answers

Your algorithm contains a couple of unnecessary predicates for the base cases. It can be stated more simply as follows:

int pascal(int row, int col) {
  if (col == 0 || col == row) {
    return 1;
  } else {
    return pascal(row - 1, col - 1) + pascal(row - 1, col);
  }
}

This of course assumes that you're guaranteeing that the arguments passed to the function are non-negative integers; you can always include an assertion if you can't impose such a guarantee from outside the function.

like image 127
pmcs Avatar answered Oct 12 '22 18:10

pmcs


Pascal's triangle is essentially the sum of the two values immediately above it....

           1
         1   1
       1   2   1
     1   3   3   1

etc

  • In this, the 1's are obtained by adding the 1 above it with the blank space (0)
  • For code, all the 1's are occupied in either the first column (0), or when the (col == row)

For these two border conditions, we code in special cases (for initialization). The main chunk of the code (the recursive part) is the actual logic.

(The condition 'row == 1' is not necessary)

like image 7
Fox Avatar answered Oct 12 '22 16:10

Fox


The most optimized way is this one:

int pascal(int row, int col) {
  if (col == 0 || col == row) return 1;
  else if(col == 1 || (col + 1) == row) return row;
  else return pascal(row - 1, col - 1) + pascal(row - 1, col);
}

Unlike Fox's algorithm it prevents recursive calls for values which can be easily computed right from the input values.

like image 6
Jacobian Avatar answered Oct 12 '22 18:10

Jacobian