Jump to content

Catalan numbers: Difference between revisions

m
→‎version 1: simplified a function, added whitespace to output section.
m (→‎version 1: indented a statement for alignment.)
m (→‎version 1: simplified a function, added whitespace to output section.)
Line 2,797:
@cat=' Catalan' /*a nice literal to have for the SAY. */
w=length(top) /*width of the largest number for SAY. */
call hdr 1A; do j=bot to top; say @cat right(j,w)": " catalan1ACatalan1A(j); end
call hdr 1B; do j=bot to top; say @cat right(j,w)": " catalan1BCatalan1B(j); end
call hdr 2 ; do j=bot to top; say @cat right(j,w)": " catalan2Catalan2(j); end
call hdr 3 ; do j=bot to top; say @cat right(j,w)": " catalan3Catalan3(j); end
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
catalan1ACatalan1A: procedure expose !.; parse arg n; return comb(n+n, n) % (n+1)
catalan1BCatalan1B: procedure expose !.; parse arg n; return !(n+n) % ((n+1) * !(n)**2)
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; return !
Line 2,815:
!.x=!; return !
/*──────────────────────────────────Catalan method 2──────────────────────────*/
catalan2Catalan2: procedure expose c.; parse arg n; s$=0; if c.n\==. then return c.n
do k=0 to n-1; s$=s$+catalan2(k)*catalan2(n-k-1); end
c.n=s$; return s$ /*use a REXX memoization technique. */
/*──────────────────────────────────Catalan method 3──────────────────────────*/
catalan3Catalan3: procedure expose c.; parse arg n; if c.n\==. then return c.n
if c.n==. then c.n=(4*n-2) * catalan3(n-1) % (n+1); return c.n/</lang>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 0 &nbsp; 16 </tt>
<pre>
───────────────────────── Catalan numbers, method 1A ──────────────────────────
Cookies help us deliver our services. By using our services, you agree to our use of cookies.