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


/* NetRexx */
options replace format comments java crossref symbols nobinary
 
numeric digits 1000 -- allow very large numbers
parse arg rows .
if rows = '' then rows = 11 -- default to 11 rows
printPascalTriangle(rows)
return
 
-- -----------------------------------------------------------------------------
method printPascalTriangle(rows = 11) public static
  lines = ''
  mx = (factorial(rows - 1) / factorial(rows % 2) / factorial(rows - 1 - rows % 2)).length() -- width of widest number
 
  loop row = 1 to rows
    n1 = 1.center(mx)
    line = n1
    loop col = 2 to row
      n2 = col - 1
      n1 = n1 * (row - n2) / n2
      line = line n1.center(mx)
      end col
    lines[row] = line.strip()
    end row
 
  -- display triangle
  ml = lines[rows].length() -- length of longest line
  loop row = 1 to rows
    say lines[row].centre(ml)
    end row
 
  return
 
-- -----------------------------------------------------------------------------
method factorial(n) public static
  fac = 1
  loop n_ = 2 to n
    fac = fac * n_
    end n_
  return fac /*calc. factorial*/
 