Catalan numbers/Pascal's triangle: Difference between revisions

Content added Content deleted
No edit summary
(rearranges in order of the language.)
Line 256: Line 256:
{{out}}
{{out}}
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 </pre>
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 </pre>

=={{header|Icon}} and {{header|Unicon}}==

The following works in both languages. It avoids computing elements in Pascal's triangle
that aren't used.

<lang unicon>link math

procedure main(A)
limit := (integer(A[1])|15)+1
every write(right(binocoef(i := 2*seq(0)\limit,i/2)-binocoef(i,i/2+1),30))
end</lang>

Sample run:
<pre>
->cn
1
2
5
14
42
132
429
1430
4862
16796
58786
208012
742900
2674440
9694845
->
</pre>


=={{header|ERRE}}==
=={{header|ERRE}}==
Line 349: Line 316:
15 9694845
15 9694845
</pre>
</pre>

=={{header|Icon}} and {{header|Unicon}}==

The following works in both languages. It avoids computing elements in Pascal's triangle
that aren't used.

<lang unicon>link math

procedure main(A)
limit := (integer(A[1])|15)+1
every write(right(binocoef(i := 2*seq(0)\limit,i/2)-binocoef(i,i/2+1),30))
end</lang>

Sample run:
<pre>
->cn
1
2
5
14
42
132
429
1430
4862
16796
58786
208012
742900
2674440
9694845
->
</pre>

=={{header|J}}==
=={{header|J}}==


Line 484: Line 485:
[1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]
[1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]
</pre>
</pre>

=={{header|PARI/GP}}==
<lang parigp>vector(15,n,binomial(2*n,n)-binomial(2*n,n+1))</lang>
{{out}}
<pre>%1 = [1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]</pre>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 538: Line 544:


</pre>
</pre>

=={{header|PARI/GP}}==
<lang parigp>vector(15,n,binomial(2*n,n)-binomial(2*n,n+1))</lang>
{{out}}
<pre>%1 = [1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845]</pre>


=={{header|Perl}}==
=={{header|Perl}}==
Line 736: Line 737:
if x-y<y then y=x-y; _=1; do j=x-y+1 to x; _=_*j; end; return _/!(y)</lang>
if x-y<y then y=x-y; _=1; do j=x-y+1 to x; _=_*j; end; return _/!(y)</lang>
'''output''' is the same as the 1<sup>st</sup> version. <br>
'''output''' is the same as the 1<sup>st</sup> version. <br>

=={{header|Run BASIC}}==
<lang runbasic>n = 15
dim t(n+2)
t(1) = 1
for i = 1 to n
for j = i to 1 step -1 : t(j) = t(j) + t(j-1): next j
t(i+1) = t(i)
for j = i+1 to 1 step -1: t(j) = t(j) + t(j-1 : next j
print t(i+1) - t(i);" ";
next i</lang>
{{out}}
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 </pre>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 765: Line 753:
<pre>1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845</pre>
<pre>1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845</pre>


=={{header|VBScript}}==
=={{header|Run BASIC}}==
<lang runbasic>n = 15
To run in console mode with cscript.
<lang vbscript>dim t()
dim t(n+2)
t(1) = 1
if Wscript.arguments.count=1 then
for i = 1 to n
n=Wscript.arguments.item(0)
for j = i to 1 step -1 : t(j) = t(j) + t(j-1): next j
else
t(i+1) = t(i)
n=15
for j = i+1 to 1 step -1: t(j) = t(j) + t(j-1 : next j
end if
redim t(n+1)
print t(i+1) - t(i);" ";
next i</lang>
't(*)=0
{{out}}
t(1)=1
<pre>1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900 2674440 9694845 </pre>
for i=1 to n
ip=i+1
for j = i to 1 step -1
t(j)=t(j)+t(j-1)
next 'j
t(i+1)=t(i)
for j = i+1 to 1 step -1
t(j)=t(j)+t(j-1)
next 'j
Wscript.echo t(i+1)-t(i)
next 'i</lang>


=={{header|Scilab}}==
=={{header|Scilab}}==
Line 896: Line 874:
9694845
9694845
Done </pre>
Done </pre>

=={{header|VBScript}}==
To run in console mode with cscript.
<lang vbscript>dim t()
if Wscript.arguments.count=1 then
n=Wscript.arguments.item(0)
else
n=15
end if
redim t(n+1)
't(*)=0
t(1)=1
for i=1 to n
ip=i+1
for j = i to 1 step -1
t(j)=t(j)+t(j-1)
next 'j
t(i+1)=t(i)
for j = i+1 to 1 step -1
t(j)=t(j)+t(j-1)
next 'j
Wscript.echo t(i+1)-t(i)
next 'i</lang>


=={{header|Visual Basic}}==
=={{header|Visual Basic}}==