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


Imports System.Numerics
 
Module Module1
    Iterator Function GetRow(rowNumber As BigInteger) As IEnumerable(Of BigInteger)
        Dim denominator As BigInteger = 1
        Dim numerator = rowNumber
 
        Dim currentValue As BigInteger = 1
        For counter = 0 To rowNumber
            Yield currentValue
            currentValue = currentValue * numerator
            numerator = numerator - 1
            currentValue = currentValue / denominator
            denominator = denominator + 1
        Next
    End Function
 
    Function GetTriangle(quantityOfRows As Integer) As IEnumerable(Of BigInteger())
        Dim range = Enumerable.Range(0, quantityOfRows).Select(Function(num) New BigInteger(num))
        Return range.Select(Function(num) GetRow(num).ToArray())
    End Function
 
    Function CenterString(text As String, width As Integer)
        Dim spaces = width - text.Length
        Dim padLeft = (spaces / 2) + text.Length
        Return text.PadLeft(padLeft).PadRight(width)
    End Function
 
    Function FormatTriangleString(triangle As IEnumerable(Of BigInteger())) As String
        Dim maxDigitWidth = triangle.Last().Max().ToString().Length
        Dim rows = triangle.Select(Function(arr) String.Join(" ", arr.Select(Function(array) CenterString(array.ToString(), maxDigitWidth))))
        Dim maxRowWidth = rows.Last().Length
        Return String.Join(Environment.NewLine, rows.Select(Function(row) CenterString(row, maxRowWidth)))
    End Function
 
    Sub Main()
        Dim triangle = GetTriangle(20)
        Dim output = FormatTriangleString(triangle)
        Console.WriteLine(output)
    End Sub
 
End Module