[Question]
  [
[Pascal's triangle](http://en.wikipedia.org/wiki/Pascal%27s_triangle) is generated by starting with a 1 on the first row. On subsequent rows, the number is determined by the sum of the two numbers directly above it to the left and right.
To demonstrate, here are the first 5 rows of Pascal's triangle:
```
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1
```
## The Challenge
Given an input *n* (provided however is most convenient in your chosen language), generate the first *n* rows of Pascal's triangle. You may assume that *n* is an integer inclusively between 1 and 25. There must be a line break between each row and a space between each number, but aside from that, you may format it however you like. 
This is **code-golf**, so the shortest solution wins.
## Example I/O
```
> 1
1
> 9
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
```
  
[Answer]
## [J](http://jsoftware.com/), 12 characters
```
":@(!{:)\@i.
```
---
```
   i.5
0 1 2 3 4
   {:i.5
4
   (i.5)!{:i.5
1 4 6 4 1
   (!{:)i.5
1 4 6 4 1
   (!{:)\i.5
1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
   ":@(!{:)\i.5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
   (":@(!{:)\@i.)`''
+----------------------------------+
|+-+------------------------------+|
||@|+-------------------------+--+||
|| ||+-+---------------------+|i.|||
|| |||\|+-------------------+||  |||
|| ||| ||+-+---------------+|||  |||
|| ||| |||@|+--+----------+||||  |||
|| ||| ||| ||":|+-+------+|||||  |||
|| ||| ||| ||  ||2|+-+--+||||||  |||
|| ||| ||| ||  || ||!|{:|||||||  |||
|| ||| ||| ||  || |+-+--+||||||  |||
|| ||| ||| ||  |+-+------+|||||  |||
|| ||| ||| |+--+----------+||||  |||
|| ||| ||+-+---------------+|||  |||
|| ||| |+-------------------+||  |||
|| ||+-+---------------------+|  |||
|| |+-------------------------+--+||
|+-+------------------------------+|
+----------------------------------+
```
[Answer]
# Python, 56 Bytes
```
a=[1];exec"print a;a=map(sum,zip([0]+a,a+[0]));"*input()
```
Sample usage:
```
echo 9 | python filename.py
```
Produces:
```
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
```
[Answer]
# C, 522
 
A self demonstrating C answer. Couldn't be clearer!
Bonus points for finding the extra character.
```
#define returns return 0
#define fr for
#define twentyonechexpressis0 0
                                                                                i
                                                                               , x
                                                                              [ 52 ]
                                                                            [ 52] ,j, y
                                                                       ; main (c){fr (;i< c
                                                                    ; i++){ x[i][i]=x[ i][0]= 1
                                                         ; }for(i =2;i<c;i++){for (j=1;j<i;j++){x [i][j] =
                                    1 +x[i][j ]+x[i-1][j-1]+x[i-1] [j]+1-1+1-1+1-1+1-1+1-1+111-11- twentyonechexpressis0 -100-1; }
} ;for(i=0 ;i<c;i++){for(j=0;j<=i;j++){ printf("%3d%c",x[i][j],(1+1+1+1)*(1+1+1+1+1+1+1+1)) ;}putchar(1+1+(1<<1+1)+1+1+1+1+1+111111-111111-1);} /*thiscomment_takes28chars*/ returns; }
```
[Answer]
## Python, 94 91 88 70 63 characters
```
x=[1]
for i in input()*x:
 print x
 x=map(sum,zip([0]+x,x+[0]))
```
[Answer]
## Mathematica: 36 (41?)
  
Mathematica has the `Binomial` function, but that takes the fun out of this. I propose:
```
NestList[{0,##}+{##,0}&@@#&,{1},n-1]
```
The line above will render a ragged array such as:
```
{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1},
 {1, 5, 10, 10, 5, 1}, {1, 6, 15, 20, 15, 6, 1}}
```
Since this is a basic format in Mathematica I thought it would be acceptable, but as I read the rules again, I think it may not be. Adding `Grid@` will produce unequivocally acceptable output, for a total of **41** characters:
```
Grid@NestList[{0,##}+{##,0}&@@#&,{1},n-1]
```
`n = 6`:
```
1                       
1   1                   
1   2   1               
1   3   3   1           
1   4   6   4   1       
1   5   10  10  5   1   
1   6   15  20  15  6   1
```
[Answer]
### Golfscript (21 chars)
```
~]({0\{.@+\}/;1].p}*;
```
Since an explanation was requested:
```
# Stack contains 'n'
~](
# Stack: [] n
{
    # prev_row is [\binom{i,0} ... \binom{i,i}]
    # We loop to generate almost all of the next row as
    #     [(\binom{i,-1} + \binom{i,0}) ... (\binom{i,i-1} + \binom{i,i})]
    # \binom{i,-1} is, of course, 0
    # Stack: prev_row
    0\
    # Stack: 0 prev_row
    {
        # Stack: ... \binom{i,j-1} \binom{i,j}
        .@+\
        # Stack: ... (\binom{i,j-1} + \binom{i,j}) \binom{i,j}
    }/
    # Stack: \binom{i+1,0} ... \binom{i+1,i} \binom{i,i}
    # unless it's the first time round, when we still have 0
    # so we need to pop and then push a 1 for \binom{i+1,i+1}
    ;1]
    # next_row
    .p
}*
# final_row
;
```
[Answer]
# [convey](http://xn--wxa.land/convey/), 39 bytes
convey is a new 2d esolang I made. It is based on conveyor belts that move values around in a factory. `v<^>` are explicit belt directions, other connections are inferred, as each function has a fixed number of inputs and outputs. For example, `+` takes 2 values in and returns 1 value.
```
v<<<.~/.]
v>,v}"!{
"/&v}11
\v>+/^
$,^
_
```
[Try it online!](https://xn--wxa.land/convey/run.html#eyJjIjoidjw8PC5+Ly5dXG52Pix2fVwiIXtcblwiLyZ2fTExXG5cXHY+Ky9eXG4kLF5cbl8iLCJ2IjoxLCJpIjoiNCJ9)
[![pascals triangle](https://i.stack.imgur.com/ryL3m.gif)](https://i.stack.imgur.com/ryL3m.gif)
A row enters the main part from the top left. Two tiles in, the row gets `"` copied down and right. The right part prepends a `_`, which is a so called 'default value'. The actual value of `_` depends on the function it enters. It works like the neutral element, thus `_ + 3 = 3`, `4 * _ = 4`.
To understand the next part we have to take a look at port order. For example, `,` will join two paths, favoring the main input port. The order is as follows (lower takes precedence):
```
For input:     For output:
   0                3
   v                ^
 1>f<3            2<f>0
   ^                v
   2                1
```
So the upper `,` will favor inputs from the left, and the lower `,` will favor inputs from the top. In the animation the arrow heads are drawn a bit thicker to indicate main ports.
On `/` (rising flank) whenever a new list enters the tile, `_` gets pushed to the side port. `_` and the row then get joined, whereas the `_` comes from the main port and thus has right of way.
The bottom part appends a `_` by similar means: `\` (falling flank) pushes the length of the list to the side port, that will be set `$` to `_`.
Finally, `_ 1 2 1` and `1 2 1 _` get added in `+` and we get the next row `1 3 3 1`! We print a newline `/}` (`_` in `}` prints newlines) and then copy the row into the output `"}`. To prevent a row colliding with the next row when they get longer, we batch `~.` the lists: we first let 1 element through, then 2, then 3 … by getting the indices `/.` of an list of `n` 1s (`{!1`}), that get discarded in a sink `]`.
[Answer]
# Ruby: ~~51~~ ~~49~~ 46 characters
(45 characters code + 1 character command line option)
```
p=[];$_.to_i.times{n=0;p p.map!{|i|n+n=i}<<1}
```
Thanks to:
* [jsvnm](https://codegolf.stackexchange.com/users/3586/jsvnm) for suggesting an alternative for the value switching (2 characters)
* [G B](https://codegolf.stackexchange.com/users/18535/g-b) for spotting out a variable unused after previous improvement (4 characters)
Sample run:
```
bash-4.4$ ruby -ne 'p=[];$_.to_i.times{n=0;p p.map!{|i|n+n=i}<<1}' <<< 1
[1]
bash-4.4$ ruby -ne 'p=[];$_.to_i.times{n=0;p p.map!{|i|n+n=i}<<1}' <<< 9
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
```
[Try it online!](https://tio.run/##KypNqvz/v8A2OtZaJV6vJD8@U68kMze1uDrP1sC6QKFALzexQLG6JrMmTzvPNrPWxsaw9v9/y3/5BSWZ@XnF/3XzAA "Ruby – Try It Online")
[Answer]
## Scala, ~~81~~ ~~78~~ ~~72~~ 70 characters
81 chars: first attempt, shamelessly copied from the Python version :)
```
var x=Seq(1)
for(i<-1 to args(0).toInt){println(x)
x=(0+:x,x:+0).zipped.map(_+_)}
```
Run it as a script, or directly in the REPL.
Cut to 70 chars with something surprisingly readable and idiomatic:
```
Seq.iterate(Seq(1),readInt)(a=>(0+:a,a:+0).zipped.map(_+_))map println
```
Or ~~72~~ 70 characters with a totally different method:
```
0 to(readInt-1)map(i=>println(0 to i map(1 to i combinations(_)size)))
```
[Answer]
# [Keg](https://esolangs.org/wiki/Keg), ~~40~~ 33 bytes
```
1:&¿1.
,(|(⑻|:⑻$@MCƒℤ. ,⑨)⑹_01.
,
```
[Try it online!](https://tio.run/##y05N///f0Ert0H5DPS4djRqNRxN311gBCRUHX@djkx61LNFT0Hk0cYXmo4k74w1Aav7/twAA "Keg – Try It Online")
## Old Program Explained
![Pen](https://i.stack.imgur.com/mlTg5.png)
![Highlighted](https://i.stack.imgur.com/JVvtQ.png)
[Answer]
# Haskell, ~~94~~ 92
```
f=[1]:[zipWith(+)(0:x)x++[1]|x<-f]
main=readLn>>=mapM_(putStrLn.unwords.map show).(`take`f)
```
Output:
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
A 71 character version which does not print a space between each number:
```
f=[1]:[zipWith(+)(0:x)x++[1]|x<-f]
main=readLn>>=mapM_ print.(`take`f)
```
Output:
```
[1]
[1,1]
[1,2,1]
[1,3,3,1]
```
[Answer]
# R, 39 chars
R seems to be the very right tool for this task :-)
```
x=1;for(i in 1:n)x=c(print(x),0)+c(0,x)
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), ~~13~~ 10 bytes
*-3 bytes thanks to [Zgarb](https://codegolf.stackexchange.com/users/32014)*
```
mw↑¡Sż+Θ;1
```
[Try it online!](https://tio.run/##yygtzv7/P7f8UdvEQwuDj@7RPjfD2vD///@mAA "Husk – Try It Online")
### Explanation
Based on the same idea as [my Pip answer](https://codegolf.stackexchange.com/a/211994/16766): generate each row from the last one by tacking a 0 to the front of the row and adding it to itself element-wise. The Husk version creates the entire triangle as an infinite lazy list and then takes however many rows we need.
```
        ;1  Start with [1]
   ¡        Iterate this function:
    S        S-combinator: Sfgx is fx(gx) (aka f(x,g(x)) in C-style syntax)
     ż+       f: add a list itemwise to another list, keeping the extra value at 
               the end of the longer list unchanged
       Θ      g: prepend a falsey value (given a list of integers, prepend 0)
             The result of Sż+Θ is a 1-argument function that prepends a 0 to its
              argument and adds that itemwise to another copy of the argument
  ↑         From the infinite list of results, take N (where N is the input)
mw          Join each sublist on spaces (which means the main list gets
             autojoined on newlines)
```
[Answer]
## JavaScript (90 85 83 81)
```
for(n=prompt(o=i='');i++<n;o+='\n')for(s=j=1;j<=i;s=s*(i-j)/j++)o+=s+' ';alert(o)
```
Demo: <http://jsfiddle.net/tcRCS/3/>
**NOTE**: Doesn't work well in practice for about n > 30 because numbers overflow built-in integer data type and become floating-point numbers.
---
**Edit 1**: removed 5 characters by converting `while` to `for` and combining statements
**Edit 2**: move `s=` statement inside `for` and save 2 chars
**Edit 3**: combine `s=1,j=1` initializer into `s=j=1` and save 2 chars
[Answer]
## in Q (25 characters/20 with shorter version)
```
t:{(x-1) (p:{0+':x,0})\1}
```
Shorter 
```
t:{(x-1){0+':x,0}\1}
```
Sample usage:
```
q)t 4
1
1 1
1 2 1
1 3 3 1
```
[Answer]
# awk - 73 chars
fairly straightforward implementation:
```
{for(i=0;i<$1;++i)for(j=i;j>=0;)printf"%d%c",Y[j]+=i?Y[j-1]:1,j--?32:10}
```
sample run:
```
% awk -f pascal.awk <<<10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
```
[Answer]
## Perl, 52, 49 characters
Edit: using `say` instead of `print`
```
map{@_=(1,map$_[$_-1]+$_[$_],1..@_);say"@_"}1..<>
```
[Answer]
## APL, ~~19~~ 15 characters
A bit late to the party, perhaps?
```
{⍪{⍵!⍨⍳⍵+1}¨⍳⍵}
```
It doesn't beat the J entry, though.
This assumes that the index origin (`⎕IO`) is set to `0`. Unfortunately, with an index origin of `1`, we need ~~25~~ 18 characters:
```
{⍪{⍵!⍨0,⍳⍵}¨1-⍨⍳⍵}
```
There are two `⍨`s in the code to express my frustration.
Demo:
```
      {⍪{⍵!⍨⍳⍵+1}¨⍳⍵}5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
---
Explanations
Short version:
* `⍳⍵` (with an index origin of 0) produces an array of the numbers from 0 to `⍵-1` inclusive, where `⍵` is the right argument to the function.
* `⍳⍵+1` generates all numbers from 0 to `⍵`
* `{⍵!⍨⍳⍵+1}` generates `⍵` choose `k` for every element `k` in `⍳⍵+1`. The `⍨` (commute) operator swaps the arguments to a function around, such that the right hand argument becomes the left, and vice versa.
* `{⍵!⍨⍳⍵+1}¨⍳⍵` passes each element in `⍳⍵` using the `¨` (each) operator. The result is a one dimensional array containing the first `⍵` rows of the Pascal's Triangle.
* The one argument form of `⍪` takes a one dimensional vector, and makes it a column rather than a row. Each row of the triangle is put on its own line.
Long answer:
* Virtually the same as the other version, except that `1-⍨` is placed before an `⍳` to replicate an index origin of 0.
* `0,⍳⍵` with an index origin of 1 replicates `⍳⍵+1` with an index origin of 0.
[Answer]
# Perl, ~~47~~ 54 characters
```
$p=1;map{print"@{[split//,$p]}\n";$p*=11}1..<>
```
It takes a number from the command line, but doesn't perform any error checks.
Just realized it only works up to n=4. It was some old code I had on my hd.
This works though:
```
map{@a=(1,map$a[$_-1]+=$a[$_],1..@a);print"@a\n"}a..n
```
n has to be input into the script though, or it would be one character more.
[Answer]
# Pascal: ~~216~~ 192 characters
(Not a real competitor, just an honorific presence.)
```
var p:array[0..1,0..25]of LongInt;i,j,n,u:Word;begin
Read(n);u:=0;for i:=1to n do begin
p[1,1]:=1;for j:=1to i do begin
p[u,j]:=p[1-u,j-1]+p[1-u,j];Write(p[u,j],' ')end;u:=1-u;Writeln
end
end.
```
Sample run:
```
bash-4.2$ fpc pascal.pas 
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
bash-4.2$ ./pascal <<< 1
1 
bash-4.2$ ./pascal <<< 9
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
```
[Answer]
# Perl, 37 bytes
```
s/\d+/$&+$'/eg,say$_="1 $_"for($a)x<>
```
[Try it online!](https://tio.run/##K0gtyjH9/79YPyZFW19FTVtFXT81Xac4sVIl3lbJUEElXiktv0hDJVGzwsbu/3/Lf/kFJZn5ecX/dX1N9QwMDQA)
[Answer]
## Perl, 77 Chars
```
$o[0]=1;for(1..<>){$"=" ";for(1..$_){$n[$_]=$o[$_]+$o[$_-1]}@o=@n;print"@o
"}
```
Example input
```
5
```
Example output
```
 1
 1 1
 1 2 1
 1 3 3 1
 1 4 6 4 1
```
[Answer]
## C, ~~132~~ 127 characters
```
c[25][25],n,i,j;main(){for(scanf("%d",&n);i<n;i++)for(j=0;j<=i;j++)printf("%d%c",c[i][j]=j?c[i-1][j-1]+c[i-1][j]:1,i-j?32:10);}
```
[Answer]
# [MATL](https://esolangs.org/wiki/MATL), 10 bytes
*Language created after this challenge*
```
1iq:"tTTY+
```
[**Try it online!**](http://matl.tryitonline.net/#code=MWlxOiJ0VFRZKw&input=OQ)
```
1       % Push a 1. This will be the first row
iq:     % Take input n. Generate range [1,2,...,n-1]
"       % For each (that is, repeat n-1 times)
  t     %   Duplicate latest row
  TT    %   Push [1 1]
  Y+    %   Convolve latest row with [1 1] to produce next row
        % Implicitly end for each
        % Implicitly display stack contents
```
[Answer]
# JavaScript, ~~70~~ 69 bytes
Generating Pascal's Triangle in a golfy manner has always given me brain ache but every time it comes up, I give it another try. Last night, armed with a few beers, I finally cracked it and came up with a *working* solution I was happy with. Fitting, then, that this should be my 500th (undeleted) solution here.
0-indexed and includes a trailing newline and a trailing space on each line.
```
n=>(g=x=>x++>n?``:(h=z=>y>x?``:z+` `+h(z*(x-y)/y++))(y=1)+`
`+g(x))``
```
---
## Try it
```
o.innerText=(f=
n=>(g=x=>x++>n?``:(h=z=>y>x?``:z+` `+h(z*(x-y)/y++))(y=1)+`
`+g(x))``)(i.value=8);oninput=_=>o.innerText=f(+i.value)
```
```
<input id=i type=number><pre id=o></pre>
```
[Answer]
## D ~~134~~ 128 chars
```
import std.stdio;void main(){int n,m;int[]l,k=[0,1];readf("%d",&n);foreach(i;0..n){writeln(l=k~0);k=[];foreach(e;l)k~=m+(m=e);}}
```
output for 9 is 
```
>9
[0, 1, 0]
[0, 1, 1, 0]
[0, 1, 2, 1, 0]
[0, 1, 3, 3, 1, 0]
[0, 1, 4, 6, 4, 1, 0]
[0, 1, 5, 10, 10, 5, 1, 0]
[0, 1, 6, 15, 20, 15, 6, 1, 0]
[0, 1, 7, 21, 35, 35, 21, 7, 1, 0]
[0, 1, 8, 28, 56, 70, 56, 28, 8, 1, 0]
```
taking full advantage of "you may format it however you like"; there is a space between each number and a linebreak 
edit repositioned the assignment to `l` to shave of some chars
[Answer]
## Scala, 131 characters
```
object P extends App{var x=List(1)
while(x.size<=args(0).toInt){println(x.mkString(" "))
x=(0+:x:+0).sliding(2).map(_.sum).toList}}
```
Takes the input from the command line.
Output for n=10:
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
```
[Answer]
## F♯ - 203 characters
My first attempt at a round of code golf, and first attempt at functional programming. There is probably some obvious way to shorten it I haven't quite figured out yet. It complies in VS2010s F♯ compiler (which has the effect of running #light by default unlike earlier versions), and also works in the F♯ interpreter. Accepts input via stdin. Wish there was a better way for the input/output though! Lots of characters!
```
open System
let rec C r m =if r=0||m<=0||m>=r then 1 else C(r-1)m+C(r-1)(m-1)
for j = 0 to Convert.ToInt32(Console.ReadLine ()) do (
 [0..j]|>List.map(C j)|>List.iter(fun k->printf "%i " k)
 printf "\n")
```
[Answer]
*Why is there no accepted answer to this question?*
## VBA - 249 chars
```
Sub t(n)
ReDim a(1 To n,1 To n*2)
a(1,n)=1:y=vbCr:z=" ":d=z & 1 & z & y:For b=2 To n:For c=1 To n*2:x=a(b-1,c)
If c>1 Then a(b,c)=a(b-1,c-1)+x
If c<n*2 Then a(b,c)=a(b-1,c+1)+x
d=IIf(a(b,c)<>0,d & z & a(b,c) & z,d):Next:d=d & y:Next:MsgBox d
End Sub
```
[Answer]
postscript - 59 chars (63 if you count `-dn=` to get the number of rows in)
```
[1]n{dup ==[0 3 2 roll{dup 3 2 roll add exch}forall]}repeat
```
run with
```
gs -q -dn=10 -dBATCH pascal.ps 
```
to get
```
[1]
[1 1]
[1 2 1]
[1 3 3 1]
[1 4 6 4 1]
[1 5 10 10 5 1]
[1 6 15 20 15 6 1]
[1 7 21 35 35 21 7 1]
[1 8 28 56 70 56 28 8 1]
[1 9 36 84 126 126 84 36 9 1]
```
]