ANSWERS TO EXERCISE 3:
1.
pascal = zeros(12,13);
pascal(1,2) = 1;
for row = 2:12
for col
= 2:13
pascal(row, col) =
pascal(row-1, col) + pascal(row-1,col-1);
end
end
pascal(:,1) = []
First we create the 12 x 13 matrix of zeros.
The element at (1,1) of Pascal’s Triangle is 1, but we have to keep an extra column of zeros on the left. So, the element to be set is at (1,2).
Since the first row contains only one element, we can start on the second row.
We must also start on the second column, because the first column is just made up of zeros.
We set each element of Pascal’s Triangle to be the sum of the one above and the one above-left.
Finally, we elimante the extra column of zeros
Now pascal will have 12 rows of the Pascal Triangle
2.
sumcol = sum(pasc)
12 66 220 495 792 924 792 495 220 66 12 1
The sum of columns of Pascal’s Triangle represents the possible combinations of 12 elements(we have 12 rows in this case) elements. The first is “12 choose 1”, then “12 choose 2”, etc, until the last one: “12 choose 12”
These are represented by the formula n! / ((n-k)! k!), where n in this case is 12 and k varies from 1 to 12.
You can check these values using the factorial operator. Ex: factorial (12) / (factorial(10) * factorial(2)) gives you 66, just as expected.
sumrow = sum(pasc')
1 2 4 8 16 32 64 128 256 512 1024 2048
These are simply the powers of 2 from 0 to 11.
3.
fib = zeros(1,12);
for row = 1:12
for col
= 1:row
fib(row)
= fib(row) + pascal(row-col+1, col);
end
end
fib
This question wasn’t very obvious and few people know that Fibonacci’s numbers are hidden in Pascal’s Triangle.
We obtain the numbers by adding anti-diagonals, from the top left corner. Each diagonal adds up to one of the Fibonacci’s numbers.
We start from the first row, and for each row, we add each column to the next column on the row above.
That is:
fib(1) is pascal(1,1)
fib(2) is pascal(2,1) + pascal(1,2)
fib(3) is pascal(3,1) + pascal(2,2) + pascal(1,3)
fib(3) is pascal(4,1) + pascal(3,2) + pascal(2,3) + pascal(1,4)
…
Notice the need for row-col + 1 to determine the row of the Pascal element to be added.