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 from a Pascal's triangle. */
parse arg N .; if N=='' then N=15 /*Any args?Obtain the No,optional thenargument usefrom defaultCL.*/
numericif digitsN=='' | max(9,N%2+=="." then N%8)=15 /*canNot specified? handleThen hugeuse Catalanthe numbersdefault.*/
@.=0; @.1=1 numeric digits max(9, N%2 + N%8) /*stemso we arraycan default;handle huge 1stCatalan value.numbers*/
exit@.=0; @.1=1 /*stick astem forkarray indefault; it,define we're1st donevalue.*/
 
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
@.ip=@.i; do k=ip by -1 for N; km=k-1; @.k=@.k+@.km; end /*k*/
say @.ip-@.i do k=ip by -1 for N; km=k-1; /*display the Ith@.k=@.k+@.km; Catalan number. end /*k*/
endsay @.ip /*- @.i*/ /*stickdisplay athe fork in it,Ith we're done Catalan number. */</lang>
end /*i*/ /*stick a fork in it, we're all done. */</lang>
{{Out}}'''output''' &nbsp; when using the default input:
<pre>
1
Line 1,011 ⟶ 1,013:
 
===implicit subscripts===
<lang rexx>/*REXX program obtains/ and displays Catalan numbers from a Pascal's triangle. */
parse arg N .; if N=='' then N=15 /*Any args?Obtain the No,optional thenargument usefrom defaultCL.*/
numericif digitsN=='' | max(9,N%2+=="." then N%8)=15 /*canNot specified? handleThen hugeuse Catalanthe numbersdefault.*/
@.=0; @.1=1 numeric digits max(9, N%2 + N%8) /*stemso we arraycan default;handle huge 1stCatalan value.numbers*/
@.=0; @.1=1 /*stem array default; define 1st value.*/
 
do i=1 for N; ip=i+1
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 /*display the Ith Catalan number. */
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>
/*──────────────────────────────────@ subroutine────────────────────────*/
'''output''' &nbsp; 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/ and displays Catalan numbers from a Pascal's triangle. */
parse arg N .; if N=='' then N=15 /*Any args?Obtain the No,optional thenargument usefrom defaultCL.*/
numericif digitsN=='' | max(9,N*4)=="." then N=15 /*canNot specified? handleThen hugeuse Catalanthe numbersdefault.*/
numeric digits max(9, N%2 + N%8) /*so we can handle huge Catalan numbers*/
 
do j=1 for N /* [↓] showdisplay N Catalan numbers. */
say comb(j+j,j) % (j+1) /*display the Jth Catalan number. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────! (factorial) function──────────────*/
!: procedure; parse arg z; _=1; do j=1 for arg(1); _=_*j; end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────COMB (binomial coefficient) function*/
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''' &nbsp; 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/ and displays Catalan numbers from a Pascal's triangle. */
parse arg N .; if N=='' then N=15 /*Any args?Obtain the No,optional thenargument usefrom defaultCL.*/
numericif digitsN=='' | max(9,N*4)=="." then N=15 /*canNot specified? handleThen hugeuse Catalanthe numbersdefault.*/
numeric digits max(9, N%2 + N%8) /*so we can handle huge Catalan numbers*/
!.=.
do j=1 for N /* [↓] showdisplay N Catalan numbers. */
say comb(j+j,j) % (j+1) /*display the Jth Catalan number. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────! (factorial) function──────────────*/
!: procedure expose !.; parse arg z; if !.z\==. then return !.z; _=1
do j=1 for arg(1); _=_*j; end; !.z=_; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────COMB (binomial coefficient) function*/
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>
if y>x then return 0
'''output''' &nbsp; 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}}==