How to resolve the algorithm Generator/Exponential step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Generator/Exponential step by step in the jq programming language

Table of Contents

Problem Statement

A generator is an executable entity (like a function or procedure) that contains code that yields a sequence of values, one at a time, so that each time you call the generator, the next value in the sequence is provided. Generators are often built on top of coroutines or objects so that the internal state of the object is handled “naturally”. Generators are often used in situations where a sequence is potentially infinite, and where it is possible to construct the next value of the sequence with only minimal state.

Note that this task requires the use of generators in the calculation of the result.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Generator/Exponential step by step in the jq programming language

Source code in the jq programming language

# Compute self^m where m is a non-negative integer:
def pow(m): . as $in | reduce range(0;m) as $i (1; .*$in);

# state: [i, i^m]
def next_power(m): .[0] + 1 | [., pow(m) ];

# skip m states, and return the next state
def skip(m; next):
  if m <= 0 then . else next | skip(m-1; next) end;

# emit m states including the initial state
def emit(m; next):
  if m <= 0 then empty else ., (next | emit(m-1; next)) end;

# Generate the first 4 values in the sequence i^2:
[0,0] | emit(4; next_power(2)) | .[1]

# Generate all the values in the sequence i^3 less than 100:
[0,0] | recurse(next_power(3) | if .[1] < 100 then . else empty end) | .[1]

# Infrastructure:
def last(f): reduce f as $i (null; $i);

# emit the last value that satisfies condition, or null
def while(condition; next):
  def w: if condition then ., (next|w) else empty end;
  last(w);

# Powers of m1 that are not also powers of m2.
# filtered_next_power(m1;m2) produces [[i, i^m1], [j, j^m1]] where i^m1
# is not a power of m2 and j^m2 < i^m1
#
def filtered_next_power(m1; m2):
  if . then . else [[0,0],[0,0]] end
  | (.[0] | next_power(m1)) as $next1
  | (.[1] | while( .[1] <= $next1[1]; next_power(m2))) as $next2
  | if $next1[1] == $next2[1]
    then [$next1, $next2] | filtered_next_power(m1;m2)
    else [$next1, $next2]
    end ;

# Emit ten powers of 2 that are NOT powers of 3,
# skipping the first 20 integers satisfying the condition, including 0.
filtered_next_power(2;3)
  | skip(20; filtered_next_power(2;3))
  | emit(10; filtered_next_power(2;3))
  | .[0][1]

$ jq -n -f generators.jq
529
576
625
676
784
841
900
961
1024
1089


  

You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Topswops step by step in the Picat programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the OCaml programming language
You may also check:How to resolve the algorithm Esthetic numbers step by step in the Rust programming language