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

Published on 12 May 2024 09:40 PM
#Jq

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

Table of Contents

Problem Statement

The Sisyphus sequence is an infinite sequence of positive integers that was devised in 2022 by Eric Angelini and Carole Dubois. The first term is 1. Subsequent terms are found by applying the following rule: 1 is odd and so the second term is: 1 + 2 = 3, because 2 is the smallest prime not yet added. 3 is odd and so the third term is: 3 + 3 = 6, because 3 is the smallest prime not yet added. 6 is even and so the fourth term is : 6 ÷ 2 = 3, and so on. Find and show on this page (in 10 lines of 10 terms), the first 100 terms of the sequence. What are the 1,000th, 10,000th, 100,000th and 1,000,000th terms of the sequence and, in each case, what is the highest prime needed to reach them? If it is difficult or impossible for your language or output device to meet all of these requirements, then just do what you reasonably can. What are the 10 millionth and 100 millionth terms and the highest prime needed to reach each one? By the time the 100 millionth term is reached, which number(s) under 250:

What is the number of the first term to equal 36? This was originally set as a challenge by Neil Sloane who was worried by its non-appearance and found by Russ Cox.

Let's start with the solution:

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

Source code in the jq programming language

# The def of _nwise can be omitted if using the C implementation of jq.
def _nwise($n):
  def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
  n;

def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;

# tabular print
def tprint(columns; wide):
  reduce _nwise(columns) as $row ("";
     . + ($row|map(lpad(wide)) | join(" ")) + "\n" );

# Input:  a positive integer
# Output: an array, $a, of length .+1 such that
#         $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
  # erase(i) sets .[i*j] to false for integral j > 1
  def erase($i):
    if .[$i] then
      reduce (range(2*$i; length; $i)) as $j (.; .[$j] = false) 
    else .
    end;
  (. + 1) as $n
  | (($n|sqrt) / 2) as $s
  | [null, null, range(2; $n)]
  | reduce (2, 1 + (2 * range(1; $s))) as $i (.; erase($i)) ;

# $limit is the target number of Sisyphus numbers to find and should be at least 100
def task($limit):
  ((7 * $limit) | primeSieve | map(select(.))) as $primes
  | { under250: [0, 1],
      sisyphus: [1],
      prev: 1,
      nextPrimeIndex: 0,
      specific: 1000,
      count:1 }
  | while(.count <= $limit;
      .emit = null
      | if .prev % 2 == 0 then .next = .prev/2
        else .next = .prev + $primes[.nextPrimeIndex]
        | .nextPrimeIndex += 1
	end
     | .count += 1
     | if .count <= 100 then .sisyphus += [.next] else . end
     | if .next < 250 then .under250[.next] += 1 else . end
     | if .count == 100
       then .emit = "The first 100 members of the Sisyphus sequence are:\n" + (.sisyphus | tprint(10;3))
       elif .count == .specific
       then $primes[.nextPrimeIndex-1] as $prime
       | .emit = "\(.count|lpad(8))th member is: \(.next|lpad(10)) and highest prime needed: \($prime|lpad(10))"
         | .specific *= 10
       else .
       end
    | .prev = .next )
  # The results:
  | select(.emit).emit,
    if .count == $limit
    then .under250 as $u
    | [range(1;250) | select( $u[.] == null)] as $notFound
    | ($u|max) as $max
    | [range(1;250) | select($u[.] == $max)] as $maxFound
    | "\nThese numbers under 250 do not occur in the first \(.count) terms:",
      "  \($notFound)",
      "\nThese numbers under 250 occur the most in the first \(.count) terms:",
      "  \($maxFound) all occur \($max) times."
    else empty
    end;

task(1e7)

  

You may also check:How to resolve the algorithm Egyptian division step by step in the Sidef programming language
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Window creation step by step in the TXR programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the Raku programming language
You may also check:How to resolve the algorithm Loops/For step by step in the FALSE programming language