Create a Pascal's Triangle that is a nested list and contains zeros in the unused spots.

In the output array, the numbers of Pascal's Triangle are separated by zeroes and padded by zeroes on each side so that they are centered. For example, the bottom row (last sub-array) must have no zeroes on the left and the right; the second-last sub-array has one zero padding on each side, and so on.

Here is the output for input 5:


As usual, the solution with the fewest bytes wins.

  • 5
    \$\begingroup\$ Duplicate of this. Just changing the output format does not change the challenge, unfortunately. Try posting on Stack Overflow if you still need help with this. \$\endgroup\$ – GamrCorps Feb 29 '16 at 4:44
  • 2
    \$\begingroup\$ Well, there are the extra zeros. \$\endgroup\$ – CalculatorFeline Feb 29 '16 at 4:46
  • \$\begingroup\$ This program prints the one you want (Python 3):print("def pascal(n):\n #make the nested list\n a=[[0 for i in range(2*n+1)] for j in range(n+1)] #make the list\n a[0][n]=1 #add the initial 1\n for i in range(1,n+1):\n for j in range(2*n+1):\n a[i][j]=a[i-1][j-1]+a[i-1][(j+1)%(2*n+1)] #the main part\n return a") \$\endgroup\$ – CalculatorFeline Feb 29 '16 at 4:53
  • 1
    \$\begingroup\$ @CatsAreFluffy The extra zeroes just replace the spaces in the previous iteration - this is functionally the exact same problem. \$\endgroup\$ – ricdesi Feb 29 '16 at 14:42
  • 2
    \$\begingroup\$ Can I use the native array representation syntax for my language, or is the format non-negotiable? \$\endgroup\$ – cat Feb 29 '16 at 15:46

Mathematica, 70 68 bytes


Similar to the MATL solution.


Mathematica, 48 bytes


CellularAutomation is fantastic.


Jelly, 12 bytes


Try it here.


                   This is a list of functions, each operating on the input, n:
NR                 Get the range [-n -n+1 ... 0 ... n-1 n].
  ¬                Logical NOT the entire range: [0 0 ... 1 ... 0 0].
         ³Ð¡       Repeat n times, and cumulate the results:
   ṙ-,1                Rotate by both -1 and 1
       S               Sum the results.
        $              (Joins the above two functions)

Haskell, 66 bytes

q n|d<-0<$[2..n]=scanl(\(s:t)_->zipWith(+)(0:s:t)$t++[0])(d++1:d)d

Usage example: q 4 -> [[0,0,0,1,0,0,0],[0,0,1,0,1,0,0],[0,1,0,2,0,1,0],[1,0,3,0,3,0,1]].

How it works:

d <- 0<$[2..n]                      -- bind d to a list of (length n)-1 zeros
scanl                               -- build a list
                         (d++1:d)   -- starting with  [d ++ 1 ++ d]
      \(s:t)_                    d  -- by combining the previous element with the
                                    -- elements of d, but ignoring them, i.e.
                                    -- build a list of (length d) by repeatedly
                                    -- modifying the start element by
          zipWith(+)                -- adding element-wise
                    (0:s:t)         -- the previous element prepended by 0  
                           t++[0]   -- and the tail of the previous element
                                    -- followed by a 0 

Python 3, 172 158 133 bytes

def p(n):
 for i in y(1,n+1):
  for j in y(x):a[i][j]=a[i-1][j-1]+a[i-1][(j+1)%(x)]
 return a

Keeps getting better

  • 1
    \$\begingroup\$ this isn't fully golfed yet, right? \$\endgroup\$ – cat Feb 29 '16 at 15:41
  • \$\begingroup\$ Um, yeah. This (in a slightly less golfed form) is printed by a program I left a a comment on the question. \$\endgroup\$ – CalculatorFeline Feb 29 '16 at 16:16

MATL, 24 22 21 bytes


EDIT (May 20, 2016): as of version 18.0.0 of the language, the above code needs a few changes to run. The link below includes those modifications

Try it online!

This uses a loop to push each new row onto the stack. A new row is computed from the previous row applying convolution with [1,0,1] and keeping only the desired size. After the loop, all rows are concatenated into a 2D array, which is displayed. 2D array are displayed in MATL as column-aligned numeric tables.

t           % implicit input n. Duplicate
Eq          % 2*n-1
:           % range [1,2,...,2*n-1]
=           % gives [0,0,...1,...0,0]. This is the first row
Gq:         % range [1,2,...,n-1]
"           % for each. Repeat n-1 times
  t         %   duplicate latest row. This duplicate will become the next row
  5B        %   push array [1,0,1] (5 converted to binary)
  X+        %   convolution
  8L        %   predefined literal [2,-1i]. Used for indexing
  )         %   apply that index: remove one element at each end
]           % end for each
N$v         % concatenate all rows into a 2D array. Implicitly display

Javascript, 152 146 bytes

f=i=>[...Array(i)].map((x,j)=>(z=[...Array(i*2-1)].map((_,k)=>+!!~[i-j,i+j].indexOf(k+1)),y=j?z.map((_,k)=>_||(k&&(k+1 in y)?y[k-1]+y[k+1]:_)):z))

                (k&&(k+1 in y)?

<input id=I onInput="O.innerHTML=F(I.value)" value=5><pre id=O>


Seriously, 33 bytes


Try it online

I'm relatively certain at least 7 or so of those bytes can be shaved off, so I'm going to wait to post an explanation until I'm done golfing this further.


PHP, 106 bytes


Try it online!


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.