How to resolve the algorithm Loops/With multiple ranges step by step in the Factor programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Loops/With multiple ranges step by step in the Factor programming language

Table of Contents

Problem Statement

Some languages allow multiple loop ranges, such as the PL/I example (snippet) below.

Simulate/translate the above PL/I program snippet as best as possible in your language,   with particular emphasis on the   do   loop construct. The   do   index must be incremented/decremented in the same order shown. If feasible, add commas to the two output numbers (being displayed). Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/With multiple ranges step by step in the Factor programming language

Source code in the factor programming language

USING: formatting kernel locals math math.functions math.ranges
sequences sequences.generalizations tools.memory.private ;

[let                            ! Allow lexical variables.
     1 :> prod!                 ! Start with a product of unity.
     0 :> sum!                  !   "     "  "   sum    " zero.
     5 :> x
    -5 :> y
    -2 :> z
     1 :> one
     3 :> three
     7 :> seven

    three neg 3 3 ^ three <range>              ! Create array
    seven neg seven x     <range>              ! of 7 ranges.
    555 550 y -             [a,b]
    22 -28 three neg      <range>
    1927 1939               [a,b]
    x y z                 <range>
    11 x ^ 11 x ^ 1 +       [a,b] 7 narray

    [
        [
            :> j j abs sum + sum!
            prod abs 2 27 ^ < j zero? not and
            [ prod j * prod! ] when
        ] each                      ! Loop over range.
    ] each                          ! Loop over array of ranges.
    
    ! SUM and PROD are used for verification of J incrementation.
    sum prod [ commas ] bi@ " sum=  %s\nprod= %s\n" printf
]


  

You may also check:How to resolve the algorithm String concatenation step by step in the ReScript programming language
You may also check:How to resolve the algorithm Quine step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the REXX programming language
You may also check:How to resolve the algorithm First-class functions step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Solve a Numbrix puzzle step by step in the Elixir programming language