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

Published on 12 May 2024 09:40 PM
#Jq

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

Table of Contents

Problem Statement

The Yellowstone sequence, also called the Yellowstone permutation, is defined as: For n <= 3, For n >= 4,

The sequence is a permutation of the natural numbers, and gets its name from what its authors felt was a spiking, geyser like appearance of a plot of the sequence.

a(4) is 4 because 4 is the smallest number following 1, 2, 3 in the sequence that is relatively prime to the entry before it (3), and is not relatively prime to the number two entries before it (2).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Yellowstone sequence step by step in the jq programming language

Source code in the jq programming language

# jq optimizes the recursive call of _gcd in the following:
def gcd(a;b):
  def _gcd:
    if .[1] != 0 then [.[1], .[0] % .[1]] | _gcd else .[0] end;
  [a,b] | _gcd ;

# emit the yellowstone sequence as a stream
def yellowstone:
  1,2,3,
  ({ a: [2, 3],                               # the last two items only
     b: {"1":  true, "2": true, "3" : true},  # a record, to avoid having to save the entire history
     start: 4 }
   | foreach range(1; infinite) as $n (.;
       first(
          .b as $b
     	  | .start = first( range(.start;infinite) | select($b[tostring]|not) )
          | foreach range(.start; infinite) as $i (.;
              .emit = null
              | ($i|tostring) as $is
              | if .b[$is] then .
              # "a(n) is relatively prime to a(n-1) and is not relatively prime to a(n-2)"
              elif (gcd($i; .a[1]) == 1) and (gcd($i; .a[0]) > 1)
              then .emit = $i
              | .a = [.a[1], $i]
              | .b[$is] = true
              else .
              end;
              select(.emit)) );
       .emit ));

"The first 30 entries of the Yellowstone permutation:",
[limit(30;yellowstone)]

  

You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the REALbasic programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the Ring programming language
You may also check:How to resolve the algorithm Hostname step by step in the Fortran programming language
You may also check:How to resolve the algorithm Start from a main routine step by step in the Erlang programming language
You may also check:How to resolve the algorithm Draw a pixel step by step in the ZX Spectrum Basic programming language