How to resolve the algorithm Catalan numbers/Pascal's triangle step by step in the Racket 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 Racket 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 Racket programming language

Source code in the racket programming language

#lang racket

(define (next-half-row r)
  (define r1 (for/list ([x r] [y (cdr r)]) (+ x y)))
  `(,(* 2 (car r1)) ,@(for/list ([x r1] [y (cdr r1)]) (+ x y)) 1 0))

(let loop ([n 15] [r '(1 0)])
  (cons (- (car r) (cadr r))
        (if (zero? n) '() (loop (sub1 n) (next-half-row r)))))
;; -> '(1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900
;;      2674440 9694845)


  

You may also check:How to resolve the algorithm Anonymous recursion step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Check Machin-like formulas step by step in the R programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Mercury programming language
You may also check:How to resolve the algorithm Long primes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Kotlin programming language