That is what I thought, I could not make any sense from your formula.

The formula given by your professor is an iterative formula, you can't calculate it in one shot. If you are at row 5, you need to use values of row 4 to calculate values of row 5. But since you still don't know what values in row 4 are, you need to calculate first row 3, and so on. So, the product has to be calculated iteratively or recursively. Since I do not know what your professor has taught you, I can't really choose between the two methods.

Let's set up the Pascal triangle:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

Suppose we need to compute pascal(5, 3), whose value is 6 (i.e. 5th row, 3rd column). We have: pascal (5, 3) = pascal (4, 2) + pascal (4,3) = 3 + 3. But, of course, you don't know at this point that these two values are both 3. so you need to compute then. And so on until you reach a know value.

So the mathematical rules are as follows:

- pascal (1, 1) = 1;

- pascal (i, j) = pascal (i-1, j-1) + pascal (i-1, j);

- With an additional rule: if j gets to 0, set it to 1.

If you have learned subroutines and recursion, this can be done in about 4 lines of code. Possibly a couple of lines more with iteration.

Please note that I do not wish to give you a ready made solution (this is not proper for a homework in my view, the point is that you should really work out of this problem your way to learn), but I am trying to help you finding one, meaning that I am spending much more time trying to explain how to proceed than I would spend giving you a solution.

Just in case you think that I am bragging, this is an example code to do the work:

Perl Code:

use strict;
use warnings;
my ($i, $j) = @ARGV;
die "Error; second number must be smaller than first" if $j > $i;
print pascal($i, $j);
sub pascal {
my ($i, $j) = @_;
return 1 if $i == 1 or $j == 1;
return 1 if $j > $i;
return pascal ($i-1, $j) + pascal ($i-1, $j-1?$j-1:1);

Except that (1) I am not sure you will be able to explain it to your professor without a bit of hard work, and (2) I have intentionally left one small bug: it will print one of the Pascal triangle values, but not necessarily the right one.

Sample execution:

Code:

$ perl pascal.pl 20 13
125970

Pascal triangle according to Wikipedia:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

1 10 45 120 210 252 210 120 45 10 1

1 11 55 165 330 462 462 330 165 55 11 1

1 12 66 220 495 792 924 792 495 220 66 12 1

1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1

1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1

1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1

1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1

1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1

1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1

1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1

1 20 190 1140 4845 15504 38760 77520

**125970**167960 184756 167960

**125970** 77520 38760 15504 4845 1140 190 20 1

1 21 210 1330 5985 20349 54264 116280 203490 293930 352716 352716 293930 203490 116280 54264 20349 5985 1330 210 21 1

1 22 231 1540 7315 26334 74613 170544 319770 497420 646646 705432 646646 497420 319770 170544 74613 26334 7315 1540 231 22 1

Tweet This+ 1 thisPost To Linkedin