4
\$\begingroup\$

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ possible duplicate as it is a sub-problem of Pascal's Pyramid and Higher Dimensions \$\endgroup\$ – Howard May 17 '13 at 6:21
  • \$\begingroup\$ @Howard that question gets you to generate only a single layer of the pyramid, while this gets you to generate the whole pyramid. \$\endgroup\$ – Volatility May 17 '13 at 6:42
  • \$\begingroup\$ 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? \$\endgroup\$ – ejrb May 17 '13 at 10:36
  • \$\begingroup\$ @ejrb the program should generate the first n layers - changed the formula to (a + b + c)^(n-1) to reflect that. \$\endgroup\$ – Volatility May 17 '13 at 10:39
2
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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[""]]
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ The top layer should be {1}, not {{1},{1,1}}. Use {p, 0, #} \$\endgroup\$ – DavidC May 18 '13 at 0:38
  • \$\begingroup\$ @DavidCarraher Yep, I haven't noticed it. Thanks \$\endgroup\$ – Dr. belisarius May 18 '13 at 1:24
1
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ while a has the same functionality as while a!=0 assuming a is always an integer. same for b \$\endgroup\$ – ejrb May 17 '13 at 13:17
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$
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  
\$\endgroup\$
  • \$\begingroup\$ Code and Preformatted Text \$\endgroup\$ – manatwork May 31 '13 at 10:37
  • \$\begingroup\$ 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. \$\endgroup\$ – manatwork May 31 '13 at 10:46
0
\$\begingroup\$

APL, 18

{A←0,⍳⍵ ⋄ ⍉A∘.!A}

Output

{A←0,⍳⍵ ⋄ ⍉A∘.!A} 5

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

You'd think that Pascal would have a function that prints Pascal's Triangle or at least the nth row of it, but it doesn't.

\$\endgroup\$
  • \$\begingroup\$ The challenge asks for Pascal's Pyramid, not his Triangle. \$\endgroup\$ – Volatility Jun 10 '13 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.