Pascal\'s triangle is an arrangement of numbers such that each row is equivalent
ID: 670671 • Letter: P
Question
Pascal's triangle is an arrangement of numbers such that each row is equivalent to the coefficients of the binomial expansion of (x + y)(p - ), where p is some positive integer more than or equal to 1. For example, (x + y)2 = 1x2 + 2xy + 1y2 so the third row of Pascal's triangle is 1 2 1. Let Rm represent the /m-th row of Pascal's triangle, and Rm(n) be the n-th element of the row. By definition, Rm has m elements, and Rm(1) = Rm(n) = 1. The remaining elements are computed by the following recursive relationship: Rm(i) = Rm-1(i - 1) + Rm-1(i) for i = 2, hellips, m - 1. The first few rows of Pascal's triangle are depicted in the following figure. You may assume that m is a strictly positive integer. The output variable, row, must be a row vector.Explanation / Answer
working code:
function rownum = myPascalRow(n)
rownum(1, 1) = 1;
rownum(2, 1 : 2) = [1 1];
if n < 3
return
end
for r = 3 : n
rownum(r, 1) = 1;
for c = 2 : r-1
rownum(r, c) = rownum(r-1, c-1) + rownum(r-1, c);
end
rownum(r, r) = 1;
end