Catalan numbers: Difference between revisions
Content added Content deleted
m (shown the formulas in larger fonts to be easier to read the italics and subscripts, add a Task (bold) header.) |
m (→version 1: added/changed comments, indentations, and whitespace.) |
||
Line 3,014: | Line 3,014: | ||
===version 1=== |
===version 1=== |
||
All methods use (independent) memoization for the computation of factorials. |
All methods use (independent) memoization for the computation of factorials. |
||
<lang rexx>/*REXX program calculates Catalan numbers using four different methods. */ |
<lang rexx>/*REXX program calculates Catalan numbers using four different methods. */ |
||
parse arg |
parse arg LO HI . /*get optional arguments from the C.L. */ |
||
if |
if LO=='' | LO=="," then do; HI=15; LO=0; end /*No args? Use a range of 0 ───► 15. */ |
||
if |
if HI=='' | HI=="," then HI=LO /*No HI? Use the LO for default. */ |
||
numeric digits max(20, 5* |
numeric digits max(20, 5*HI) /*this allows gihugic Catalan numbers. */ |
||
@cat=' Catalan' /*a nice literal to have for the SAY. */ |
@cat=' Catalan' /*a nice literal to have for the SAY. */ |
||
w=length( |
w=length(HI) /*width of the largest number for SAY. */ |
||
call hdr 1A; do j= |
call hdr 1A; do j=LO to HI; say @cat right(j, w)": " Catalan1A(j); end |
||
call hdr 1B; do j= |
call hdr 1B; do j=LO to HI; say @cat right(j, w)": " Catalan1B(j); end |
||
call hdr 2 ; do j= |
call hdr 2 ; do j=LO to HI; say @cat right(j, w)": " Catalan2(j); end |
||
call hdr 3 ; do j= |
call hdr 3 ; do j=LO to HI; say @cat right(j, w)": " Catalan3(j); end |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
Catalan1A: procedure expose !.; parse arg n; return comb(n+n, n) % (n+1) |
Catalan1A: procedure expose !.; parse arg n; return comb(n+n, n) % (n+1) |
||
Catalan1B: procedure expose !.; parse arg n; return !(n+n) % ((n+1) * !(n)**2) |
Catalan1B: procedure expose !.; parse arg n; return !(n+n) % ((n+1) * !(n)**2) |
||
comb: procedure; |
comb: procedure; parse arg x,y; return pFact(x-y+1,x) % pFact(2,y) |
||
pFact: procedure; !=1; do k=arg(1) to arg(2); !=!*k; end; |
pFact: procedure; !=1; do k=arg(1) to arg(2); !=!*k; end; return ! |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
!: procedure expose !.; parse arg x; if !.x\==. then return !.x; !=1 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
!: procedure expose !.; parse arg x; !=1; if !.x\==. then return !.x |
|||
⚫ | |||
⚫ | |||
/*──────────────────────────────────Catalan method 2──────────────────────────*/ |
|||
Catalan2: procedure expose c.; parse arg n; $=0; if c.n\==. then return c.n |
Catalan2: procedure expose c.; parse arg n; $=0; if c.n\==. then return c.n |
||
do k=0 to n-1; $=$+catalan2(k)*catalan2(n-k-1); |
do k=0 to n-1; $=$+catalan2(k)*catalan2(n-k-1); end |
||
c.n=$; return $ /*use a REXX memoization technique.*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────Catalan method 3──────────────────────────*/ |
|||
Catalan3: procedure expose c.; |
Catalan3: procedure expose c.; parse arg n |
||
if c.n==. then c.n=(4*n-2) * catalan3(n-1) % (n+1) |
if c.n==. then c.n=(4*n-2) * catalan3(n-1) % (n+1) |
||
return c.n |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
hdr: !.=.; c.=.; c.0=1; say |
|||
⚫ | |||
'''output''' when using the input of: <tt> 0 16 </tt> |
'''output''' when using the input of: <tt> 0 16 </tt> |
||
<pre> |
<pre> |