How to resolve the algorithm Van Eck sequence step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Van Eck sequence step by step in the Lua programming language

Table of Contents

Problem Statement

The sequence is generated by following this pseudo-code:

Using A: Using B: Using C: Using B: Using C: (zero last occurred two steps back - before the one) Using B: Using C: (two last occurred two steps back - before the zero) Using C: (two last occurred one step back) Using C: (one last appeared six steps back) ...

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Van Eck sequence step by step in the Lua programming language

Source code in the lua programming language

-- Return a table of the first n values of the Van Eck sequence
function vanEck (n)
  local seq, foundAt = {0}
  while #seq < n do
    foundAt = nil
    for pos = #seq - 1, 1, -1 do
      if seq[pos] == seq[#seq] then
        foundAt = pos
        break
      end
    end
    if foundAt then
      table.insert(seq, #seq - foundAt)
    else
      table.insert(seq, 0)
    end
  end
  return seq
end

-- Show the set of values in table t from key numbers lo to hi
function showValues (t, lo, hi)
  for i = lo, hi do
    io.write(t[i] .. " ")
  end
  print()
end

-- Main procedure
local sequence = vanEck(1000)
showValues(sequence, 1, 10)
showValues(sequence, 991, 1000)


  

You may also check:How to resolve the algorithm Random number generator (device) step by step in the zkl programming language
You may also check:How to resolve the algorithm Sudan function step by step in the APL programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the PowerShell programming language