How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Phix programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Phix programming language

Table of Contents

Problem Statement

Print out the first   15   Catalan numbers by extracting them from Pascal's triangle.

Pascal's triangle

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Phix programming language

Source code in the phix programming language

-- FreeBASIC said:
--'  1   1   1   1   1   1  
--'  1   2   3   4   5   6  
--'  1   3   6  10  15  21  
--'  1   4  10  20  35  56  
--'  1   5  15  35  70 126  
--'  1   6  21  56 126 252  
--' The Pascal triangle is rotated 45 deg.
--' to find the Catalan number we need to follow the diagonal
--' for top left to bottom right 
--' take the number on diagonal and subtract the number in de cell
--' one up and one to right 
--' 1 (2 - 1), 2 (6 - 4), 5 (20 - 15) ... 
--
-- The first thing that struck me was it is twice as big as it needs to be, 
--  something like this would do...
--    1   1   1   1   1   1  
--        2   3   4   5   6  
--            6  10  15  21  
--               20  35  56  
--                   70 126  
--                      252  
-- It is more obvious from the upper square that the diagonal on that, which is 
--  that same as column 1 on this, is twice the previous, which on the second 
--  diagram is in column 2. Further, once we have calculated the value for column 
--  one above, we can use it immediately to calculate the next catalan number and 
--  do not need to store it. Lastly we can overwrite row 1 with row 2 etc in situ, 
--  and the following shows what we need for subsequent rounds:
--    1   1   1   1   1
--    3   4   5   6  
--   10  15  21  
--   35  56  
--  126  (unused)


  

You may also check:How to resolve the algorithm Semiprime step by step in the PL/M programming language
You may also check:How to resolve the algorithm User input/Text step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the LabVIEW programming language
You may also check:How to resolve the algorithm Include a file step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Variable declaration reset step by step in the F# programming language