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

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Van Eck sequence step by step in the jq 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 jq programming language

Source code in the jq programming language

# Input: an array
# If the rightmost element, .[-1], does not occur elsewhere, return 0;
# otherwise return the "depth" of its rightmost occurrence in .[0:-2]
def depth:
  .[-1] as $x
  | length as $length
  | first(range($length-2; -1; -1) as $i
          | select(.[$i] == $x) | $length - 1 - $i)
     // 0 ;

# Generate a stream of the first $n van Eck integers:
def vanEck($n):
  def v:
    recurse( if length == $n then empty
             else . + [depth] end );
  [0] | v | .[-1];

# The task:
[vanEck(10)], [vanEck(1000)][990:1001]


[0,0,1,0,2,0,2,2,1,6]
[4,7,30,25,67,225,488,0,10,136]


  

You may also check:How to resolve the algorithm Least common multiple step by step in the Bracmat programming language
You may also check:How to resolve the algorithm 4-rings or 4-squares puzzle step by step in the Wren programming language
You may also check:How to resolve the algorithm Abelian sandpile model step by step in the Nim programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the Julia programming language
You may also check:How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the C# programming language