Catalan’s Triangle
September 19, 2017
Since our exercise a week ago, I’ve been reading about Catalan numbers, primarily based on the references at OEIS. Catalan’s Triangle (A009766) is a number triangle
1 1 1 1 2 2 1 3 5 5 1 4 9 14 14 1 5 14 28 42 42 1 6 20 48 90 132 132
such that each element is equal to the one above plus the one to the left.
Your task is to write a program that calculates a Catalan triangle. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Another solution in Python using itertools.accumulate.
^^^ Javascript solution
Here’s a solution in C99.
Output:
C solution
An nice way to construct Catalan’s triangle is to first construct Pascal’s triangle:
and then the Catalan elements are just differences of adjacent elements along diagonals, so, for example, 429 = 3432-3003 = 1716-1287 and 90 = 210-120: