# Generate Pascal's Pyramid

Pascal's Pyramid is an extension of Pascal's Triangle to the third dimension. Starting with a 1 as the apex, the elements of each successive layer can be determined by summing the three numbers that are above it. The nth layer contains the coefficients of (a + b + c)^(n-1).

You are to create a program that, given an input n, outputs the first n layers of Pascal's Pyramid. There must be two newlines between the different layers, and they must be in order. Within each layer, each row must be on a separate line, with the elements separated by spaces. Apart from that, you may format the pyramid however you like.

Your code should be reasonably fast generating a pyramid with 10 layers.

For example, your program, when given 4 as input, should output something like this:

1

1
1 1

1
2 2
1 2 1

1
3 3
3 6 3
1 3 3 1


May the shortest code win!

• possible duplicate as it is a sub-problem of Pascal's Pyramid and Higher Dimensions May 17, 2013 at 6:21
• @Howard that question gets you to generate only a single layer of the pyramid, while this gets you to generate the whole pyramid. May 17, 2013 at 6:42
• 1 is 0th layer. For an input of 4 do you mean to go up to the coefficients of (a + b + c)^3 or (a + b + c)^4?
– ejrb
May 17, 2013 at 10:36
• @ejrb the program should generate the first n layers - changed the formula to (a + b + c)^(n-1) to reflect that. May 17, 2013 at 10:39

## Python, 110

l=1
exec"print;n=1;r=0;exec\"c=0;exec'print n,;n*=r-c;c+=1;n/=c;'*r;print n;r+=1;n=n*l/r-n;\"*l;l+=1;"*input()


Original, ungolfed solution:

for layer in range(1, input() + 1):
print
n = 1
for row in range(1, layer + 1):
for col in range(1, row):
print n,
n *= row - col
n /= col
print n
n *= layer - row
n /= row


## Mathematica (112)

For[j = 0, j < 11, j++, For[i = 0, i <= j, i++, Print[Row[Array[Binomial[i, # - 1] &, i + 1], " "]]]; Print[""]]


Older method(166):

b = "stdout"; For[k = 0, k < 11, k++, For[i = 0, i <= k, i++, For[j = 0, j <= i, j++, WriteString[b, Binomial[i, j], " "]]; WriteString[b, "\n"]]; WriteString[b, "\n"]]


The following prints it in a different format, but is only 94 characters:

For[i = 0, i < 11, i++, Print[Column[Table[Binomial[n, k], {n, 0, i}, {k, 0, n}]]]; Print[""]]


## Mathematica 65 64

c=Column;c[c@Table[n~Binomial~k,{n,0,p},{k,0,n}]~Table~{p,0,#}]&


Usage

c=Column;c[c@Table[n~Binomial~k,{n,0,p},{k,0,n}]~Table~{p,0,#}]&@5

• The top layer should be {1}, not {{1},{1,1}}. Use {p, 0, #} May 18, 2013 at 0:38
• @DavidCarraher Yep, I haven't noticed it. Thanks May 18, 2013 at 1:24

## Python (134 128)

Factorials are probably more space-efficient, but I wanted to be fancy.

for n in range(1,input()+1):
a=r=1.
while a:
b=a;j=1.;a*=(n-r)/r
while b:print int(b),;b*=(r-j)/j;j+=1
print;r+=1
print


Uses the ratio property of Pascal's Triangle.
e.x. 1 4 6 4 1, 1:4, 4:6, 6:4, 4:1 = 1:4, 2:3, 3:2, 4:1

for n in range(1,input()+1): #Generate layers
a=r=1.               #a is the first value of a row in a layer; r represents the row
while a:          #when a is 0, there are no more rows in the pyramid.
b=a;i=1.;a*=(n-r)/r #b will calculate the values of that row
#use ratio property to determine next value of a
#i is a counter representing the ith item in row r
while b:print int(b),;b*=(r-i)/i;i+=1 #Output values using ratio property
print;r+=1          #increment row; loop
print

• while a has the same functionality as while a!=0 assuming a is always an integer. same for b
– ejrb
May 17, 2013 at 13:17

k 31

q)k)f:{,/{x**|x}{x{0+':x,0}\1}@'!x}
q)f 4
1
1
1 1
1
2 2
1 2 1
1
3 3
3 6 3
1 3 3 1

q)k)f:{,/{p**|p:x{0+':x,0}\1}'!x}

q)f 4

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

• Code and Preformatted Text May 31, 2013 at 10:37
• is preferable to use 4 space indentation. That also makes the font monospace and highlights the syntax when possible. Just like in the strangely similar answer by rrr. May 31, 2013 at 10:46