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

Published on 12 May 2024 09:40 PM

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

This Ruby code explores various methods for calculating Catalan numbers, which are used in counting structures such as binary trees and Dyck words. The code includes both direct and recursive approaches, and it analyzes their performance.

  1. factorial Function:

    • This function calculates the factorial of a given number n using a range and the reduce method to multiply all the numbers from 1 to n.
  2. catalan_direct Function (Direct Approach):

    • This function calculates the Catalan number directly using factorials. It uses the formula Catalan(n) = (2n)! / ((n+1)! * n!).
  3. catalan_rec1 Function (Recursive Approach #1):

    • This function calculates the Catalan number recursively using a base case of catalan_rec1(0) = 1. For larger values of n, it calculates the sum of catalan_rec1(i) * catalan_rec1(n-1-i) for i in the range (0...n).
  4. catalan_rec2 Function (Recursive Approach #2):

    • This function also calculates the Catalan number recursively, but it uses a different formula: catalan_rec2(n) = 2*(2*n - 1) * catalan_rec2(n-1) / (n+1).
  5. Performance Benchmark:

    • The code includes a performance benchmark using the Benchmark library. It compares the execution times of each function for n values from 0 to 16.
  6. Memoization:

    • The code memoizes the catalan_rec1 function using the memoize gem to improve its performance for recursive calls.
  7. Output:

    • The code prints a table showing the Catalan numbers for n from 0 to 16, calculated using the direct and recursive approaches.

Overall, this code demonstrates different approaches for calculating Catalan numbers and highlights the performance differences between them. It also shows how memoization can be used to improve the efficiency of recursive algorithms.

Source code in the ruby programming language

def factorial(n)
  (1..n).reduce(1, :*)
end

# direct

def catalan_direct(n)
  factorial(2*n) / (factorial(n+1) * factorial(n))
end

# recursive

def catalan_rec1(n)
  return 1 if n == 0
  (0...n).sum{|i| catalan_rec1(i) * catalan_rec1(n-1-i)}
end

def catalan_rec2(n)
  return 1 if n == 0
  2*(2*n - 1) * catalan_rec2(n-1) / (n+1)
end

# performance and results

require 'benchmark'
require 'memoize'
include Memoize

Benchmark.bm(17) do |b|
  b.report('catalan_direct')    {16.times {|n| catalan_direct(n)} }
  b.report('catalan_rec1')      {16.times {|n| catalan_rec1(n)} }
  b.report('catalan_rec2')      {16.times {|n| catalan_rec2(n)} }
  
  memoize :catalan_rec1
  b.report('catalan_rec1(memo)'){16.times {|n| catalan_rec1(n)} }
end

puts "\n       direct     rec1     rec2"
16.times {|n| puts "%2d :%9d%9d%9d" % [n, catalan_direct(n), catalan_rec1(n), catalan_rec2(n)]}


  

You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the Wren programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the K programming language
You may also check:How to resolve the algorithm Nth root step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm MAC vendor lookup step by step in the OCaml programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the Ring programming language