Pascal's triangle is an arithmetic and geometric figure often associated with the name of Blaise Pascal, but also studied centuries earlier in India, Persia, China and elsewhere.

Its first few rows look like this: 

    1
   1 1
  1 2 1
 1 3 3 1 

where each element of each row is either 1 or the sum of the two elements right above it.

For example, the next row of the triangle would be:

   1   (since the first element of each row doesn't have two elements above it)
   4   (1 + 3)
   6   (3 + 3)
   4   (3 + 1)
   1   (since the last element of each row doesn't have two elements above it) 
So the triangle now looks like this: 

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

Each row    n    (starting with row   0   at the top) shows the coefficients of the binomial expansion of    (x + y)n. 



Task

Write a function that prints out the first    n    rows of the triangle   (with    f(1)    yielding the row consisting of only the element 1).

This can be done either by summing elements from the previous rows or using a binary coefficient or combination function.

Behavior for    n ≤ 0    does not need to be uniform, but should be noted.



See also

 Evaluate binomial coefficients


/*REXX program displays (or writes to a file)   Pascal's triangle  (centered/formatted).*/
numeric digits 3000                              /*be able to handle gihugeic triangles.*/
parse arg nn .                                   /*obtain the optional argument from CL.*/
if nn=='' | nn==","  then nn= 10                 /*Not specified?  Then use the default.*/
n= abs(nn)                                       /*N  is the number of rows in triangle.*/
w= length( !(n-1)  %  !(n%2)  %  !(n - 1 - n%2)) /*W:  the width of biggest integer.    */
ww= (n-1) * (W + 1)   +   1                      /*WW:  "    "    " triangle's last row.*/
@.= 1;      $.= @.;          unity= right(1, w)  /*defaults rows & lines; aligned unity.*/
                                                 /* [↓]  build rows of Pascals' triangle*/
        do    r=1  for n;              rm= r-1   /*Note:  the first column is always  1.*/
           do c=2  to rm;              cm= c-1   /*build the rest of the columns in row.*/
           @.r.c= @.rm.cm  +  @.rm.c             /*assign value to a specific row & col.*/
           $.r  = $.r   right(@.r.c, w)          /*and construct a line for output (row)*/
           end   /*c*/                           /* [↑]    C  is the column being built.*/
        if r\==1  then $.r= $.r  unity           /*for  rows≥2,  append a trailing  "1".*/
        end      /*r*/                           /* [↑]    R  is the  row   being built.*/
                                                 /* [↑]  WIDTH: for nicely looking line.*/
     do r=1  for n;     $$= center($.r, ww)      /*center this particular Pascals' row. */
     if nn>0  then say                       $$  /*SAY    if   NN    is positive,  else */
              else call lineout 'PASCALS.'n, $$  /*write this Pascal's row ───►  a file.*/
     end   /*r*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; !=1;  do j=2  to arg(1); != !*j; end /*j*/;  return !  /*compute factorial*/