Anonymous user
Catalan numbers/Pascal's triangle: Difference between revisions
m
→{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (added whitespace before the TOC (table of contents), added a ;Task: and ;See: (bold) headers.) |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
||
Line 981:
===explicit subscripts===
All of the REXX program examples can handle arbitrary large numbers.
<lang rexx>/*REXX program obtains and displays Catalan numbers
parse arg N .
do i=1 for N; ip=i+1
do j=i by -1 for N; jm=j-1; @.j=@.j+@.jm; end /*j*/
@.ip=@.i
end /*i*/ /*stick a fork in it, we're all done. */</lang>
<pre>
1
Line 1,011 ⟶ 1,013:
===implicit subscripts===
<lang rexx>/*REXX program obtains
parse arg N .
@.=0; @.1=1 /*stem array default; define 1st value.*/
do i=1 for N;
do j=i by -1 for N; @.j=@.j+@(j-1); end /*j*/
@.ip=@.i; do k=ip by -1 for N; @.k=@.k+@(k-1); end /*k*/
say @.ip - @.i
end /*i*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲exit /*stick a fork in it, we're done.*/
@: parse arg !; return @.! /*return the value of @.[arg(1)] */</lang>▼
'''output''' is the same as the 1<sup>st</sup> version. ▼
▲@: parse arg !; return @.! /*return the value of @.[arg(1)]*/</lang>
▲'''output''' is the same as the 1<sup>st</sup> version.
===using binomial coefficients===
<lang rexx>/*REXX program obtains
parse arg N .
numeric digits max(9, N%2 + N%8) /*so we can handle huge Catalan numbers*/
do j=1 for N /* [↓]
say comb(j+j,j) % (j+1) /*display the Jth Catalan number. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; parse arg z; _=1; do j=1 for arg(1); _=_*j; end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
comb: procedure; parse arg x,y; if x=y then return 1; if y>x then return 0
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.
===binomial coefficients, memoized===
This REXX version uses memoization for the calculation of factorials.
<lang rexx>/*REXX program obtains
parse arg N .
numeric digits max(9, N%2 + N%8) /*so we can handle huge Catalan numbers*/
!.=.
do j=1 for N /* [↓]
say comb(j+j,j) % (j+1) /*display the Jth Catalan number. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure expose !.;
do j=1 for arg(1); _=_*j; end; !.z=_;
/*──────────────────────────────────────────────────────────────────────────────────────*/
comb: procedure expose !.; parse arg x,y; if x=y then return 1; if y>x then return 0
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><br>▼
▲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>
=={{header|Ruby}}==
|