Pascal's triangle


Simple solution

  • Nothing fancy. Initially I thought of filling the triangle diagonally from both sides. With each iteration approaching the vertical line 1,3,6 etc. But this solution took me too long to implement. Problem was with the last row (Triangle bottom). It keps having sparces.
function generate(rows: number): number[][] {
  const output: number[][] = [];

  for (let i = 0; i < rows; i++) {
    output[i] = new Array(i + 1).fill(1);

    for (let j = 1; j < i; j++) {
      output[i][j] = output[i - 1][j - 1] + output[i - 1][j];
    }
  }

  return output;
}