Answered # How can i add rows of pascal's triangle without using an array?

Featured Reply Nutster 58 Nutster 58 OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...

1

The elements of Pascal's triangle are the statistical combination of the given row and column, if you consider Pascal's triangle as a right triangle.

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

...

Each element of Pascal's triangle is the result of nCr, where *n* is the row number, starting at 0, and *r* is the column number, also starting at 0. As far as I can tell, Combination is not a function in the Math library, but really it should be.

```
public static long Factorial(byte nValue)
{
long nFact = 1L;
// If nValue > 20, this will generate a numberic overflow, even using long.
if (nValue < 0)
return 0L;
else if (nValue < 2)
return 1L;
for (long i=nValue; i > 1; --i)
nFact *= i;
return nFact;
}
public static int Combination(int nSetSize, int nChosen)
{
int nLow;
int nCombo = 1;
if (nChosen > nSetSize-nChosen)
nLow = nChosen;
else
nLow = nSetSize - nChosen;
for (int i=nLow+1; i <= nSetSize; ++i)
nCombo *= i;
nCombo /= Factorial(nSetSize - nLow);
return nCombo;
}
```

This code has not been tested, so there might be a bug or two in it.

There is also a good article on this stuff, including producing Pascal's triangle here.

0

Actually the sum of any row a Pascal's Triangle is 2^n, where n is the row number, starting at 0. The sum of all values above a certain row is (2^n)-1. So the sum of all values in row 5 is 2^5 = 32. The sum of all values above row 5 is 2^5 - 1 = 32-1 = 31.

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I don’t want at this stage work on a big separate project as I've already got plenty ...