How to resolve the algorithm N-smooth numbers step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm N-smooth numbers step by step in the REXX programming language

Table of Contents

Problem Statement

n-smooth   numbers are positive integers which have no prime factors > n. The   n   in the expression   n-smooth   is always prime; there are   no   9-smooth numbers. 1   (unity)   is always included in n-smooth numbers.

2-smooth   numbers are non-negative powers of two. 5-smooth   numbers are also called   Hamming numbers. 7-smooth   numbers are also called   humble numbers.

A way to express   11-smooth   numbers is:

All ranges   (for   n)   are to be inclusive, and only prime numbers are to be used. The (optional) n-smooth numbers for the third range are:   503,   509,   and   521. Show all n-smooth numbers for any particular   n   in a horizontal list. Show all output here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm N-smooth numbers step by step in the REXX programming language

Source code in the rexx programming language

/*REXX pgm computes&displays X n-smooth numbers; both X and N can be specified as ranges*/
numeric digits 200                               /*be able to handle some big numbers.  */
parse arg LOx HIx LOn HIn .                      /*obtain optional arguments from the CL*/
if LOx=='' | LOx==","  then LOx=  1              /*Not specified?  Then use the default.*/
if HIx=='' | HIx==","  then HIx= LOx + 24        /* "      "         "   "   "     "    */
if LOn=='' | LOn==","  then LOn=  2              /* "      "         "   "   "     "    */
if HIn=='' | HIn==","  then HIn= LOn + 27        /* "      "         "   "   "     "    */
call genP HIn                                    /*generate enough primes to satisfy HIn*/
@aList= ' a list of the ';              @thru= ' through '  /*literals used with a  SAY.*/

     do j=LOn  to  HIn;  if !.j==0  then iterate /*if not prime, then skip this number. */
     call smooth HIx,j;                 $=       /*invoke SMOOTH; initialize $  (list). */
                     do k=LOx  to HIx;  $= $ #.k /*append a  smooth number to  "  "   " */
                     end   /*k*/
     say center(@aList  th(LOx)  @thru  th(HIx)     ' numbers for' j"-smooth ",  130, "═")
     say strip($);                      say
     end   /*j*/                                 /* [↑]  the $ list has a leading blank.*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: procedure expose @. !. #; parse arg x      /*#≡num of primes; @. ≡array of primes.*/
      @.=;      @.1=2; @.2=3; @.3=5; @.4=7; @.5=11; @.6=13; @.7=17; @.8=19; @.9=23;    #=9
      !.=0;     !.2=1; !.3=2; !.5=3; !.7=4; !.11=5; !.13=6; !.17=7; !.19=8; !.23=9
           do k=@.#+6  by 2  until #>=x ;        if k//3==0    then iterate
           parse var  k  ''  -1  _;              if _==5       then iterate
                        do d=4  until @.d**2>k;  if k//@.d==0  then iterate k
                        end   /*d*/
           #= # + 1;    !.k= #;       @.#= k     /*found a prime, bump counter; assign @*/
           end  /*k*/;                return
/*──────────────────────────────────────────────────────────────────────────────────────*/
smooth: procedure expose @. !. #.; parse arg y,p /*obtain the arguments from the invoker*/
        if p==''  then p= 3                      /*Not specified? Then assume Hamming #s*/
        n= !.p                                   /*the number of primes being used.     */
        nn= n - 1;            #.=  0;    #.1= 1  /*an array of n-smooth numbers (so far)*/
        f.=  1                                   /*the indices of factors of a number.  */
                do j=2  for y-1;              _= f.1
                z= @.1 * #._
                             do k=2  for nn;  _= f.k;  v= @.k * #._;    if v
                             end   /*k*/
                #.j= z
                             do d=1  for n;   _= f.d;  if @.d * #._==z  then f.d= f.d + 1
                             end   /*d*/
                end   /*j*/;                  return
/*──────────────────────────────────────────────────────────────────────────────────────*/
th: parse arg th; return th || word('th st nd rd', 1+(th//10)*(th//100%10\==1)*(th//10<4))


  

You may also check:How to resolve the algorithm Secure temporary file step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Word wrap step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Rhonda numbers step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Range expansion step by step in the ALGOL 68 programming language