function Pascals-Triangle { [CmdLetBinding()] param ( # Single parameter is the number of levels [Parameter(Mandatory=$true)][ValidateRange(1, [int]::MaxValue)][int]$Levels ) process { # Create a dummy previous array, just so that we know what is going on. $prev = [System.Numerics.BigInteger[]]::new(0) # Now for each level for($l = 0; $l -lt $Levels; $l++) { # Create the current working array $current = [System.Numerics.BigInteger[]]::new($prev.Length + 1) # We know for sure that the first and the last element are 1 (This avoids bounds checking in the loop below) $current[0] = 1 $current[$current.Length - 1] = 1 # Now for all other elements add the previous two together for($i = 1; $i -lt $current.Length - 1; $i++) { $current[$i] = $prev[$i - 1] + $prev[$i] } # Write out our result Write-Output ([string]::Join(' ', $current)) # Set the previous to the current for the next iteration $prev = $current } } }