Pascal's Triangle

Given N; generate Pascal's triangle up to N rows.

Each column should have one space after it unless it is the last column.
All numbers should be right-aligned to the column.

IN

10

OUT

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

IN

1

OUT

1

IN

2

OUT

1
1 1

IN

3

OUT

1
1 1
1 2 1

IN

4

OUT

1
1 1
1 2 1
1 3 3 1

IN

5

OUT

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

IN

6

OUT

1
1 1
1 2  1
1 3  3  1
1 4  6  4 1
1 5 10 10 5 1

IN

7

OUT

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

IN

8

OUT

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

IN

9

OUT

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

No column present on the server; leaving my Bash code as a comment: n=`cat`;p=0+0;for i in `seq $n`;do c=1;for j in `seq $n`;do c+=+$((`cut -d+ -f-2<<<$p`));p=${p#*+};done;echo $c;p=$c;done|rev|column -ts+ -o\ |rev|sed 's/ *0.*//'

Wed Jul 27 06:43:26 2016


Leave a comment

Parsed as Markdown

Login to leave a comment