Pascal's triangle: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added FreeBASIC)
(to create a pascal triangle of any size given)
Line 1: Line 1:
func pascal(n:Int)->[Int]{
{{task|Arithmetic operations}}
if n==1{

let a=[1]
[[wp:Pascal's triangle|Pascal's triangle]]   is an arithmetic and geometric figure first imagined by   [[wp:Blaise Pascal|Blaise Pascal]].
print(a)

return a
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 &nbsp; <tt> n </tt> &nbsp; (starting with row &nbsp; 0 &nbsp; at the top) shows the coefficients of the binomial expansion of &nbsp; <big><big> (x + y)<sup>n</sup>. </big></big>


;Task:
Write a function that prints out the first &nbsp; <tt> n </tt> &nbsp; rows of the triangle &nbsp; (with &nbsp; <tt> f(1) </tt> &nbsp; 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 &nbsp; <big><tt> n ≤ 0 </tt></big> &nbsp; does not need to be uniform, but should be noted.


;See also:
* [[Evaluate binomial coefficients]]
<br><br>

=={{header|360 Assembly}}==
{{trans|PL/I}}
<lang 360asm>* Pascal's triangle 25/10/2015
PASCAL CSECT
USING PASCAL,R15 set base register
LA R7,1 n=1
LOOPN C R7,=A(M) do n=1 to m
BH ELOOPN if n>m then goto
MVC U,=F'1' u(1)=1
LA R8,PG pgi=@pg
LA R6,1 i=1
LOOPI CR R6,R7 do i=1 to n
BH ELOOPI if i>n then goto
LR R1,R6 i
SLA R1,2 i*4
L R3,T-4(R1) t(i)
L R4,T(R1) t(i+1)
AR R3,R4 t(i)+t(i+1)
ST R3,U(R1) u(i+1)=t(i)+t(i+1)
LR R1,R6 i
SLA R1,2 i*4
L R2,U-4(R1) u(i)
XDECO R2,XD edit u(i)
MVC 0(4,R8),XD+8 output u(i):4
LA R8,4(R8) pgi=pgi+4
LA R6,1(R6) i=i+1
B LOOPI end i
ELOOPI MVC T((M+1)*(L'T)),U t=u
XPRNT PG,80 print
LA R7,1(R7) n=n+1
B LOOPN end n
ELOOPN XR R15,R15 set return code
BR R14 return to caller
M EQU 11 <== input
T DC (M+1)F'0' t(m+1) init 0
U DC (M+1)F'0' u(m+1) init 0
PG DC CL80' ' pg init ' '
XD DS CL12 temp
YREGS
END PASCAL</lang>
{{out}}
<pre>
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
1 10 45 120 210 252 210 120 45 10 1
</pre>

=={{header|8th}}==
One way, using array operations:
<lang forth>
\ print the array
: .arr \ a -- a
( . space ) a:each ;

: pasc \ a --
\ print the row
.arr cr
dup
\ create two rows from the first, one with a leading the other with a trailing 0
[0] 0 a:insert swap 0 a:push
\ add the arrays together to make the new one
' n:+ a:op ;

\ print the first 16 rows:
[1] ' pasc 16 times
</lang>

Another way, using the relation between element 'n' and element 'n-1' in a row:
<lang forth>
: ratio \ m n -- num denom
tuck n:- n:1+ swap ;

\ one item in the row: n m
: pascitem \ n m -- n
r@ swap
ratio
n:*/ n:round int
dup . space ;

\ One row of Pascal's triangle
: pascline \ n --
>r 1 int dup . space
' pascitem
1 r@ loop rdrop drop cr ;

\ Calculate the first 'n' rows of Pascal's triangle:
: pasc \ n
' pascline 0 rot loop cr ;

15 pasc
</lang>

=={{header|Ada}}==

The specification of auxiliary package "Pascal". "First_Row" outputs a row with a single "1", "Next_Row" computes the next row from a given row, and "Length" gives the number of entries in a row. The package is also used for the Catalan numbers solution [[http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle]]

<lang ada>package Pascal is
type Row is array (Natural range <>) of Natural;
function Length(R: Row) return Positive;
function First_Row(Max_Length: Positive) return Row;
function Next_Row(R: Row) return Row;
end Pascal;</lang>

The implementation of that auxiliary package "Pascal":

<lang Ada>package body Pascal is
function First_Row(Max_Length: Positive) return Row is
R: Row(0 .. Max_Length) := (0 | 1 => 1, others => 0);
begin
return R;
end First_Row;
function Next_Row(R: Row) return Row is
S: Row(R'Range);
begin
S(0) := Length(R)+1;
S(Length(S)) := 1;
for J in reverse 2 .. Length(R) loop
S(J) := R(J)+R(J-1);
end loop;
S(1) := 1;
return S;
end Next_Row;
function Length(R: Row) return Positive is
begin
return R(0);
end Length;
end Pascal;</lang>

The main program, using "Pascal". It prints the desired number of rows. The number is read from the command line.

<lang Ada>with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Command_Line, Pascal; use Pascal;

procedure Triangle is
Number_Of_Rows: Positive := Integer'Value(Ada.Command_Line.Argument(1));
Row: Pascal.Row := First_Row(Number_Of_Rows);
begin
loop
-- print one row
for J in 1 .. Length(Row) loop
Ada.Integer_Text_IO.Put(Row(J), 5);
end loop;
Ada.Text_IO.New_Line;
exit when Length(Row) >= Number_Of_Rows;
Row := Next_Row(Row);
end loop;
end Triangle;</lang>

{{out}}

<pre>>./triangle 12
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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1</pre>

=={{header|ALGOL 68}}==
<lang algol68>PRIO MINLWB = 8, MAXUPB = 8;
OP MINLWB = ([]INT a,b)INT: (LWB a<LWB b|LWB a|LWB b),
MAXUPB = ([]INT a,b)INT: (UPB a>UPB b|UPB a|UPB b);

OP + = ([]INT a,b)[]INT:(
[a MINLWB b:a MAXUPB b]INT out; FOR i FROM LWB out TO UPB out DO out[i]:= 0 OD;
out[LWB a:UPB a] := a; FOR i FROM LWB b TO UPB b DO out[i]+:= b[i] OD;
out
);

INT width = 4, stop = 9;
FORMAT centre = $n((stop-UPB row+1)*width OVER 2)(q)$;

FLEX[1]INT row := 1; # example of rowing #
FOR i WHILE
printf((centre, $g(-width)$, row, $l$));
# WHILE # i < stop DO
row := row[AT 1] + row[AT 2]
OD</lang>
{{Out}}
<pre>
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
</pre>

=={{header|APL}}==
Pascal' s triangle of order ⍵

<lang apl>
{A←0,⍳⍵ ⋄ ⍉A∘.!A}
</lang>

example

<lang apl>
{A←0,⍳⍵ ⋄ ⍉A∘.!A} 3
</lang>

<pre>
1 0 0 0
1 1 0 0
1 2 1 0
1 3 3 1
</pre>


=={{header|AppleScript}}==

<lang AppleScript>-- pascal :: Int -> [[Int]]
on pascal(intRows)
script addRow
on nextRow(row)
script add
on lambda(a, b)
a + b
end lambda
end script
zipWith(add, [0] & row, row & [0])
end nextRow
on lambda(xs)
xs & {nextRow(item -1 of xs)}
end lambda
end script
foldr(addRow, {{1}}, range(1, intRows - 1))
end pascal


-- TEST

on run
set lstTriangle to pascal(7)
script spaced
on lambda(xs)
script rightAlign
on lambda(x)
text -4 thru -1 of (" " & x)
end lambda
end script
intercalate("", map(rightAlign, xs))
end lambda
end script
script indented
on lambda(a, x)
set strIndent to leftSpace of a
{rows:strIndent & x & linefeed & rows of a, leftSpace:leftSpace of a & " "}
end lambda
end script
rows of foldr(indented, {rows:"", leftSpace:""}, map(spaced, lstTriangle))
end run



-- GENERIC LIBRARY FUNCTIONS

-- foldr :: (a -> b -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from lng to 1 by -1
set v to lambda(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldr

-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map

-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set nx to length of xs
set ny to length of ys
if nx < 1 or ny < 1 then
{}
else
set lng to cond(nx < ny, nx, ny)
set lst to {}
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, item i of ys)
end repeat
return lst
end tell
end if
end zipWith

-- cond :: Bool -> (a -> b) -> (a -> b) -> (a -> b)
on cond(bool, f, g)
if bool then
f
else
g
end if
end cond

-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate

-- range :: Int -> Int -> [Int]
on range(m, n)
set lng to (n - m) + 1
set base to m - 1
set lst to {}
repeat with i from 1 to lng
set end of lst to i + base
end repeat
return lst
end range

-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn</lang>

{{Out}}
<pre> 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
</pre>

=={{header|AutoHotkey}}==
ahk forum: [http://www.autohotkey.com/forum/viewtopic.php?p=276617#276617 discussion]
<lang AutoHotkey>n := 8, p0 := "1" ; 1+n rows of Pascal's triangle
Loop %n% {
p := "p" A_Index, %p% := v := 1, q := "p" A_Index-1
Loop Parse, %q%, %A_Space%
If (A_Index > 1)
%p% .= " " v+A_LoopField, v := A_LoopField
%p% .= " 1"
}
; Triangular Formatted output
VarSetCapacity(tabs,n,Asc("`t"))
t .= tabs "`t1"
Loop %n% {
t .= "`n" SubStr(tabs,A_Index)
Loop Parse, p%A_Index%, %A_Space%
t .= A_LoopField "`t`t"
}
Gui Add, Text,, %t% ; Show result in a GUI
Gui Show
Return

GuiClose:
ExitApp</lang>

Alternate {{works with|AutoHotkey L}}
<lang AutoHotkey>Msgbox % format(pascalstriangle())
Return

format(o) ; converts object to string
{
For k, v in o
s .= IsObject(v) ? format(v) "`n" : v " "
Return s
}
pascalstriangle(n=7) ; n rows of Pascal's triangle
{
p := Object(), z:=Object()
Loop, % n
Loop, % row := A_Index
col := A_Index
, p[row, col] := row = 1 and col = 1
? 1
: (p[row-1, col-1] = "" ; math operations on blanks return blanks; I want to assume zero
? 0
: p[row-1, col-1])
+ (p[row-1, col] = ""
? 0
: p[row-1, col])
Return p
}</lang>
n <= 0 returns empty

=={{header|AWK}}==
<lang awk>$ awk 'BEGIN{for(i=0;i<6;i++){c=1;r=c;for(j=0;j<i;j++){c*=(i-j)/(j+1);r=r" "c};print r}}'</lang>
{{Out}}<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
</pre>

=={{header|BASIC}}==
===Summing from Previous Rows===
{{works with|FreeBASIC}}
This implementation uses an array to store one row of the triangle.
DIM initializes the array values to zero. For first row, "1" is then stored in the array.
To calculate values for next row, the value in cell (i-1) is added to each cell (i).
This summing is done from right to left so that it can be done on-place, without using a tmp buffer.
Because of symmetry, the values can be displayed from left to right.

Space for max 5 digit numbers is reserved when formatting the display.
The maximum size of triangle is 100 rows, but in practice it is limited by screen space.
If the user enters value less than 1, the first row is still always displayed.

<lang freebasic>DIM i AS Integer
DIM row AS Integer
DIM nrows AS Integer
DIM values(100) AS Integer

INPUT "Number of rows: "; nrows
values(1) = 1
PRINT TAB((nrows)*3);" 1"
FOR row = 2 TO nrows
PRINT TAB((nrows-row)*3+1);
FOR i = row TO 1 STEP -1
values(i) = values(i) + values(i-1)
PRINT USING "##### "; values(i);
NEXT i
PRINT
NEXT row</lang>

=={{header|Batch File}}==
Based from the Fortran Code.
<lang dos>@echo off
setlocal enabledelayedexpansion

::The Main Thing...
cls
echo.
set row=15
call :pascal
echo.
pause
exit /b 0
::/The Main Thing.

::The Functions...
:pascal
set /a prev=%row%-1
for /l %%I in (0,1,%prev%) do (
set c=1&set r=
for /l %%K in (0,1,%row%) do (
if not !c!==0 (
call :numstr !c!
set r=!r!!space!!c!
)
set /a c=!c!*^(%%I-%%K^)/^(%%K+1^)
)
echo !r!
)
goto :EOF

:numstr
::This function returns the number of whitespaces to be applied on each numbers.
set cnt=0&set proc=%1&set space=
:loop
set currchar=!proc:~%cnt%,1!
if not "!currchar!"=="" set /a cnt+=1&goto loop
set /a numspaces=5-!cnt!
for /l %%A in (1,1,%numspaces%) do set "space=!space! "
goto :EOF
::/The Functions.</lang>
{{Out}}
<pre> 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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1

Press any key to continue . . .</pre>

=={{header|BBC BASIC}}==
<lang bbcbasic> nrows% = 10
colwidth% = 4
@% = colwidth% : REM Set column width
FOR row% = 1 TO nrows%
PRINT SPC(colwidth%*(nrows% - row%)/2);
acc% = 1
FOR element% = 1 TO row%
PRINT acc%;
acc% = acc% * (row% - element%) / element% + 0.5
NEXT
PRINT
NEXT row%</lang>
{{Out}}
<pre> 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</pre>

=={{header|Befunge}}==
<lang Befunge>0" :swor fo rebmuN">:#,_&> 55+, v
v01*p00-1:g00.:<1p011p00:\-1_v#:<
>g:1+10p/48*,:#^_$ 55+,1+\: ^>$$@</lang>
{{Out}}
<pre>Number of rows: 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 </pre>

=={{header|Bracmat}}==
<lang bracmat>( out$"Number of rows? "
& get':?R
& -1:?I
& whl
' ( 1+!I:<!R:?I
& 1:?C
& -1:?K
& !R+-1*!I:?tabs
& whl'(!tabs+-1:>0:?tabs&put$\t)
& whl
' ( 1+!K:~>!I:?K
& put$(!C \t\t)
& !C*(!I+-1*!K)*(!K+1)^-1:?C
)
& put$\n
)
&
)</lang>
{{Out}}
<pre>Number of rows?
7
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</pre>

=={{header|Burlesque}}==

<lang burlesque>
blsq ) {1}{1 1}{^^2CO{p^?+}m[1+]1[+}15E!#s<-spbx#S
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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
</lang>



=={{header|C}}==

{{trans|Fortran}}

<lang c>#include <stdio.h>

void pascaltriangle(unsigned int n)
{
unsigned int c, i, j, k;

for(i=0; i < n; i++) {
c = 1;
for(j=1; j <= 2*(n-1-i); j++) printf(" ");
for(k=0; k <= i; k++) {
printf("%3d ", c);
c = c * (i-k)/(k+1);
}
}
printf("\n");
else{
var a=pascal(n:n-1)
}
var temp=a
}
for i in 0..<a.count{

if i+1==a.count{
int main()
temp.append(1)
{
break
pascaltriangle(8);
return 0;
}</lang>

===Recursive===
<lang c>#include <stdio.h>

#define D 32
int pascals(int *x, int *y, int d)
{
int i;
for (i = 1; i < d; i++)
printf("%d%c", y[i] = x[i - 1] + x[i],
i < d - 1 ? ' ' : '\n');

return D > d ? pascals(y, x, d + 1) : 0;
}

int main()
{
int x[D] = {0, 1, 0}, y[D] = {0};
return pascals(x, y, 0);
}</lang>

===Adding previous row values===

<lang c>void triangleC(int nRows) {
if (nRows <= 0) return;
int *prevRow = NULL;
for (int r = 1; r <= nRows; r++) {
int *currRow = malloc(r * sizeof(int));
for (int i = 0; i < r; i++) {
int val = i==0 || i==r-1 ? 1 : prevRow[i-1] + prevRow[i];
currRow[i] = val;
printf(" %4d", val);
}
printf("\n");
free(prevRow);
prevRow = currRow;
}
free(prevRow);
}</lang>

=={{header|C++}}==
<lang cpp>#include <iostream>
#include <algorithm>
#include<cstdio>
using namespace std;
void Pascal_Triangle(int size) {

int a[100][100];
int i, j;

//first row and first coloumn has the same value=1
for (i = 1; i <= size; i++) {
a[i][1] = a[1][i] = 1;
}
//Generate the full Triangle
for (i = 2; i <= size; i++) {
for (j = 2; j <= size - i; j++) {
if (a[i - 1][j] == 0 || a[i][j - 1] == 0) {
break;
}
a[i][j] = a[i - 1][j] + a[i][j - 1];
}
}

/*
1 1 1 1
1 2 3
1 3
1
first print as above format-->
for (i = 1; i < size; i++) {
for (j = 1; j < size; j++) {
if (a[i][j] == 0) {
break;
}
printf("%8d",a[i][j]);
}
cout<<"\n\n";
}*/
// standard Pascal Triangle Format
int row,space;
for (i = 1; i < size; i++) {
space=row=i;
j=1;
while(space<=size+(size-i)+1){
cout<<" ";
space++;
}
while(j<=i){
if (a[row][j] == 0){
break;
}
if(j==1){
printf("%d",a[row--][j++]);
}
else
printf("%6d",a[row--][j++]);
}
cout<<"\n\n";
}
}

int main()
{
//freopen("out.txt","w",stdout);
int size;
cin>>size;
Pascal_Triangle(size);
}

}</lang>
===C++11 (with dynamic and semi-static vectors)===
Constructs the whole triangle in memory before printing it. Uses vector of vectors as a 2D array with variable column size. Theoretically, semi-static version should work a little faster.
<lang cpp>// Compile with -std=c++11
#include<iostream>
#include<vector>
using namespace std;
void print_vector(vector<int> dummy){
for (vector<int>::iterator i = dummy.begin(); i != dummy.end(); ++i)
cout<<*i<<" ";
cout<<endl;
}
void print_vector_of_vectors(vector<vector<int>> dummy){
for (vector<vector<int>>::iterator i = dummy.begin(); i != dummy.end(); ++i)
print_vector(*i);
cout<<endl;
}
vector<vector<int>> dynamic_triangle(int dummy){
vector<vector<int>> result;
if (dummy > 0){ // if the argument is 0 or negative exit immediately
vector<int> row;
// The first row
row.push_back(1);
result.push_back(row);
// The second row
if (dummy > 1){
row.clear();
row.push_back(1); row.push_back(1);
result.push_back(row);
}
// The other rows
if (dummy > 2){
for (int i = 2; i < dummy; i++){
row.clear();
row.push_back(1);
for (int j = 1; j < i; j++)
row.push_back(result.back().at(j - 1) + result.back().at(j));
row.push_back(1);
result.push_back(row);
}
}
}
return result;
}
vector<vector<int>> static_triangle(int dummy){
vector<vector<int>> result;
if (dummy > 0){ // if the argument is 0 or negative exit immediately
vector<int> row;
result.resize(dummy); // This should work faster than consecutive push_back()s
// The first row
row.resize(1);
row.at(0) = 1;
result.at(0) = row;
// The second row
if (result.size() > 1){
row.resize(2);
row.at(0) = 1; row.at(1) = 1;
result.at(1) = row;
}
// The other rows
if (result.size() > 2){
for (int i = 2; i < result.size(); i++){
row.resize(i + 1); // This should work faster than consecutive push_back()s
row.front() = 1;
for (int j = 1; j < row.size() - 1; j++)
row.at(j) = result.at(i - 1).at(j - 1) + result.at(i - 1).at(j);
row.back() = 1;
result.at(i) = row;
}
}
}
return result;
}
int main(){
vector<vector<int>> triangle;
int n;
cout<<endl<<"The Pascal's Triangle"<<endl<<"Enter the number of rows: ";
cin>>n;
// Call the dynamic function
triangle = dynamic_triangle(n);
cout<<endl<<"Calculated using dynamic vectors:"<<endl<<endl;
print_vector_of_vectors(triangle);
// Call the static function
triangle = static_triangle(n);
cout<<endl<<"Calculated using static vectors:"<<endl<<endl;
print_vector_of_vectors(triangle);
return 0;
}

</lang>
===C++11 (with a class) ===
A full fledged example with a class definition and methods to retrieve data, worthy of the title object-oriented.
<lang cpp>// Compile with -std=c++11
#include<iostream>
#include<vector>
using namespace std;
class pascal_triangle{
vector<vector<int>> data; // This is the actual data
void print_row(vector<int> dummy){
for (vector<int>::iterator i = dummy.begin(); i != dummy.end(); ++i)
cout<<*i<<" ";
cout<<endl;
}
public:
pascal_triangle(int dummy){ // Everything is done on the construction phase
if (dummy > 0){ // if the argument is 0 or negative exit immediately
vector<int> row;
data.resize(dummy); // Theoretically this should work faster than consecutive push_back()s
// The first row
row.resize(1);
row.at(0) = 1;
data.at(0) = row;
// The second row
if (data.size() > 1){
row.resize(2);
row.at(0) = 1; row.at(1) = 1;
data.at(1) = row;
}
// The other rows
if (data.size() > 2){
for (int i = 2; i < data.size(); i++){
row.resize(i + 1); // Theoretically this should work faster than consecutive push_back()s
row.front() = 1;
for (int j = 1; j < row.size() - 1; j++)
row.at(j) = data.at(i - 1).at(j - 1) + data.at(i - 1).at(j);
row.back() = 1;
data.at(i) = row;
}
}
}
}
~pascal_triangle(){
for (vector<vector<int>>::iterator i = data.begin(); i != data.end(); ++i)
i->clear(); // I'm not sure about the necessity of this loop!
data.clear();
}
void print_row(int dummy){
if (dummy < data.size())
for (vector<int>::iterator i = data.at(dummy).begin(); i != data.at(dummy).end(); ++i)
cout<<*i<<" ";
cout<<endl;
}
void print(){
for (int i = 0; i < data.size(); i++)
print_row(i);
}
int get_coeff(int dummy1, int dummy2){
int result = 0;
if ((dummy1 < data.size()) && (dummy2 < data.at(dummy1).size()))
result = data.at(dummy1).at(dummy2);
return result;
}
vector<int> get_row(int dummy){
vector<int> result;
if (dummy < data.size())
result = data.at(dummy);
return result;
}
};
int main(){
int n;
cout<<endl<<"The Pascal's Triangle with a class!"<<endl<<endl<<"Enter the number of rows: ";
cin>>n;
pascal_triangle myptri(n);
cout<<endl<<"The whole triangle:"<<endl;
myptri.print();
cout<<endl<<"Just one row:"<<endl;
myptri.print_row(n/2);
cout<<endl<<"Just one coefficient:"<<endl;
cout<<myptri.get_coeff(n/2, n/4)<<endl<<endl;
return 0;
}

</lang>
=={{header|C sharp|C#}}==
{{trans|Fortran}}
Produces no output when n is less than or equal to zero.

<lang csharp>using System;

namespace RosettaCode {

class PascalsTriangle {

public static void CreateTriangle(int n) {
if (n > 0) {
for (int i = 0; i < n; i++) {
int c = 1;
Console.Write(" ".PadLeft(2 * (n - 1 - i)));
for (int k = 0; k <= i; k++) {
Console.Write("{0}", c.ToString().PadLeft(3));
c = c * (i - k) / (k + 1);
}
Console.WriteLine();
}
}
}
temp[i+1] = a[i]+a[i+1]
}
}
a=temp

public static void Main() {
print(a)
CreateTriangle(8);
return a
}
}
}</lang>

=={{header|Clojure}}==

For n < 1, prints nothing, always returns nil. Copied from the Common Lisp implementation below, but with local functions and explicit tail-call-optimized recursion (recur).
<lang lisp>(defn pascal [n]
(let [newrow (fn newrow [lst ret]
(if lst
(recur (rest lst)
(conj ret (+ (first lst) (or (second lst) 0))))
ret))
genrow (fn genrow [n lst]
(when (< 0 n)
(do (println lst)
(recur (dec n) (conj (newrow lst []) 1)))))]
(genrow n [1])))
(pascal 4)</lang>
And here's another version, using the ''partition'' function to produce the sequence of pairs in a row, which are summed and placed between two ones to produce the next row:
<lang lisp>
(defn nextrow [row]
(vec (concat [1] (map #(apply + %) (partition 2 1 row)) [1] )))

(defn pascal [n]
(assert (and (integer? n) (pos? n)))
(let [triangle (take n (iterate nextrow [1]))]
(doseq [row triangle]
(println row))))
</lang>
The ''assert'' form causes the ''pascal'' function to throw an exception unless the argument is (integral and) positive.

Here's a third version using the ''iterate'' function
<lang lisp>
(def pascal
(iterate
(fn [prev-row]
(->>
(concat [[(first prev-row)]] (partition 2 1 prev-row) [[(last prev-row)]])
(map (partial apply +) ,,,)))
[1]))
</lang>

Another short version which returns an infinite pascal triangle as a list, using the iterate function.

<lang lisp>
(def pascal
(iterate #(concat [1]
(map + % (rest %))
[1])
[1]))
</lang>

One can then get the first n rows using the take function

<lang lisp>
(take 10 pascal) ; returns a list of the first 10 pascal rows
</lang>

Also, one can retrieve the nth row using the nth function

<lang lisp>
(nth pascal 10) ;returns the nth row
</lang>

=={{header|CoffeeScript}}==
This version assumes n is an integer and n >= 1. It efficiently computes binomial coefficients.
<lang coffeescript>
pascal = (n) ->
width = 6
for r in [1..n]
s = ws (width/2) * (n-r) # center row
output = (n) -> s += pad width, n
cell = 1
output cell
# Compute binomial coefficients as you go
# across the row.
for c in [1...r]
cell *= (r-c) / c
output cell
console.log s

ws = (n) ->
s = ''
s += ' ' for i in [0...n]
s

pad = (cnt, n) ->
s = n.toString()
# There is probably a better way to do this.
cnt -= s.length
right = Math.floor(cnt / 2)
left = cnt - right
ws(left) + s + ws(right)

pascal(7)

</lang>

{{Out}}
<pre>
> coffee pascal.coffee
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
</pre>

=={{header|Common Lisp}}==
To evaluate, call (pascal n). For n < 1, it simply returns nil.

<lang lisp>(defun pascal (n)
(genrow n '(1)))

(defun genrow (n l)
(when (< 0 n)
(print l)
(genrow (1- n) (cons 1 (newrow l)))))

(defun newrow (l)
(if (> 2 (length l))
'(1)
(cons (+ (car l) (cadr l)) (newrow (cdr l)))))</lang>

An iterative solution with ''loop'', using ''nconc'' instead of ''collect'' to keep track of the last ''cons''. Otherwise, it would be necessary to traverse the list to do a ''(rplacd (last a) (list 1))''.

<lang lisp>(defun pascal-next-row (a)
(loop :for q :in a
:and p = 0 :then q
:as s = (list (+ p q))
:nconc s :into a
:finally (rplacd s (list 1))
(return a)))

(defun pascal (n)
(loop :for a = (list 1) :then (pascal-next-row a)
:repeat n
:collect a))</lang>

=={{header|D}}==
===Less functional Version===
<lang d>int[][] pascalsTriangle(in int rows) pure nothrow {
auto tri = new int[][rows];
foreach (r; 0 .. rows) {
int v = 1;
foreach (c; 0 .. r+1) {
tri[r] ~= v;
v = (v * (r - c)) / (c + 1);
}
}
return tri;
}

void main() {
immutable t = pascalsTriangle(10);
assert(t == [[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]]);
}</lang>
===More functional Version===
<lang d>import std.stdio, std.algorithm, std.range;

auto pascal() pure nothrow {
return [1].recurrence!q{ zip(a[n - 1] ~ 0, 0 ~ a[n - 1])
.map!q{ a[0] + a[1] }
.array };
}

void main() {
pascal.take(5).writeln;
}</lang>
{{out}}
<pre>[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]</pre>

===Alternative Version===
There is similarity between Pascal's triangle and [[Sierpinski triangle]].
Their difference are the initial line and the operation that act on the line element to produce next line.
The following is a generic pascal's triangle implementation for positive number of lines output (n).
<lang d>import std.stdio, std.string, std.array, std.format;

string Pascal(alias dg, T, T initValue)(int n) {
string output;

void append(in T[] l) {
output ~= " ".replicate((n - l.length + 1) * 2);
foreach (e; l)
output ~= format("%4s", format("%4s", e));
output ~= "\n";
}

if (n > 0) {
T[][] lines = [[initValue]];
append(lines[0]);
foreach (i; 1 .. n) {
lines ~= lines[i - 1] ~ initValue; // length + 1
foreach (int j; 1 .. lines[i-1].length)
lines[i][j] = dg(lines[i-1][j], lines[i-1][j-1]);
append(lines[i]);
}
}
return output;
}

string delegate(int n) genericPascal(alias dg, T, T initValue)() {
mixin Pascal!(dg, T, initValue);
return &Pascal;
}

void main() {
auto pascal = genericPascal!((int a, int b) => a + b, int, 1)();
static char xor(char a, char b) { return a == b ? '_' : '*'; }
auto sierpinski = genericPascal!(xor, char, '*')();

foreach (i; [1, 5, 9])
writef(pascal(i));
// an order 4 sierpinski triangle is a 2^4 lines generic
// Pascal triangle with xor operation
foreach (i; [16])
writef(sierpinski(i));
}</lang>
{{out}}
<pre> 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
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
*
* *
* _ *
* * * *
* _ _ _ *
* * _ _ * *
* _ * _ * _ *
* * * * * * * *
* _ _ _ _ _ _ _ *
* * _ _ _ _ _ _ * *
* _ * _ _ _ _ _ * _ *
* * * * _ _ _ _ * * * *
* _ _ _ * _ _ _ * _ _ _ *
* * _ _ * * _ _ * * _ _ * *
* _ * _ * _ * _ * _ * _ * _ *
* * * * * * * * * * * * * * * *</pre>

=={{header|Delphi}}==
<lang delphi>program PascalsTriangle;

procedure Pascal(r:Integer);
var
i, c, k:Integer;
begin
for i := 0 to r - 1 do
begin
c := 1;
for k := 0 to i do
begin
Write(c:3);
c := c * (i - k) div (k + 1);
end;
Writeln;
end;
end;

begin
Pascal(9);
end.</lang>

=={{header|DWScript}}==
Doesn't print anything for negative or null values.
<lang delphi>procedure Pascal(r : Integer);
var
i, c, k : Integer;
begin
for i:=0 to r-1 do begin
c:=1;
for k:=0 to i do begin
Print(Format('%4d', [c]));
c:=(c*(i-k)) div (k+1);
end;
PrintLn('');
end;
end;

Pascal(9);</lang>
{{Out}}
<pre> 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</pre>

=={{header|E}}==

So as not to bother with text layout, this implementation generates a HTML fragment. It uses a single mutable array, appending one 1 and adding to each value the preceding value.

<lang e>def pascalsTriangle(n, out) {
def row := [].diverge(int)
out.print("<table style='text-align: center; border: 0; border-collapse: collapse;'>")
for y in 1..n {
out.print("<tr>")
row.push(1)
def skip := n - y
if (skip > 0) {
out.print(`<td colspan="$skip"></td>`)
}
for x => v in row {
out.print(`<td>$v</td><td></td>`)
}
for i in (1..!y).descending() {
row[i] += row[i - 1]
}
out.println("</tr>")
}
out.print("</table>")
}</lang>

<lang e>def out := <file:triangle.html>.textWriter()
try {
pascalsTriangle(15, out)
} finally {
out.close()
}
makeCommand("yourFavoriteWebBrowser")("triangle.html")</lang>

=={{header|Eiffel}}==

<lang eiffel>
note
description : "Prints pascal's triangle"
output : "[
Per requirements of the RosettaCode example, execution will print the first n rows of pascal's triangle
]"
date : "19 December 2013"
authors : "Sandro Meier", "Roman Brunner"
revision : "1.0"
libraries : "Relies on HASH_TABLE from EIFFEL_BASE library"
implementation : "[
Recursive implementation to calculate the n'th row.
]"
warning : "[
Will not work for large n's (INTEGER_32)
]"

class
APPLICATION

inherit
ARGUMENTS

create
make

feature {NONE} -- Initialization

make
local
n:INTEGER
do
create {HASH_TABLE[ARRAY[INTEGER],INTEGER]}pascal_lines.make (n) --create the hash_table object
io.new_line
n:=25
draw(n)
end
feature
line(n:INTEGER):ARRAY[INTEGER]
--Calculates the n'th line
local
upper_line:ARRAY[INTEGER]
i:INTEGER
do
if n=1 then --trivial case first line
create Result.make_filled (0, 1, n+2)
Result.put (0, 1)
Result.put (1, 2)
Result.put (0, 3)
elseif pascal_lines.has (n) then --checks if the result was already calculated
Result := pascal_lines.at (n)
else --calculates the n'th line recursively
create Result.make_filled(0,1,n+2) --for caluclation purposes add a 0 at the beginning of each line
Result.put (0, 1)
upper_line:=line(n-1)
from
i:=1
until
i>upper_line.count-1
loop
Result.put(upper_line[i]+upper_line[i+1],i+1)
i:=i+1
end
Result.put (0, n+2) --for caluclation purposes add a 0 at the end of each line
pascal_lines.put (Result, n)
end
end

draw(n:INTEGER)
--draw n lines of pascal's triangle
local
space_string:STRING
width, i:INTEGER

do
space_string:=" " --question of design: add space_string at the beginning of each line
width:=line(n).count
space_string.multiply (width)
from
i:=1
until
i>n
loop
space_string.remove_tail (1)
io.put_string (space_string)
across line(i) as c
loop
if
c.item/=0
then
io.put_string (c.item.out+" ")
end
end
io.new_line
i:=i+1
end
end

feature --Access
pascal_lines:HASH_TABLE[ARRAY[INTEGER],INTEGER]
--Contains all already calculated lines
end
</lang>

=={{header|Elixir}}==
<lang elixir>defmodule Pascal do
def triangle(n), do: triangle(n,[1])
def triangle(0,list), do: list
def triangle(n,list) do
IO.inspect list
new_list = Enum.zip([0]++list, list++[0]) |> Enum.map(fn {a,b} -> a+b end)
triangle(n-1,new_list)
end
end

Pascal.triangle(8)</lang>

{{out}}
<pre>
[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]
</pre>

=={{header|Erlang}}==

<lang erlang>
-import(lists).
-export([pascal/1]).

pascal(1)-> [[1]];
pascal(N) ->
L = pascal(N-1),
[H|_] = L,
[lists:zipwith(fun(X,Y)->X+Y end,[0]++H,H++[0])|L].
</lang>

{{Out}}
<pre>
Eshell V5.5.5 (abort with ^G)
1> pascal:pascal(5).
[[1,4,6,4,1],[1,3,3,1],[1,2,1],[1,1],[1]]
</pre>

=={{header|ERRE}}==
<lang ERRE>
PROGRAM PASCAL_TRIANGLE

PROCEDURE PASCAL(R%)
LOCAL I%,C%,K%
FOR I%=0 TO R%-1 DO
C%=1
FOR K%=0 TO I% DO
WRITE("###";C%;)
C%=(C%*(I%-K%)) DIV (K%+1)
END FOR
PRINT
END FOR
END PROCEDURE

BEGIN
PASCAL(9)
END PROGRAM
</lang>
Output:
<pre>
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
</pre>

=={{header|Euphoria}}==
===Summing from Previous Rows===
<lang Euphoria>sequence row
row = {}
for m = 1 to 10 do
row = row & 1
for n = length(row)-1 to 2 by -1 do
row[n] += row[n-1]
end for
print(1,row)
puts(1,'\n')
end for</lang>

{{Out}}
<pre>
{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}
</pre>

=={{header|F Sharp|F#}}==
<lang fsharp>let rec nextrow l =
match l with
| [] -> []
| h :: [] -> [1]
| h :: t -> h + t.Head :: nextrow t
let pascalTri n = List.scan(fun l i -> 1 :: nextrow l) [1] [1 .. n]

for row in pascalTri(10) do
for i in row do
printf "%s" (i.ToString() + ", ")
printfn "%s" "\n"
</lang>

=={{header|Factor}}==

This implementation works by summing the previous line content. Result for n < 1 is the same as for n == 1.

<lang factor>USING: grouping kernel math sequences ;

: (pascal) ( seq -- newseq )
dup last 0 prefix 0 suffix 2 <clumps> [ sum ] map suffix ;

: pascal ( n -- seq )
1 - { { 1 } } swap [ (pascal) ] times ;</lang>

It works as:

<lang factor>5 pascal .
{ { 1 } { 1 1 } { 1 2 1 } { 1 3 3 1 } { 1 4 6 4 1 } }</lang>

=={{header|Fantom}}==

<lang fantom>
class Main
{
Int[] next_row (Int[] row)
{
new_row := [1]
(row.size-1).times |i|
{
new_row.add (row[i] + row[i+1])
}
new_row.add (1)

return new_row
}

Void print_pascal (Int n) // no output for n <= 0
{
current_row := [1]
n.times
{
echo (current_row.join(" "))
current_row = next_row (current_row)
}
}

Void main ()
{
print_pascal (10)
}
}
</lang>

=={{header|Forth}}==
<lang forth>: init ( n -- )
here swap cells erase 1 here ! ;
: .line ( n -- )
cr here swap 0 do dup @ . cell+ loop drop ;
: next ( n -- )
here swap 1- cells here + do
i @ i cell+ +!
-1 cells +loop ;
: pascal ( n -- )
dup init 1 .line
1 ?do i next i 1+ .line loop ;</lang>
This is a bit more efficient.
{{trans|C}}
<lang forth>: PascTriangle
cr dup 0
?do
1 over 1- i - 2* spaces i 1+ 0 ?do dup 4 .r j i - * i 1+ / loop cr drop
loop drop
;

13 PascTriangle</lang>

=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
Prints nothing for n<=0. Output formatting breaks down for n>20
<lang fortran>PROGRAM Pascals_Triangle

CALL Print_Triangle(8)

END PROGRAM Pascals_Triangle

SUBROUTINE Print_Triangle(n)

IMPLICIT NONE
INTEGER, INTENT(IN) :: n
INTEGER :: c, i, j, k, spaces

DO i = 0, n-1
c = 1
spaces = 3 * (n - 1 - i)
DO j = 1, spaces
WRITE(*,"(A)", ADVANCE="NO") " "
END DO
DO k = 0, i
WRITE(*,"(I6)", ADVANCE="NO") c
c = c * (i - k) / (k + 1)
END DO
WRITE(*,*)
END DO

END SUBROUTINE Print_Triangle</lang>

=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64

Sub pascalTriangle(n As UInteger)
If n = 0 Then Return
Dim prevRow(1 To n) As UInteger
Dim currRow(1 To n) As UInteger
Dim start(1 To n) As UInteger ''stores starting column for each row
start(n) = 1
For i As Integer = n - 1 To 1 Step -1
start(i) = start(i + 1) + 3
Next
prevRow(1) = 1
Print Tab(start(1));
Print 1U
For i As UInteger = 2 To n
For j As UInteger = 1 To i
If j = 1 Then
Print Tab(start(i)); "1";
currRow(1) = 1
ElseIf j = i Then
Print " 1"
currRow(i) = 1
Else
currRow(j) = prevRow(j - 1) + prevRow(j)
Print Using "######"; currRow(j); " ";
End If
Next j
For j As UInteger = 1 To i
prevRow(j) = currRow(j)
Next j
Next i
End Sub
pascalTriangle(14)
Print
Print "Press any key to quit"
Sleep</lang>

{{out}}
<pre>
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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
</pre>

=={{header|FunL}}==
=== Summing from Previous Rows ===
{{trans|Scala}}
<lang funl>import lists.zip

def
pascal( 1 ) = [1]
pascal( n ) = [1] + map( (a, b) -> a + b, zip(pascal(n-1), pascal(n-1).tail()) ) + [1]</lang>

=== Combinations ===
{{trans|Haskell}}
<lang funl>import integers.choose

def pascal( n ) = [choose( n - 1, k ) | k <- 0..n-1]</lang>

=== Pascal's Triangle ===
<lang funl>def triangle( height ) =
width = max( map(a -> a.toString().length(), pascal(height)) )

if 2|width
width++

for n <- 1..height
print( ' '*((width + 1)\2)*(height - n) )
println( map(a -> format('%' + width + 'd ', a), pascal(n)).mkString() )

triangle( 10 )</lang>

{{out}}
<pre>
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
</pre>

=={{header|GAP}}==
<lang gap>Pascal := function(n)
local i, v;
v := [1];
for i in [1 .. n] do
Display(v);
v := Concatenation([0], v) + Concatenation(v, [0]);
od;
end;

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 ]</lang>

=={{header|Go}}==
No output for n < 1. Otherwise, output formatted left justified.
<lang go>
package main

import "fmt"

func printTriangle(n int) {
// degenerate cases
if n <= 0 {
return
}
fmt.Println(1)
if n == 1 {
return
}
// iterate over rows, zero based
a := make([]int, (n+1)/2)
a[0] = 1
for row, middle := 1, 0; row < n; row++ {
// generate new row
even := row&1 == 0
if even {
a[middle+1] = a[middle] * 2
}
for i := middle; i > 0; i-- {
a[i] += a[i-1]
}
// print row
for i := 0; i <= middle; i++ {
fmt.Print(a[i], " ")
}
if even {
middle++
}
for i := middle; i >= 0; i-- {
fmt.Print(a[i], " ")
}
fmt.Println("")
}
}

func main() {
printTriangle(4)
}
</lang>
Output:
<pre>
1
1 1
1 2 1
1 3 3 1
</pre>

=={{header|Groovy}}==
=== Recursive ===
In the spirit of the Haskell "think in whole lists" solution here is a list-driven, minimalist solution:
<lang groovy>def pascal
pascal = { n -> (n <= 1) ? [1] : [[0] + pascal(n - 1), pascal(n - 1) + [0]].transpose().collect { it.sum() } }</lang>
However, this solution is horribly inefficient (O(''n''**2)). It slowly grinds to a halt on a reasonably powerful PC after about line 25 of the triangle.

Test program:
<lang groovy>def count = 15
(1..count).each { n ->
printf ("%2d:", n); (0..(count-n)).each { print " " }; pascal(n).each{ printf("%6d ", it) }; println ""
}</lang>

{{out}}
<pre> 1: 1
2: 1 1
3: 1 2 1
4: 1 3 3 1
5: 1 4 6 4 1
6: 1 5 10 10 5 1
7: 1 6 15 20 15 6 1
8: 1 7 21 35 35 21 7 1
9: 1 8 28 56 70 56 28 8 1
10: 1 9 36 84 126 126 84 36 9 1
11: 1 10 45 120 210 252 210 120 45 10 1
12: 1 11 55 165 330 462 462 330 165 55 11 1
13: 1 12 66 220 495 792 924 792 495 220 66 12 1
14: 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
15: 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 </pre>

=={{header|GW-BASIC}}==
<lang qbasic>10 INPUT "Number of rows? ",R
20 FOR I=0 TO R-1
30 C=1
40 FOR K=0 TO I
50 PRINT USING "####";C;
60 C=C*(I-K)/(K+1)
70 NEXT
80 PRINT
90 NEXT</lang>

Output:
<pre>
Number of rows? 7
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
</pre>

=={{header|Haskell}}==
An approach using the "think in whole lists" principle: Each row in
the triangle can be calculated from the previous row by adding a
shifted version of itself to it, keeping the ones at the ends. The
Prelude function ''zipWith'' can be used to add two lists, but it
won't keep the old values when one list is shorter. So we need a
similar function

<lang haskell>zapWith :: (a -> a -> a) -> [a] -> [a] -> [a]
zapWith f xs [] = xs
zapWith f [] ys = ys
zapWith f (x:xs) (y:ys) = f x y : zapWith f xs ys</lang>

Now we can shift a list and add it to itself, extending it by keeping
the ends:

<lang haskell>extendWith f [] = []
extendWith f xs@(x:ys) = x : zapWith f xs ys</lang>

And for the whole (infinite) triangle, we just iterate this operation,
starting with the first row:

<lang haskell>pascal = iterate (extendWith (+)) [1]</lang>

For the first ''n'' rows, we just take the first ''n'' elements from this
list, as in

<lang haskell>*Main> take 6 pascal
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]]</lang>

A shorter approach, plagiarized from [http://www.haskell.org/haskellwiki/Blow_your_mind]
<lang haskell>-- generate next row from current row
nextRow row = zipWith (+) ([0] ++ row) (row ++ [0])

-- returns the first n rows
pascal = iterate nextRow [1]</lang>


With binomial coefficients:
<lang haskell>fac = product . enumFromTo 1

binCoef n k = (fac n) `div` ((fac k) * (fac $ n - k))

pascal n = map (binCoef $ n - 1) [0..n-1]</lang>

Example:
<lang haskell>*Main> putStr $ unlines $ map unwords $ map (map show) $ pascal 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
</lang>

=={{header|HicEst}}==
<lang HicEst> CALL Pascal(30)

SUBROUTINE Pascal(rows)
CHARACTER fmt*6
WRITE(Text=fmt, Format='"i", i5.5') 1+rows/4

DO row = 0, rows-1
n = 1
DO k = 0, row
col = rows*(rows-row+2*k)/4
WRITE(Row=row+1, Column=col, F=fmt) n
n = n * (row - k) / (k + 1)
ENDDO
ENDDO
END</lang>
=={{header|Icon}} and {{header|Unicon}}==
The code below is slightly modified from the library version of pascal which prints 0's to the full width of the carpet.
It also presents the data as an isoceles triangle.
<lang Icon>link math
procedure main(A)
every n := !A do { # for each command line argument
n := integer(\n) | &null
pascal(n)
}
end
procedure pascal(n) #: Pascal triangle
/n := 16
write("width=", n, " height=", n) # carpet header
fw := *(2 ^ n)+1
every i := 0 to n - 1 do {
writes(repl(" ",fw*(n-i)/2))
every j := 0 to n - 1 do
writes(center(binocoef(i, j),fw) | break)
write()
}
end</lang>

{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/math.icn math provides binocoef]
[http://www.cs.arizona.edu/icon/library/src/procs/pascal.icn math provides the original version of pascal]

Sample output:<pre>->pascal 1 4 8
width=1 height=1
1
width=4 height=4
1
1 1
1 2 1
1 3 3 1
width=8 height=8
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
-></pre>

=={{header|IDL}}==
<lang IDL>Pro Pascal, n
;n is the number of lines of the triangle to be displayed
r=[1]
print, r
for i=0, (n-2) do begin
pascalrow,r
endfor
End

Pro PascalRow, r
for i=0,(n_elements(r)-2) do begin
r[i]=r[i]+r[i+1]
endfor
r= [1, r]
print, r

End</lang>

=={{header|J}}==
<lang j> !~/~ 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</lang>

<lang j> ([: ":@-.&0"1 !~/~)@i. 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1</lang>

<lang j> (-@|. |."_1 [: ":@-.&0"1 !~/~)@i. 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1</lang>

See the [[Talk:Pascal's_triangle#J_Explanation|talk page]] for explanation of earlier version

See also [[Pascal_matrix_generation#J|Pascal matrix generation]] and [[Sierpinski_triangle#J|Sierpinski triangle]].

=={{header|Java}}==
===Summing from Previous Rows===
{{works with|Java|1.5+}}
<lang java>import java.util.ArrayList;
...//class definition, etc.
public static void genPyrN(int rows){
if(rows < 0) return;
//save the last row here
ArrayList<Integer> last = new ArrayList<Integer>();
last.add(1);
System.out.println(last);
for(int i= 1;i <= rows;++i){
//work on the next row
ArrayList<Integer> thisRow= new ArrayList<Integer>();
thisRow.add(last.get(0)); //beginning
for(int j= 1;j < i;++j){//loop the number of elements in this row
//sum from the last row
thisRow.add(last.get(j - 1) + last.get(j));
}
thisRow.add(last.get(0)); //end
last= thisRow;//save this row
System.out.println(thisRow);
}
}</lang>

===Combinations===
This method is limited to 21 rows because of the limits of <tt>long</tt>. Calling <tt>pas</tt> with an argument of 22 or above will cause intermediate math to wrap around and give false answers.
<lang java>public class Pas{
public static void main(String[] args){
//usage
pas(20);
}

public static void pas(int rows){
for(int i = 0; i < rows; i++){
for(int j = 0; j <= i; j++){
System.out.print(ncr(i, j) + " ");
}
System.out.println();
}
}

public static long ncr(int n, int r){
return fact(n) / (fact(r) * fact(n - r));
}

public static long fact(int n){
long ans = 1;
for(int i = 2; i <= n; i++){
ans *= i;
}
return ans;
}
}</lang>

===Using arithmetic calculation of each row element ===
This method is limited to 30 rows because of the limits of integer calculations (probably when calculating the multiplication). If m is declared as long then 62 rows can be printed.
<lang java>
public class Pascal {
private static void printPascalLine (int n) {
if (n < 1)
return;
int m = 1;
System.out.print("1 ");
for (int j=1; j<n; j++) {
m = m * (n-j)/j;
System.out.print(m);
System.out.print(" ");
}
System.out.println();
}
public static void printPascal (int nRows) {
for(int i=1; i<=nRows; i++)
printPascalLine(i);
}
}
</lang>

=={{header|JavaScript}}==

===ES5===
====Imperative====

{{works with|SpiderMonkey}}
{{works with|V8}}
<lang javascript>// Pascal's triangle object
function pascalTriangle (rows) {

// Number of rows the triangle contains
this.rows = rows;

// The 2D array holding the rows of the triangle
this.triangle = new Array();
for (var r = 0; r < rows; r++) {
this.triangle[r] = new Array();
for (var i = 0; i <= r; i++) {
if (i == 0 || i == r)
this.triangle[r][i] = 1;
else
this.triangle[r][i] = this.triangle[r-1][i-1]+this.triangle[r-1][i];
}
}

// Method to print the triangle
this.print = function(base) {
if (!base)
base = 10;

// Private method to calculate digits in number
var digits = function(n,b) {
var d = 0;
while (n >= 1) {
d++;
n /= b;
}
return d;
}

// Calculate max spaces needed
var spacing = digits(this.triangle[this.rows-1][Math.round(this.rows/2)],base);

// Private method to add spacing between numbers
var insertSpaces = function(s) {
var buf = "";
while (s > 0) {
s--;
buf += " ";
}
return buf;
}

// Print the triangle line by line
for (var r = 0; r < this.triangle.length; r++) {
var l = "";
for (var s = 0; s < Math.round(this.rows-1-r); s++) {
l += insertSpaces(spacing);
}
for (var i = 0; i < this.triangle[r].length; i++) {
if (i != 0)
l += insertSpaces(spacing-Math.ceil(digits(this.triangle[r][i],base)/2));
l += this.triangle[r][i].toString(base);
if (i < this.triangle[r].length-1)
l += insertSpaces(spacing-Math.floor(digits(this.triangle[r][i],base)/2));
}
print(l);
}
}

}

// Display 4 row triangle in base 10
var tri = new pascalTriangle(4);
tri.print();
// Display 8 row triangle in base 16
tri = new pascalTriangle(8);
tri.print(16);</lang>
Output:
<pre>$ d8 pascal.js
1
1 1
1 2 1
1 3 3 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 a a 5 1
1 6 f 14 f 6 1
1 7 15 23 23 15 7 1
</pre>


====Functional====

{{Trans|Haskell}}

<lang JavaScript>(function (n) {
'use strict';

// A Pascal triangle of n rows

// pascal :: Int -> [[Int]]
function pascal(n) {
return range(1, n - 1)
.reduce(function (a) {
var lstPreviousRow = a.slice(-1)[0];

return a
.concat(
[zipWith(
function (a, b) {
return a + b
},
[0].concat(lstPreviousRow),
lstPreviousRow.concat(0)
)]
);
}, [[1]]);
}



// GENERIC FUNCTIONS

// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
function zipWith(f, xs, ys) {
return xs.length === ys.length ? (
xs.map(function (x, i) {
return f(x, ys[i]);
})
) : undefined;
}

// range :: Int -> Int -> [Int]
function range(m, n) {
return Array.apply(null, Array(n - m + 1))
.map(function (x, i) {
return m + i;
});
}

// TEST
var lstTriangle = pascal(n);


// FORMAT OUTPUT AS WIKI TABLE

// [[a]] -> bool -> s -> s
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (
strStyle ? 'style="' + strStyle + '"' : ''
) + lstRows.map(function (lstRow, iRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');

return '\n|-\n' + strDelim + ' ' + lstRow.map(function (
v) {
return typeof v === 'undefined' ? ' ' : v;
})
.join(' ' + strDelim + strDelim + ' ');
})
.join('') + '\n|}';
}

var lstLastLine = lstTriangle.slice(-1)[0],
lngBase = (lstLastLine.length * 2) - 1,
nWidth = lstLastLine.reduce(function (a, x) {
var d = x.toString()
.length;
return d > a ? d : a;
}, 1) * lngBase;

return [
wikiTable(
lstTriangle.map(function (lst) {
return lst.join(';;')
.split(';');
})
.map(function (line, i) {
var lstPad = Array((lngBase - line.length) / 2);
return lstPad.concat(line)
.concat(lstPad);
}),
false,
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
'em;table-layout:fixed;'
),

JSON.stringify(lstTriangle)
].join('\n\n');
})(7);</lang>

Output:

{| class="wikitable" style="text-align:center;width:26em;height:26em;table-layout:fixed;"
|-
| || || || || || || 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
|}

<lang JavaScript>[[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]]</lang>


===ES6===


<lang JavaScript>(() => {
'use strict';

// pascal :: Int -> [[Int]]
let pascal = n =>
range(1, n - 1)
.reduce(a => {
let lstPreviousRow = a.slice(-1)[0];

return a
.concat([zipWith((a, b) => a + b,
[0].concat(lstPreviousRow),
lstPreviousRow.concat(0)
)]);
}, [
[1]
]);

// GENERIC FUNCTIONS

// Int -> Int -> Maybe Int -> [Int]
let range = (m, n, step) => {
let d = (step || 1) * (n >= m ? 1 : -1);
return Array.from({
length: Math.floor((n - m) / d) + 1
}, (_, i) => m + (i * d));
},

// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith = (f, xs, ys) =>
xs.length === ys.length ? (
xs.map((x, i) => f(x, ys[i]))
) : undefined;

// TEST
return pascal(7)
.reduceRight((a, x) => {
let strIndent = a.indent;

return {
rows: strIndent + x
.map(n => (' ' + n).slice(-4))
.join('') + '\n' + a.rows,
indent: strIndent + ' '
};
}, {
rows: '',
indent: ''
}).rows;
})();</lang>

{{Out}}
<pre> 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
</pre>

=={{header|jq}}==
{{works with|jq|1.4}}
pascal(n) as defined here produces a stream of n arrays,
each corresponding to a row of the Pascal triangle.
The implementation avoids any arithmetic except addition.
<lang jq># pascal(n) for n>=0; pascal(0) emits an empty stream.
def pascal(n):
def _pascal: # input: the previous row
. as $in
| .,
if length >= n then empty
else
reduce range(0;length-1) as $i
([1]; . + [ $in[$i] + $in[$i + 1] ]) + [1] | _pascal
end;
if n <= 0 then empty else [1] | _pascal end ;</lang>
'''Example''':
pascal(5)
{{ Out }}
<lang sh>$ jq -c -n -f pascal_triangle.jq
[1]
[1,1]
[1,2,1]
[1,3,3,1]
[1,4,6,4,1]</lang>

'''Using recurse/1'''

Here is an equivalent implementation that uses the built-in filter, recurse/1, instead of the inner function.
<lang jq>def pascal(n):
if n <= 0 then empty
else [1]
| recurse( if length >= n then empty
else . as $in
| reduce range(0;length-1) as $i
([1]; . + [ $in[$i] + $in[$i + 1] ]) + [1]
end)
end;</lang>

=={{header|K}}==
<lang K>
pascal:{(x-1){+':x,0}\1}
pascal 6
(1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1)</lang>

=={{header|Kotlin}}==
<lang kotlin>fun pas(rows: Int) {
for (i in 0..rows - 1) {
for (j in 0..i)
print(ncr(i, j).toString() + " ")
println()
}
}

fun ncr(n: Int, r: Int) = fact(n) / (fact(r) * fact(n - r))

fun fact(n: Int) : Long {
var ans = 1.toLong()
for (i in 2..n)
ans *= i
return ans
}

fun main(args: Array<String>) = pas(args[0].toInt())</lang>

=={{header|Liberty BASIC}}==
<lang lb>input "How much rows would you like? "; n
dim a$(n)

for i= 0 to n
c = 1
o$ =""
for k =0 to i
o$ =o$ ; c; " "
c =c *(i-k)/(k+1)
next k
a$(i)=o$
next i

maxLen = len(a$(n))
for i= 0 to n
print space$((maxLen-len(a$(i)))/2);a$(i)
next i

end</lang>

=={{header|Locomotive Basic}}==

<lang locobasic>10 CLS
20 INPUT "Number of rows? ", rows:GOSUB 40
30 END
40 FOR i=0 TO rows-1
50 c=1
60 FOR k=0 TO i
70 PRINT USING "####";c;
80 c=c*(i-k)/(k+1)
90 NEXT
100 PRINT
110 NEXT
120 RETURN</lang>

Output:

<pre>
Number of rows? 7
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
</pre>

=={{header|Logo}}==
<lang logo>to pascal :n
if :n = 1 [output [1]]
localmake "a pascal :n-1
output (sentence first :a (map "sum butfirst :a butlast :a) last :a)
end

for [i 1 10] [print pascal :i]</lang>
=={{header|Lua}}==
<lang lua>
function nextrow(t)
local ret = {}
t[0], t[#t+1] = 0, 0
for i = 1, #t do ret[i] = t[i-1] + t[i] end
return ret
end

function triangle(n)
t = {1}
for i = 1, n do
print(unpack(t))
t = nextrow(t)
end
end
</lang>

=={{header|Maple}}==
<lang maple>f:=n->seq(print(seq(binomial(i,k),k=0..i)),i=0..n-1);

f(3);</lang>
1
1 1
1 2 1

=={{header|Mathematica}}==
<lang Mathematica>Column[StringReplace[ToString /@ Replace[MatrixExp[SparseArray[
{Band[{2,1}] -> Range[n-1]},{n,n}]],{x__,0..}->{x},2] ,{"{"|"}"|","->" "}], Center]</lang>
[[File:MmaPascal.png]]

=={{header|MATLAB}} / {{header|Octave}}==

A matrix containing the pascal triangle can be obtained this way:
<lang MATLAB>pascal(n);</lang>

<pre>>> pascal(6)
ans =

1 1 1 1 1 1
1 2 3 4 5 6
1 3 6 10 15 21
1 4 10 20 35 56
1 5 15 35 70 126
1 6 21 56 126 252

</pre>

The binomial coefficients can be extracted from the Pascal triangle in this way:
<lang MATLAB> binomCoeff = diag(rot90(pascal(n)))', </lang>

<pre>>> for k=1:6,diag(rot90(pascal(k)))', end
ans = 1
ans =

1 1

ans =

1 2 1

ans =

1 3 3 1

ans =

1 4 6 4 1

ans =

1 5 10 10 5 1

</pre>
Another way to get a formated pascals triangle is to use the convolution method:
<pre>>>
x = [1 1] ;
y = 1;
for k=8:-1:1
fprintf(['%', num2str(k), 'c'], zeros(1,3)),
fprintf('%6d', y), fprintf('\n')
y = conv(y,x);
end
</pre>
The result is:
<pre>>>

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
</pre>

=={{header|Maxima}}==
<lang maxima>sjoin(v, j) := apply(sconcat, rest(join(makelist(j, length(v)), v)))$

display_pascal_triangle(n) := for i from 0 thru 6 do disp(sjoin(makelist(binomial(i, j), j, 0, i), " "));

display_pascal_triangle(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" */</lang>


=={{header|Metafont}}==

(The formatting starts to be less clear when numbers start to have more than two digits)

<lang metafont>vardef bincoeff(expr n, k) =
save ?;
? := (1 for i=(max(k,n-k)+1) upto n: * i endfor )
/ (1 for i=2 upto min(k, n-k): * i endfor); ?
enddef;

def pascaltr expr c =
string s_;
for i := 0 upto (c-1):
s_ := "" for k=0 upto (c-i): & " " endfor;
s_ := s_ for k=0 upto i: & decimal(bincoeff(i,k))
& " " if bincoeff(i,k)<9: & " " fi endfor;
message s_;
endfor
enddef;

pascaltr(4);
end</lang>

=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary

numeric digits 1000 -- allow very large numbers
parse arg rows .
if rows = '' then rows = 11 -- default to 11 rows
printPascalTriangle(rows)
return

-- -----------------------------------------------------------------------------
method printPascalTriangle(rows = 11) public static
lines = ''
mx = (factorial(rows - 1) / factorial(rows % 2) / factorial(rows - 1 - rows % 2)).length() -- width of widest number

loop row = 1 to rows
n1 = 1.center(mx)
line = n1
loop col = 2 to row
n2 = col - 1
n1 = n1 * (row - n2) / n2
line = line n1.center(mx)
end col
lines[row] = line.strip()
end row

-- display triangle
ml = lines[rows].length() -- length of longest line
loop row = 1 to rows
say lines[row].centre(ml)
end row

return

-- -----------------------------------------------------------------------------
method factorial(n) public static
fac = 1
loop n_ = 2 to n
fac = fac * n_
end n_
return fac /*calc. factorial*/
</lang>
{{out}}
<pre>
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
1 10 45 120 210 252 210 120 45 10 1
</pre>

=={{header|Nial}}==
Like J

(pascal.nial)
<lang nial>factorial is recur [ 0 =, 1 first, pass, product, -1 +]
combination is fork [ > [first, second], 0 first,
/ [factorial second, * [factorial - [second, first], factorial first] ]
]
pascal is transpose each combination cart [pass, pass] tell</lang>
Using it
<lang nial>|loaddefs 'pascal.nial'
|pascal 5</lang>

=={{header|Nim}}==
<lang nim>import sequtils

proc pascal(n: int) =
var row = @[1]
for r in 1..n:
echo row
row = zip(row & @[0], @[0] & row).mapIt(int, it[0] + it[1])

pascal(10)</lang>

=={{header|OCaml}}==

<lang ocaml>(* generate next row from current row *)
let next_row row =
List.map2 (+) ([0] @ row) (row @ [0])

(* returns the first n rows *)
let pascal n =
let rec loop i row =
if i = n then []
else row :: loop (i+1) (next_row row)
in loop 0 [1]</lang>

=={{header|Octave}}==
<lang octave>function pascaltriangle(h)
for i = 0:h-1
for k = 0:h-i
printf(" ");
endfor
for j = 0:i
printf("%3d ", bincoeff(i, j));
endfor
printf("\n");
endfor
endfunction

pascaltriangle(4);</lang>

=={{header|Oforth}}==

No result if n <= 0

<lang Oforth>: pascal(n) [ 1 ] #[ dup println dup 0 + 0 rot + zipWith(#+) ] times(n) drop ;</lang>

{{out}}
<pre>
10 pascal
[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]
</pre>

=={{header|Oz}}==
<lang oz>declare
fun {NextLine Xs}
{List.zip 0|Xs {Append Xs [0]}
fun {$ Left Right}
Left + Right
end}
end

fun {Triangle N}
{List.take {Iterate [1] NextLine} N}
end

fun lazy {Iterate I F}
I|{Iterate {F I} F}
end

%% Only works nicely for N =< 5.
proc {PrintTriangle T}
N = {Length T}
in
for
Line in T
Indent in N-1..0;~1
do
for _ in 1..Indent do {System.printInfo " "} end
for L in Line do {System.printInfo L#" "} end
{System.printInfo "\n"}
end
end
in
{PrintTriangle {Triangle 5}}</lang>

For n = 0, prints nothing. For negative n, throws an exception.
=={{header|PARI/GP}}==
<lang parigp>pascals_triangle(N)= {
my(row=[],prevrow=[]);
for(x=1,N,
if(x>5,break(1));
row=eval(Vec(Str(11^(x-1))));
print(row));
prevrow=row;
for(y=6,N,
for(p=2,#prevrow,
row[p]=prevrow[p-1]+prevrow[p]);
row=concat(row,1);
prevrow=row;
print(row);
);
}</lang>

=={{header|Pascal}}==
<lang pascal>Program PascalsTriangle(output);

procedure Pascal(r : Integer);
var
i, c, k : Integer;
begin
for i := 0 to r-1 do
begin
c := 1;
for k := 0 to i do
begin
write(c:3);
c := (c * (i-k)) div (k+1);
end;
writeln;
end;
end;
begin
Pascal(9)
end.</lang>
Output:
<pre>% ./PascalsTriangle
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
</pre>

=={{header|Perl}}==
These functions perform as requested in the task: they print out the first ''n'' lines. If ''n'' <= 0, they print nothing. The output is simple (no fancy formatting).
<lang perl>sub pascal {
my $rows = shift;
my @next = (1);
for my $n (1 .. $rows) {
print "@next\n";
@next = (1, (map $next[$_]+$next[$_+1], 0 .. $n-2), 1);
}
}</lang>

If you want more than 68 rows, then use either "use bigint" or "use Math::GMP qw/:constant/" inside the function to enable bigints. We can also use a binomial function which will expand to bigints if many rows are requested:
{{libheader|ntheory}}
<lang perl>use ntheory qw/binomial/;
sub pascal {
my $rows = shift;
for my $n (0 .. $rows-1) {
print join(" ", map { binomial($n,$_) } 0 .. $n), "\n";
}
}</lang>

Here is a non-obvious version using bignum, which is limited to the first 23 rows because of the algorithm used:
<lang perl>use bignum;
sub pascal_line { $_[0] ? unpack "A(A6)*", 1000001**$_[0] : 1 }
sub pascal { print "@{[map -+-$_, pascal_line $_]}\n" for 0..$_[0]-1 }</lang>

=={{header|Perl 6}}==
{{works with|rakudo|2015-10-03}}
=== using a lazy sequence generator ===

The following routine returns a lazy list of lines using the sequence operator (<tt>...</tt>). With a lazy result you need not tell the routine how many you want; you can just use a slice subscript to get the first N lines:
<lang perl6>sub pascal {
[1], { [0, |$_ Z+ |$_, 0] } ... *
}
.say for pascal[^10];</lang>

One problem with the routine above is that it might recalculate the sequence each time you call it. Slightly more idiomatic would be to define the sequence as a lazy constant. Here we use the <tt>@</tt> sigil to indicate that the sequence should cache its values for reuse, and use an explicit parameter <tt>$prev</tt> for variety:

<lang perl6>constant @pascal = [1], -> $prev { [0, |$prev Z+ |$prev, 0] } ... *;
.say for @pascal[^10];</lang>

Since we use ordinary subscripting, non-positive inputs throw an index-out-of-bounds error.

=== recursive ===

{{trans|Haskell}}

<lang perl6>multi sub pascal (1) { $[1] }
multi sub pascal (Int $n where 2..*) {
my @rows = pascal $n - 1;
|@rows, [0, |@rows[*-1] Z+ |@rows[*-1], 0 ];
}
.say for pascal 10;</lang>

Non-positive inputs throw a multiple-dispatch error.

=== iterative ===

{{trans|Perl}}
<lang perl6>sub pascal ($n where $n >= 1) {
say my @last = 1;
for 1 .. $n - 1 -> $row {
@last = 1, |map({ @last[$_] + @last[$_ + 1] }, 0 .. $row - 2), 1;
say @last;
}
}
pascal 10;</lang>

Non-positive inputs throw a type check error.

{{Output}}
<pre>[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]</pre>

=={{header|Phix}}==
<lang Phix>sequence row = {}
for m = 1 to 13 do
row = row & 1
for n=length(row)-1 to 2 by -1 do
row[n] += row[n-1]
end for
printf(1,repeat(' ',(13-m)*2))
for i=1 to length(row) do
printf(1," %3d",row[i])
end for
puts(1,'\n')
end for</lang>
{{out}}
<pre style="font-size: 8px">
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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
</pre>

"Reflected" Pascal's triangle, it uses symmetry property to "mirror" second part. It determines even and odd strings. automatically.
=={{header|PHP}}==
<lang php>
<?php
//Author Ivan Gavryshin @dcc0
function tre($n) {
$ck=1;
$kn=$n+1;
if($kn%2==0) {
$kn=$kn/2;
$i=0;
}
else
{

$kn+=1;
$kn=$kn/2;
$i= 1;
}

for ($k = 1; $k <= $kn-1; $k++) {
$ck = $ck/$k*($n-$k+1);
$arr[] = $ck;
echo "+" . $ck ;
}

if ($kn>1) {
echo $arr[i];
$arr=array_reverse($arr);
for ($i; $i<= $kn-1; $i++) {
echo "+" . $arr[$i] ;
}
}
}
//set amount of strings here
while ($n<=20) {
++$n;
echo tre($n);
echo "<br/>";
}
?>
</lang>

=={{header|PHP}}==
<lang php>function pascalsTriangle($num){
$c = 1;
$triangle = Array();
for($i=0;$i<=$num;$i++){
$triangle[$i] = Array();
if(!isset($triangle[$i-1])){
$triangle[$i][] = $c;
}else{
for($j=0;$j<count($triangle[$i-1])+1;$j++){
$triangle[$i][] = (isset($triangle[$i-1][$j-1]) && isset($triangle[$i-1][$j])) ? $triangle[$i-1][$j-1] + $triangle[$i-1][$j] : $c;
}
}
}
return $triangle;
}

$tria = pascalsTriangle(8);
foreach($tria as $val){
foreach($val as $value){
echo $value . ' ';
}
echo '<br>';
}</lang>
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

=={{header|PL/I}}==
<lang PL/I>
declare (t, u)(40) fixed binary;
declare (i, n) fixed binary;

t,u = 0;
get (n);
if n <= 0 then return;

do n = 1 to n;
u(1) = 1;
do i = 1 to n;
u(i+1) = t(i) + t(i+1);
end;
put skip edit ((u(i) do i = 1 to n)) (col(40-2*n), (n+1) f(4));
t = u;
end;
</lang>

<lang>
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
1 10 45 120 210 252 210 120 45 10 1
</lang>

=={{header|PicoLisp}}==
{{trans|C}}
<lang PicoLisp>(de pascalTriangle (N)
(for I N
(space (* 2 (- N I)))
(let C 1
(for K I
(prin (align 3 C) " ")
(setq C (*/ C (- I K) K)) ) )
(prinl) ) )</lang>

=={{header|Potion}}==
<lang potion>printpascal = (n) :
if (n < 1) :
1 print
(1)
. else :
prev = printpascal(n - 1)
prev append(0)
curr = (1)
n times (i):
curr append(prev(i) + prev(i + 1))
.
"\n" print
curr join(", ") print
curr
.
.

printpascal(read number integer)</lang>

=={{header|PowerShell}}==
<lang powershell>
$Infinity = 1
$NewNumbers = $null
$Numbers = $null
$Result = $null
$Number = $null
$Power = $args[0]

Write-Host $Power

For(
$i=0;
$i -lt $Infinity;
$i++
)
{
$Numbers = New-Object Object[] 1
$Numbers[0] = $Power
For(
$k=0;
$k -lt $NewNumbers.Length;
$k++
)
{
$Numbers = $Numbers + $NewNumbers[$k]
}
If(
$i -eq 0
)
{
$Numbers = $Numbers + $Power
}
$NewNumbers = New-Object Object[] 0
Try
{
For(
$j=0;
$j -lt $Numbers.Length;
$j++
)
{
$Result = $Numbers[$j] + $Numbers[$j+1]
$NewNumbers = $NewNumbers + $Result
}
}
Catch [System.Management.Automation.RuntimeException]
{
Write-Warning "Value was too large for a Decimal. Script aborted."
Break;
}
Foreach(
$Number in $Numbers
)
{
If(
$Number.ToString() -eq "+unendlich"
)
{
Write-Warning "Value was too large for a Decimal. Script aborted."
Exit
}
}
Write-Host $Numbers
$Infinity++
}
</lang>

Save the above code to a .ps1 script file and start it by calling its name and providing N.

<pre>
PS C:\> & '.\Pascals Triangle.ps1' 1

----

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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
</pre>

=={{header|Prolog}}==
Difference-lists are used to make quick append.
<lang Prolog>pascal(N) :-
pascal(1, N, [1], [[1]|X]-X, L),
maplist(my_format, L).

pascal(Max, Max, L, LC, LF) :-
!,
make_new_line(L, NL),
append_dl(LC, [NL|X]-X, LF-[]).

pascal(N, Max, L, NC, LF) :-
build_new_line(L, NL),
append_dl(NC, [NL|X]-X, NC1),
N1 is N+1,
pascal(N1, Max, NL, NC1, LF).

build_new_line(L, R) :-
build(L, 0, X-X, R).

build([], V, RC, RF) :-
append_dl(RC, [V|Y]-Y, RF-[]).

build([H|T], V, RC, R) :-
V1 is V+H,
append_dl(RC, [V1|Y]-Y, RC1),
build(T, H, RC1, R).

append_dl(X1-X2, X2-X3, X1-X3).

% to have a correct output !
my_format([H|T]) :-
write(H),
maplist(my_writef, T),
nl.

my_writef(X) :-
writef(' %5r', [X]).
</lang>

Output :
<lang Prolog> ?- pascal(15).
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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
true.
</lang>
=={{header|PureBasic}}==

<lang PureBasic>Procedure pascaltriangle( n.i)
For i= 0 To n
c = 1
For k=0 To i
Print(Str( c)+" ")
c = c * (i-k)/(k+1);
Next ;k
PrintN(" "); nächste zeile
Next ;i

EndProcedure
OpenConsole()
Parameter.i = Val(ProgramParameter(0))
pascaltriangle(Parameter);
Input()</lang>

=={{header|Python}}==
<lang python>def pascal(n):
"""Prints out n rows of Pascal's triangle.
It returns False for failure and True for success."""
row = [1]
k = [0]
for x in range(max(n,0)):
print row
row=[l+r for l,r in zip(row+k,k+row)]
return n>=1</lang>

Or, by creating a scan function:
<lang python>def scan(op, seq, it):
a = []
result = it
a.append(it)
for x in seq:
result = op(result, x)
a.append(result)
return a

def pascal(n):
def nextrow(row, x):
return [l+r for l,r in zip(row+[0,],[0,]+row)]

return scan(nextrow, range(n-1), [1,])

for row in pascal(4):
print(row)</lang>

=={{header|q}}==
<lang q>
pascal:{(x-1){0+':x,0}\1}
pascal 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
</lang>

=={{header|Qi}}==
{{trans|Haskell}}
<lang Qi>
(define iterate
_ _ 0 -> []
F V N -> [V|(iterate F (F V) (1- N))])

(define next-row
R -> (MAPCAR + [0|R] (append R [0])))

(define pascal
N -> (iterate next-row [1] N))
</lang>

=={{header|R}}==
{{trans|Octave}}
<lang R>pascalTriangle <- function(h) {
for(i in 0:(h-1)) {
s <- ""
for(k in 0:(h-i)) s <- paste(s, " ", sep="")
for(j in 0:i) {
s <- paste(s, sprintf("%3d ", choose(i, j)), sep="")
}
print(s)
}
}</lang>

Here's an R version:

<lang R>pascalTriangle <- function(h) {
lapply(0:h, function(i) choose(i, 0:i))
}</lang>

=={{header|Racket}}==

Iterative version by summing rows up to <math>n</math>.

<lang Racket>#lang racket

(define (pascal n)
(define (next-row current-row)
(map + (cons 0 current-row)
(append current-row '(0))))
(let-values
([(previous-rows final-row)
(for/fold ([triangle null]
[row '(1)])
([row-number (in-range 1 n)])
(values (cons row triangle)
(next-row row)))])
(reverse (cons final-row previous-rows))))

</lang>

=={{header|RapidQ}}==
===Summing from Previous Rows===
{{trans|BASIC}}

The main difference to BASIC implementation is the output formatting.
RapidQ does not support PRINT USING. Instead, function FORMAT$() is used.
TAB() is not supported, so SPACE$() was used instead.

Another difference is that in RapidQ, DIM does not clear array values to zero.
But if dimensioning is done with DEF..., you can give the initial values in curly braces.
If less values are given than there are elements in the array, the remaining positions are initialized to zero.
RapidQ does not require simple variables to be declared before use.

<lang rapidq>DEFINT values(100) = {0,1}

INPUT "Number of rows: "; nrows
PRINT SPACE$((nrows)*3);" 1"
FOR row = 2 TO nrows
PRINT SPACE$((nrows-row)*3+1);
FOR i = row TO 1 STEP -1
values(i) = values(i) + values(i-1)
PRINT FORMAT$("%5d ", values(i));
NEXT i
PRINT
NEXT row</lang>

===Using binary coefficients===
{{trans|BASIC}}
<lang rapidq>INPUT "Number of rows: "; nrows
FOR row = 0 TO nrows-1
c = 1
PRINT SPACE$((nrows-row)*3);
FOR i = 0 TO row
PRINT FORMAT$("%5d ", c);
c = c * (row - i) / (i+1)
NEXT i
PRINT
NEXT row</lang>

=={{header|Retro}}==
<lang Retro>2 elements i j
: pascalTriangle
cr dup
[ dup !j 1 swap 1+ [ !i dup putn space @j @i - * @i 1+ / ] iter cr drop ] iter drop
;
13 pascalTriangle</lang>

=={{header|REXX}}==
There is no practical limit for this REXX version, triangles up to 46 rows have been
generated (without wrapping) in a screen window with a width of 620 characters.

If the number (of rows) specified is negative, the output is written to a (disk) file
instead. &nbsp; Triangles with over a 1,000 rows have easily been created. &nbsp;
The output file created (that is written to disk) is named &nbsp; &nbsp; '''PASCALS.n'''
&nbsp; &nbsp; where &nbsp; '''n''' &nbsp; is the absolute value of the number entered.


Note: &nbsp; Pascal's triangle is also known as:
:::* &nbsp; Khayyam's triangle
:::* &nbsp; Khayyam─Pascal's triangle
:::* &nbsp; Tartaglia's triangle
:::* &nbsp; Yang Hui's triangle
<lang rexx>/*REXX program displays (or writes to a file) Pascal's triangle (centered/formatted).*/
numeric digits 3000 /*be able to handle gihugeic triangles.*/
parse arg nn . /*obtain the optional argument from CL.*/
if nn=='' | nn=="," then nn=10 /*Not specified? Then use the default.*/
N=abs(nn) /*N is the number of rows in triangle.*/
w=length( !(N-1) / !(N%2) / !(N-1-N%2) ) /*W: the width of the biggest integer.*/
@.=1; $.=@.; unity=right(1, w) /*defaults rows & lines; aligned unity.*/
/* [↓] build rows of Pascals' triangle*/
do r=1 for N; rm=r-1 /*Note: the first column is always 1.*/
do c=2 to rm; cm=c-1 /*build the rest of the columns in row.*/
@.r.c= @.rm.cm + @.rm.c /*assign value to a specific row & col.*/
$.r = $.r right(@.r.c, w) /*and construct a line for output (row)*/
end /*c*/ /* [↑] C is the column being built.*/
if r\==1 then $.r=$.r unity /*for rows≥2, append a trailing "1".*/
end /*r*/ /* [↑] R is the row being built.*/
/* [↑] WIDTH: for nicely looking line.*/
width=length($.N) /*width of the last (output) line (row)*/
/*if NN<0, output is written to a file.*/
do r=1 for N; $$=center($.r, width) /*center this particular Pascals' row. */
if nn>0 then say $$ /*SAY if NN is positive, else */
else call lineout 'PASCALS.'n, $$ /*write this Pascal's row ───► a file.*/
end /*r*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; !=1; do j=2 to arg(1); !=!*j; end /*j*/; return ! /*compute factorial*/</lang>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 11 </tt>
<pre>
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
1 10 45 120 210 252 210 120 45 10 1
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 22 </tt>
<pre>
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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1
1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1
1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1
1 20 190 1140 4845 15504 38760 77520 125970 167960 184756 167960 125970 77520 38760 15504 4845 1140 190 20 1
1 21 210 1330 5985 20349 54264 116280 203490 293930 352716 352716 293930 203490 116280 54264 20349 5985 1330 210 21 1
</pre>

=={{header|Ring}}==
<lang ring>
row = 5
for i = 0 to row - 1
col = 1
see left(" ",row-i)
for k = 0 to i
see "" + col + " "
col = col*(i-k)/(k+1)
next
see nl
next
</lang>
Output:
<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
</pre>

=={{header|Ruby}}==
<lang ruby>def pascal(n)
raise ArgumentError, "must be positive." if n < 1
yield ar = [1]
(n-1).times do
ar.unshift(0).push(0) # tack a zero on both ends
yield ar = ar.each_cons(2).map{|a, b| a + b }
end
end
pascal(8){|row| puts row.join(" ").center(20)}</lang>
{{out}}
<pre>
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
</pre>

Or for more or less a translation of the two line Haskell version (with inject being abused a bit I know):

<lang ruby>def next_row(row) ([0] + row).zip(row + [0]).collect {|l,r| l + r } end

def pascal(n) n.times.inject([1]) {|x,_| next_row x } end

8.times{|i| p pascal(i)}</lang>
{{out}}
<pre>
[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]
</pre>

=={{header|Run BASIC}}==
<lang runbasic>input "number of rows? ";r
for i = 0 to r - 1
c = 1
print left$(" ",(r*2)-(i*2));
for k = 0 to i
print using("####",c);
c = c*(i-k)/(k+1)
next
print
next</lang>Output:
<pre>Number of rows? ?5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1</pre>

=={{header|Scala}}==
Simple recursive row definition:
<lang scala>
def tri(row:Int):List[Int] = { row match {
case 1 => List(1)
case n:Int => List(1) ::: ((tri(n-1) zip tri(n-1).tail) map {case (a,b) => a+b}) ::: List(1)
}
}
</lang>
Function to pretty print n rows:
<lang scala>
def prettytri(n:Int) = (1 to n) foreach {i=>print(" "*(n-i)); tri(i) map (c=>print(c+" ")); println}
prettytri(5)
</lang>
Outputs:
<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
</pre>

=={{header|Scheme}}==
{{Works with|Scheme|R<math>^5</math>RS}}
<lang scheme>(define (next-row row)
(map + (cons 0 row) (append row '(0))))
(define (triangle row rows)
(if (= rows 0)
'()
(cons row (triangle (next-row row) (- rows 1)))))

(triangle (list 1) 5)
</lang>
Output:
<lang>((1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1))</lang>

=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";

const proc: main is func
local
var integer: numRows is 0;
var array integer: values is [] (0, 1);
var integer: row is 0;
var integer: index is 0;
begin
write("Number of rows: ");
readln(numRows);
writeln("1" lpad succ(numRows) * 3);
for row range 2 to numRows do
write("" lpad (numRows - row) * 3);
values &:= [] 0;
for index range succ(row) downto 2 do
values[index] +:= values[pred(index)];
write(" " <& values[index] lpad 5);
end for;
writeln;
end for;
end func;</lang>

=={{header|Sidef}}==
<lang ruby>func pascal(rows) {
var row = [1];
{ | n|
say row.join(' ');
row = [1, 0..(n-2).map {|i| row[i] + row[i+1] }..., 1];
} * rows;
}

pascal(10);</lang>

=={{header|Tcl}}==
===Summing from Previous Rows===
<lang tcl>proc pascal_iterative n {
if {$n < 1} {error "undefined behaviour for n < 1"}
set row [list 1]
lappend rows $row
set i 1
while {[incr i] <= $n} {
set prev $row
set row [list 1]
for {set j 1} {$j < [llength $prev]} {incr j} {
lappend row [expr {[lindex $prev [expr {$j - 1}]] + [lindex $prev $j]}]
}
lappend row 1
lappend rows $row
}
return $rows
}

puts [join [pascal_iterative 6] \n]</lang>
<pre>1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1</pre>
===Using binary coefficients===
{{trans|BASIC}}
<lang tcl>proc pascal_coefficients n {
if {$n < 1} {error "undefined behaviour for n < 1"}
for {set i 0} {$i < $n} {incr i} {
set c 1
set row [list $c]
for {set j 0} {$j < $i} {incr j} {
set c [expr {$c * ($i - $j) / ($j + 1)}]
lappend row $c
}
lappend rows $row
}
return $rows
}

puts [join [pascal_coefficients 6] \n]</lang>
===Combinations===
{{trans|Java}}
Thanks to Tcl 8.5's arbitrary precision integer arithmetic, this solution is not limited to a couple of dozen rows. Uses a caching factorial calculator to improve performance.
<lang tcl>package require Tcl 8.5

proc pascal_combinations n {
if {$n < 1} {error "undefined behaviour for n < 1"}
for {set i 0} {$i < $n} {incr i} {
set row [list]
for {set j 0} {$j <= $i} {incr j} {
lappend row [C $i $j]
}
lappend rows $row
}
return $rows
}

proc C {n k} {
expr {[ifact $n] / ([ifact $k] * [ifact [expr {$n - $k}]])}
}

set fact_cache {1 1}
proc ifact n {
global fact_cache
if {$n < [llength $fact_cache]} {
return [lindex $fact_cache $n]
}
set i [expr {[llength $fact_cache] - 1}]
set sum [lindex $fact_cache $i]
while {$i < $n} {
incr i
set sum [expr {$sum * $i}]
lappend fact_cache $sum
}
return $sum
}

puts [join [pascal_combinations 6] \n]</lang>

===Comparing Performance===
<lang tcl>set n 100
puts "calculate $n rows:"
foreach proc {pascal_iterative pascal_coefficients pascal_combinations} {
puts "$proc: [time [list $proc $n] 100]"
}</lang>
{{Out}}
<pre>calculate 100 rows:
pascal_iterative: 2800.14 microseconds per iteration
pascal_coefficients: 8760.98 microseconds per iteration
pascal_combinations: 38176.66 microseconds per iteration</pre>

=={{header|TI-83 BASIC}}==
===Using Addition of Previous Rows===
<lang ti83b>PROGRAM:PASCALTR
:Lbl IN
:ClrHome
:Disp "NUMBER OF ROWS"
:Input N
:If N < 1:Goto IN
:{N,N}→dim([A])
:"CHEATING TO MAKE IT FASTER"
:For(I,1,N)
:1→[A](1,1)
:End
:For(I,2,N)
:For(J,2,I)
:[A](I-1,J-1)+[A](I-1,J)→[A](I,J)
:End
:End
:[A]</lang>
===Using nCr Function===
<lang ti83b>PROGRAM:PASCALTR
:Lbl IN
:ClrHome
:Disp "NUMBER OF ROWS"
:Input N
:If N < 1:Goto IN
:{N,N}→dim([A])
:For(I,2,N)
:For(J,2,I)
:(I-1) nCr (J-1)→[A](I,J)
:End
:End
:[A]</lang>
=={{header|Turing}}==

<lang turing>
procedure pascal (n : int)
for i : 0 .. n
var c : int
c := 1
for k : 0 .. i
put intstr(c) + " " ..
c := c * (i - k) div (k + 1)
end for
put ""
end for
end pascal

pascal(5)</lang>

=={{header|uBasic/4tH}}==
<lang>Input "Number Of Rows: "; N
@(1) = 1
Print Tab((N+1)*3);"1"

For R = 2 To N
Print Tab((N-R)*3+1);
For I = R To 1 Step -1
@(I) = @(I) + @(I-1)
Print Using "______";@(i);
Next
Next

Print
End</lang>
Output:
<pre>Number Of Rows: 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

0 OK, 0:380
</pre>
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
Any n <= 1 will print the "1" row.
<lang bash>#! /bin/bash
pascal() {
local -i n=${1:-1}
if (( n <= 1 )); then
echo 1
else
local output=$( $FUNCNAME $((n - 1)) )
set -- $( tail -n 1 <<<"$output" ) # previous row
echo "$output"
printf "1 "
while [[ -n $1 ]]; do
printf "%d " $(( $1 + ${2:-0} ))
shift
done
echo
fi
}
pascal "$1"</lang>

=={{header|Ursala}}==
Zero maps to the empty list. Negatives are inexpressible.
This solution uses a library function for binomial coefficients.
<lang Ursala>#import std
#import nat

pascal = choose**ziDS+ iota*t+ iota+ successor</lang>
This solution uses direct summation. The algorithm is to
insert zero at the head of a list (initially the unit list <1>), zip it with its reversal,
map the sum over the list of pairs, iterate n times, and return the trace.
<lang Ursala>#import std
#import nat

pascal "n" = (next"n" sum*NiCixp) <1></lang>
test program:
<lang Ursala>#cast %nLL

example = pascal 10</lang>
{{Out}}
<pre><
<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>></pre>

=={{header|VBScript}}==
Derived from the BASIC version.
<lang vb>Pascal_Triangle(WScript.Arguments(0))
Function Pascal_Triangle(n)
Dim values(100)
values(1) = 1
WScript.StdOut.Write values(1)
WScript.StdOut.WriteLine
For row = 2 To n
For i = row To 1 Step -1
values(i) = values(i) + values(i-1)
WScript.StdOut.Write values(i) & " "
Next
WScript.StdOut.WriteLine
Next
End Function</lang>
{{out}}
Invoke from a command line.
<pre>
F:\VBScript>cscript /nologo rosettacode-pascals_triangle.vbs 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
</pre>


=={{header|Vedit macro language}}==
===Summing from Previous Rows===
{{trans|BASIC}}

Vedit macro language does not have actual arrays (edit buffers are normally used for storing larger amounts of data).
However, a numeric register can be used as index to access another numeric register.
For example, if #99 contains value 2, then #@99 accesses contents of numeric register #2.

<lang vedit>#100 = Get_Num("Number of rows: ", STATLINE)
#0=0; #1=1
Ins_Char(' ', COUNT, #100*3-2) Num_Ins(1)
for (#99 = 2; #99 <= #100; #99++) {
Ins_Char(' ', COUNT, (#100-#99)*3)
#@99 = 0
for (#98 = #99; #98 > 0; #98--) {
#97 = #98-1
#@98 += #@97
Num_Ins(#@98, COUNT, 6)
}
Ins_Newline
}</lang>

===Using binary coefficients===
{{trans|BASIC}}
<lang vedit>#1 = Get_Num("Number of rows: ", STATLINE)
for (#2 = 0; #2 < #1; #2++) {
#3 = 1
Ins_Char(' ', COUNT, (#1-#2-1)*3)
for (#4 = 0; #4 <= #2; #4++) {
Num_Ins(#3, COUNT, 6)
#3 = #3 * (#2-#4) / (#4+1)
}
}
Ins_Newline
}</lang>


=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
<lang vb>Sub pascaltriangle()
'Pascal's triangle
Const m = 11
Dim t(40) As Integer, u(40) As Integer
Dim i As Integer, n As Integer, s As String, ss As String
ss = ""
For n = 1 To m
u(1) = 1
s = ""
For i = 1 To n
u(i + 1) = t(i) + t(i + 1)
s = s & u(i) & " "
t(i) = u(i)
Next i
ss = ss & s & vbCrLf
Next n
MsgBox ss, , "Pascal's triangle"
End Sub 'pascaltriangle</lang>
{{out}}
<pre>
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
1 10 45 120 210 252 210 120 45 10 1
</pre>

=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codes;

proc Pascal(N); \Display the first N rows of Pascal's triangle
int N; \if N<=0 then nothing is displayed
int Row, I, Old(40), New(40);
[for Row:= 0 to N-1 do
[New(0):= 1;
for I:= 1 to Row do New(I):= Old(I-1) + Old(I);
for I:= 1 to (N-Row-1)*2 do ChOut(0, ^ );
for I:= 0 to Row do
[if New(I)<100 then ChOut(0, ^ );
IntOut(0, New(I));
if New(I)<10 then ChOut(0, ^ );
ChOut(0, ^ );
];
New(Row+1):= 0;
I:= Old; Old:= New; New:= I;
CrLf(0);
];
];

Pascal(13)</lang>

{{Out}}
<pre>
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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
</pre>

=={{header|zkl}}==
{{trans|C}}
<lang zkl>fcn pascalTriangle(n){ // n<=0-->""
foreach i in (n){
c := 1;
print(" "*(2*(n-1-i)));
foreach k in (i+1){
print("%3d ".fmt(c));
c = c * (i-k)/(k+1);
}
println();
}
}
}
let waste = pascal(n:10)
pascalTriangle(8);</lang>
{{out}}
<pre>
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
</pre>

Revision as of 18:46, 15 January 2017

func pascal(n:Int)->[Int]{

   if n==1{
       let a=[1]
       print(a)
       return a
   }
   else{
       var a=pascal(n:n-1)
       var temp=a
       for i in 0..<a.count{
           if i+1==a.count{
               temp.append(1)
               break
           }
           temp[i+1] = a[i]+a[i+1]
       }
       a=temp
       print(a)
       return a
   }

} let waste = pascal(n:10)