How to resolve the algorithm Catalan numbers step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Catalan numbers step by step in the REXX programming language

Table of Contents

Problem Statement

Catalan numbers are a sequence of numbers which can be defined directly: Or recursively: Or alternatively (also recursive):

Implement at least one of these algorithms and print out the first 15 Catalan numbers with each. Memoization   is not required, but may be worth the effort when using the second method above.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Catalan numbers step by step in the REXX programming language

Source code in the rexx programming language

/*REXX program calculates and displays  Catalan numbers  using  four different methods. */
parse arg LO HI .                                /*obtain optional arguments from the CL*/
if LO=='' | LO==","  then do;  HI=15; LO=0;  end /*No args? Then use a range of 0 ──► 15*/
if HI=='' | HI==","  then      HI=LO             /*No HI?   Then use  LO for the default*/
numeric digits max(20, 5*HI)                     /*this allows gihugic Catalan numbers. */
w=length(HI)                                     /*W:  is used for aligning the output. */
call hdr 1A;  do j=LO  to HI;  say '     Catalan'     right(j, w)": "      Cat1A(j);   end
call hdr 1B;  do j=LO  to HI;  say '     Catalan'     right(j, w)": "      Cat1B(j);   end
call hdr 2 ;  do j=LO  to HI;  say '     Catalan'     right(j, w)": "      Cat2(j) ;   end
call hdr 3 ;  do j=LO  to HI;  say '     Catalan'     right(j, w)": "      Cat3(j) ;   end
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!:     arg z; if !.z\==. then return !.z; !=1;  do k=2  to z; !=!*k; end;  !.z=!; return !
Cat1A: procedure expose !.;  parse arg n;     return comb(n+n, n)    %  (n+1)
Cat1B: procedure expose !.;  parse arg n;     return !(n+n) % ((n+1) * !(n)**2)
Cat3:  procedure expose c.;  arg n; if c.n==. then c.n=(4*n-2)*cat3(n-1)%(n+1); return c.n
comb:  procedure;            parse arg x,y;   return pFact(x-y+1, x) % pFact(2, y)
hdr:   !.=.; c.=.; c.0=1; say; say center('Catalan numbers, method' arg(1),79,'─'); return
pFact: procedure;            !=1;      do k=arg(1)  to arg(2);  !=!*k;  end;    return !
/*──────────────────────────────────────────────────────────────────────────────────────*/
Cat2:  procedure expose c.;  parse arg n;  $=0;         if c.n\==.  then return c.n
                                       do k=0  for n;   $=$ + Cat2(k) * Cat2(n-k-1);   end
                             c.n=$;           return $    /*use a memoization technique.*/


/* REXX ---------------------------------------------------------------
* 01.07.2014 Walter Pachl
*--------------------------------------------------------------------*/
Numeric Digits 1000
Parse Arg m .
If m='' Then m=20
Do i=0 To m
  c1.i=c1(i)
  End
c2.=1
Do i=1 To m
  c2.i=c2(i)
  End
c3.=1
Do i=1 To m
  im1=i-1
  c3.i=2*(2*i-1)*c3.im1/(i+1)
  End
l=length(c3.m)
hdr=' n' right('c1.n',l),
         right('c2.n',l),
         right('c3.n',l)
Say hdr
Do i=0 To m
  Say right(i,2) format(c1.i,l),
                 format(c2.i,l),
                 format(c3.i,l)
  End
Say hdr
Exit

c1: Procedure
Parse Arg n
return fact(2*n)/(fact(n)*fact(n+1))

c2: Procedure Expose c2.
Parse Arg n
res=0
Do i=0 To n-1
  nmi=n-i-1
  res=res+c2.i*c2.nmi
  End
Return res

fact: Procedure
Parse Arg n
f=1
Do i=1 To n
  f=f*i
  End
Return f


  

You may also check:How to resolve the algorithm Count the coins step by step in the Python programming language
You may also check:How to resolve the algorithm Casting out nines step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the Quackery programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the Perl programming language