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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Catalan numbers step by step in the Cowgol 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 Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";

sub catalan(n: uint32): (c: uint32) is
    c := 1;
    var i: uint32 := 1;
    while i <= n loop
        c := (4*i-2)*c/(i+1);
        i := i+1;
    end loop;
end sub;

var i: uint8 := 0;
while i < 15 loop
    print("catalan(");
    print_i8(i);
    print(") = ");
    print_i32(catalan(i as uint32));
    print_nl();
    i := i+1;
end loop;

  

You may also check:How to resolve the algorithm Nonogram solver step by step in the 11l programming language
You may also check:How to resolve the algorithm DNS query step by step in the Rust programming language
You may also check:How to resolve the algorithm Quine step by step in the dc programming language
You may also check:How to resolve the algorithm Empty program step by step in the Logo programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the ALGOL 68 programming language