Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MatLab: Your Help is Needed Pascal\'s Triangle is a physical representation whic

ID: 3768377 • Letter: M

Question

MatLab: Your Help is Needed

Pascal's Triangle is a physical representation which lists the coefficients needed to write an integer power of a binomial in its expanded form. The first, few rows of the triangle are as follows: Note that each level from n = 2 onwards satisfies the following conditions: the beginning and ending elements are always 1, every element in between the beginning and the end is the sum of the two elements diagonally above to the left and diagonally above to the right. One may use the coefficients in the n = 3 row, for example, to write (a + b)^3 in its expanded form as a^3 + 3a^2b + 3ab^2 + b^3. Given a non-negative integer n, write a program that calculates the sequence of integers on any given level of Pascal's Triangle. (Bonus points if you take advantage of symmetry.)

Explanation / Answer

function P = pascalsTriangle(N)
% generate a matrix having ones in its diagonal and first column
P = eye(N);
P(:, 1) = 1;
  
% loop through the items and summarize the elements above them
for j = 3 : N
for i = 2 : N - 1
P(j, i) = P(j - 1, i - 1) + P(j - 1, i);
end
end
end