How to resolve the algorithm Catalan numbers step by step in the Icon and Unicon programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Catalan numbers step by step in the Icon and Unicon 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 Icon and Unicon programming language
Source code in the icon programming language
procedure main()
every writes(catalan(0 to 14)," ")
end
procedure catalan(n) # return catalan(n) or fail
static M
initial M := table()
n=0 & return 1
if n > 0 then
return (n = 1) | \M[n] | ( M[n] := (2*(2*n-1)*catalan(n-1))/(n+1))
end
You may also check:How to resolve the algorithm Compiler/code generator step by step in the Task programming language
You may also check:How to resolve the algorithm Special characters step by step in the AWK programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the Scala programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Inform 7 programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the jq programming language