How to resolve the algorithm Padovan sequence step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Padovan sequence step by step in the Ruby programming language

Table of Contents

Problem Statement

The Padovan sequence is similar to the Fibonacci sequence in several ways. Some are given in the table below, and the referenced video shows some of the geometric similarities. Show output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Padovan sequence step by step in the Ruby programming language

The provided Ruby code explores the Padovan sequence and its relationship with an L-system. Let's break down the code step by step:

  1. Padovan Sequence:

    • padovan is an enumerator that generates the Padovan sequence.
    • It starts with the values [1, 1, 1] and continues adding the first two elements of the current sequence to get the next element.
  2. Floor Function:

    • padovan_f is a function that calculates the n-th term of the Padovan sequence using a mathematical formula involving the constants P and S.
  3. Displaying Padovan Sequence:

    • The code prints the first 20 elements of the Padovan sequence generated by the enumerator.
  4. Floor Function Verification:

    • The code compares the first 20 terms of the Padovan sequence with the values obtained from the padovan_f function. It checks if the two methods produce the same results.
  5. L-System:

    • The l_system method generates an L-system sequence. It takes an axiom and a set of rules as inputs.
    • You can call this method without a block, in which case it returns an enumerator. If you provide a block, it yields each element of the L-system sequence.
    • This implementation uses the following simple rules: A -> B, B -> C, and C -> AB.
  6. L-System Exploration:

    • The code prints the first 10 elements of the L-system.
    • It also checks if the sizes of the first 32 strings in the L-system match the corresponding terms of the Padovan sequence. This is based on a known relationship between Padovan numbers and L-systems.

Overall, the code provides a way to explore the Padovan sequence and its connection to L-systems.

Source code in the ruby programming language

padovan = Enumerator.new do |y|
  ar = [1, 1, 1]
  loop do
    ar << ar.first(2).sum
    y  << ar.shift
  end
end

P, S = 1.324717957244746025960908854, 1.0453567932525329623
def padovan_f(n) = (P**(n-1) / S + 0.5).floor
  
puts "Recurrence Padovan: #{padovan.take(20)}"
puts "Floor function:     #{(0...20).map{|n| padovan_f(n)}}"

n = 63
bool =  (0...n).map{|n| padovan_f(n)} == padovan.take(n)
puts "Recurrence and floor function are equal upto #{n}: #{bool}."
puts
  
def l_system(axiom = "A", rules = {"A" => "B", "B" => "C", "C" => "AB"} )
  return enum_for(__method__,  axiom, rules) unless block_given? 
  loop do
    yield axiom 
    axiom = axiom.chars.map{|c| rules[c] }.join
  end
end
  
puts "First 10 elements of L-system: #{l_system.take(10).join(", ")} "
n = 32
bool = l_system.take(n).map(&:size) == padovan.take(n)
puts "Sizes of first #{n} l_system strings equal to recurrence padovan? #{bool}."


  

You may also check:How to resolve the algorithm Exceptions step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Monads/Maybe monad step by step in the C programming language
You may also check:How to resolve the algorithm Character codes step by step in the 68000 Assembly programming language