Catalan numbers: Difference between revisions

m
→‎{{header|REXX}}: added another version of the first equation, pruned identical outputs.
m (→‎{{header|REXX}}: added another version of the first equation, pruned identical outputs.)
Line 2,340:
 
=={{header|REXX}}==
All methods use memoization for the computation of factorials.
<lang rexx>/*REXX program calculates Catalan numbers using threefour different methods.*/
parse arg bot top . /*get args from the command line.*/
if bot=='' then do; top=15; bot=0; end /*No args? Use a range of 0 ─► 150──►15.*/
if top=='' then top=bot /*No top? Use the bottom for it.*/
numeric digits max(20,5*top) /*no limit on big Catalan numbers*/
!.=.; w=length(top) /*use W to align Catalan index.*/
 
say; say center(' Catalan numbers, method 11a ' , 79, '-'); !.=0
!.=.; do m1m1a=bot to top
say right(m1m1a,w) '=' catalan1catalan1a(m1m1a)
end /*m1m1a*/
 
say; say center(' Catalan numbers, method 21b ' , 79, '-'); c.=0; c.0=1
!.=.; do m2m1b=bot to top
say right(m2m1b,w) '=' catalan2catalan1b(m2m1b)
end /*m1b*/
 
say; say center(' Catalan numbers, method 2 ' , 79, '─')
!.=.; c.=.; c.0=1; do m2=bot to top
say right(m2,w) '=' catalan2(m2)
end /*m2*/
 
say; say center(' Catalan numbers, method 3 ' , 79, '-'); c.=0; c.0=1
!.=.; c.=.; c.0=1; do m3=bot to top
say right(m3,w) '=' catalan3(m3)
end /*m3*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────catalan method 1────────────────────1a───────────────────*/
 
catalan1catalan1a: procedure expose !.; parse arg n /*n+n is faster than 2*n */
/*──────────────────────────────────catalan method 1────────────────────*/
catalan1: procedure expose !.; parse arg n /*n+n is faster than 2*n */
return !(n+n) % ( (n+1) * !(n)**2 ) /*using COMB would be faster*/
/*──────────────────────────────────catalan method 1b───────────────────*/
 
catalan1b: procedure expose !.; parse arg n
return comb(n+n,n)%(n+1) /*use COMB (binomial) function.*/
/*──────────────────────────────────catalan method 2────────────────────*/
catalan2: procedure expose c.; parse arg n; if c.n\==0. then return c.n
s=0; do j=0 to n-1
s=s + catalan2(j) * catalan2(n-j-1) /*recursive invokes.*/
Line 2,375 ⟶ 2,381:
c.n=s /*use REXX memoization technique.*/
return s
 
/*──────────────────────────────────catalan method 3────────────────────*/
catalan3: procedure expose c.; parse arg n; if c.n\==0. then return c.n
c.n=(4*n-2) * catalan3(n-1) % (n+1) /*use REXX memoization technique.*/
return c.n
 
/*──────────────────────────────────! (factorial) function──────────────*/
!: procedure expose !.; parse arg x; if !.x\==0. then return !.x; !=1
do k=1 for x
!=!*k
end /*k*/
!.x=! /*use REXX memoization technique.*/
return !</lang>
/*──────────────────────────────────COMB subroutine─────────────────────*/
'''output''' when using the input of: <tt> 0 16 </tt>
comb: procedure; parse arg x,y; return pFact(x-y+1,x) % pFact(2,y)
<pre style="height:30ex;overflow:scroll">
/*──────────────────────────────────PFACT subroutine────────────────────*/
-------------------------- Catalan numbers, method 1 --------------------------
pFact: procedure; !=1; do j=arg(1) to arg(2); !=!*j; end; return !</lang>
0 = 1
'''output''' when using the input of: &nbsp; <tt> 0 16 </tt>
<pre>
───────────────────────── Catalan numbers, method 1a ──────────────────────────
1 = 1
2 = 2
Line 2,409 ⟶ 2,416:
16 = 35357670
 
───────────────────────── Catalan numbers, method 1b ──────────────────────────
-------------------------- Catalan numbers, method 2 --------------------------
... (elided, same as first version) ...
0 = 1
1 = 1
2 = 2
3 = 5
4 = 14
5 = 42
6 = 132
7 = 429
8 = 1430
9 = 4862
10 = 16796
11 = 58786
12 = 208012
13 = 742900
14 = 2674440
15 = 9694845
16 = 35357670
 
───────────────────────── Catalan numbers, method 2 ──────────────────────────
-------------------------- Catalan numbers, method 3 --------------------------
... (elided, same as first version) ...
0 = 1
 
1 = 1
───────────────────────── Catalan numbers, method 3 ──────────────────────────
2 = 2
... (elided, same as first version) ...
3 = 5
4 = 14
5 = 42
6 = 132
7 = 429
8 = 1430
9 = 4862
10 = 16796
11 = 58786
12 = 208012
13 = 742900
14 = 2674440
15 = 9694845
16 = 35357670
</pre>