How to resolve the algorithm Executable library step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Executable library step by step in the REXX programming language

Table of Contents

Problem Statement

The general idea behind an executable library is to create a library that when used as a library does one thing; but has the ability to be run directly via command line. Thus the API comes with a CLI in the very same source code file. Task detail Notes:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Executable library step by step in the REXX programming language

Source code in the rexx programming language

/*REXX program returns the hailstone (Collatz) sequence for any integer.*/
numeric digits 20                      /*ensure enough digits for mult. */
parse arg n 1 s                        /*N & S assigned to the first arg*/
                do  while n\==1        /*loop while  N  isn't  unity.   */
                if n//2  then n=n*3+1  /*if  N  is odd,  calc:   3*n +1 */
                         else n=n%2    /* "  "   " even, perform fast ÷ */
                s=s n                  /*build a sequence list (append).*/
                end   /*while*/
return s


/*REXX pgm tests a number and a range for hailstone (Collatz) sequences.*/
parse arg x .;  if x=='' then x=27     /*get the optional first argument*/

$=hailstone(x)                         /*═════════════task 2════════════*/
#=words($)                             /*number of numbers in sequence. */
say x 'has a hailstone sequence of'  #  'and starts with: ' subword($,1,4),
                                       ' and ends with:'    subword($,#-3)
say
w=0;       do j=1  for 99999           /*═════════════task 3════════════*/
           $=hailstone(j);  #=words($) /*obtain the hailstone sequence. */
           if #<=w  then iterate       /*Not big 'nuff? Then keep going.*/
           bigJ=j;    w=#              /*remember what # has biggest HS.*/
           end   /*j*/

say '(between 1──►99,999) '  bigJ  'has the longest hailstone sequence:' w
                                       /*stick a fork in it, we're done.*/


/*REXX pgm finds the most common (popular) hailstone sequence length.   */
parse arg z .;  if z=='' then z=99999  /*get the optional first argument*/
!.=0
w=0;          do j=1  for z            /*═════════════task 4════════════*/
              #=words(hailstone(j))    /*obtain hailstone sequence count*/
              !.# = !.# + 1            /*add unity to popularity count. */
              end   /*j*/
occ=0;  p=0
              do k=1  for z
              if !.k>occ  then do;  occ=!.k;  p=k;  end
              end   /*p*/

say '(between 1──►'z") "  p,
' is the most common hailstone sequence length  (with' occ "occurrences)."
                                       /*stick a fork in it, we're done.*/


  

You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the Python programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Prolog programming language
You may also check:How to resolve the algorithm Generator/Exponential step by step in the C programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the D programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the Racket programming language