Pascal's triangle is the series of rows of numbers, starting with 1, where each cell is formed by summing the two above it. Here's an example.

Challenge and Scoring

The challenge is simple: given a number, print that many rows of Pascal's Triangle in as few characters as possible. Lowest character count wins. Each row should be separated by newlines, and each number by spaces.

Again, this is code golf: lowest character count wins.


Given 4, the program should print

1 1
1 2 1
1 3 3 1

Sample solution

$currentRow = [1];
$num = (int)file_get_contents('php://input');
function add($a, $b){
  return $a+$b;
while($num >0){
  echo implode(' ', $currentRow) . "\n";
  $currentRow = array_map(
    array_merge([0], $currentRow),
    array_merge($currentRow, [0])
  • \$\begingroup\$ Also, there is currently no winning criterion. Is this code-golf? (Not that it matters, seeing as how this is a dupe) \$\endgroup\$ – Conor O'Brien Feb 13 '18 at 4:26
  • \$\begingroup\$ Yes, it is a duplicate. I don't mind taking it down if that's a problem! I did mark in the problem that lowest character count wins. \$\endgroup\$ – JJ Brown Feb 13 '18 at 4:29
  • 1
    \$\begingroup\$ Ah, sure enough. Usually we have the code-golf tag on code golf challenges. Welcome to PPCG, and good luck on your next challenge! May I suggest the sandbox? You can iron out any potential problems there :) \$\endgroup\$ – Conor O'Brien Feb 13 '18 at 4:32
  • 4
    \$\begingroup\$ Welcome to PPCG! I'm sorry that your first challenge was a dupe, but thanks for taking it well. It was well-written too. I hope you stick around! \$\endgroup\$ – Rɪᴋᴇʀ Feb 13 '18 at 4:50
  • \$\begingroup\$ I also suggest using the sandbox. Besides, 1. We scores by byte count (see Sclipting esoteric programming language for more details), and 2. We really like flexible I/O format. There is no reason to override that. \$\endgroup\$ – user202729 Feb 13 '18 at 10:11

Browse other questions tagged or ask your own question.