Pascal's Triangle is a familiar mathematical construct with many interesting properties. It is constructed by starting with a 1 on top, and generating the numbers in the next row from the sum of the number to the left and right in the row above (with implied 0's around the boundary):
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
The Triangle can be expanded into N dimensions by using the same rules. For example, the 3-D Pascal's Triangle would be a pyramid with each row being a cross-section of the pyramid, i.e., a triangle. Each triangle's entry is generated by adding the three numbers in the "row" above forming a triangle directly above that entry. An example of the first four "rows" is shown below:
1
1
1 1
1
2 2
1 2 1
1
3 3
3 6 3
1 3 3 1
The 4-D Pascal's Triangle would have a 3-D Pascal's Triangle for the cross section, and so forth.
Your task is to write a program that takes as input the dimension of the Pascal's Triange, and a row index to display. The row index starts at 0, so row 0 is 1
for every dimension. Your program should abide by the following specifications:
- Take input N = dimension number, R = row number (0-based). The input can be either command-line arguments or from stdin. N >= 2, R >= 0
- Output the R'th row in the N-dimensional Pascal's Triangle, by displaying each row or triangle on a separate line as in the above example.
- The format can vary as long as it is 'triangle-like' and obvious where each row and entry in the row belongs with respect to other rows.
Here is some sample output (I believe these are correct):
>./pascal 2 5
1 5 10 10 5 1
>./pascal 3 4
1
4 4
6 12 6
4 12 12 4
1 4 6 4 1
>./pascal 4 3
1
3
3 3
3
6 6
3 6 3
1
3 3
3 6 3
1 3 3 1
>./pascal 5 2
1
2
2
2 2
1
2
2 2
1
2 2
1 2 1
Bonus: What is the significance of each "row" in an N-dimensional Pascal's triangle? I can think of at least three interesting facts.