How to resolve the algorithm Ulam spiral (for primes) step by step in the 360 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ulam spiral (for primes) step by step in the 360 Assembly programming language

Table of Contents

Problem Statement

An Ulam spiral (of primes) is a method of visualizing primes when expressed in a (normally counter-clockwise) outward spiral (usually starting at 1),   constructed on a square grid, starting at the "center". An Ulam spiral is also known as a   prime spiral. The first grid (green) is shown with sequential integers,   starting at   1. In an Ulam spiral of primes, only the primes are shown (usually indicated by some glyph such as a dot or asterisk),   and all non-primes as shown as a blank   (or some other whitespace). Of course, the grid and border are not to be displayed (but they are displayed here when using these Wiki HTML tables). Normally, the spiral starts in the "center",   and the   2nd   number is to the viewer's right and the number spiral starts from there in a counter-clockwise direction. There are other geometric shapes that are used as well, including clock-wise spirals. Also, some spirals (for the   2nd   number)   is viewed upwards from the   1st   number instead of to the right, but that is just a matter of orientation. Sometimes, the starting number can be specified to show more visual striking patterns (of prime densities). [A larger than necessary grid (numbers wise) is shown here to illustrate the pattern of numbers on the diagonals   (which may be used by the method to orientate the direction of spiral-construction algorithm within the example computer programs)]. Then, in the next phase in the transformation of the Ulam prime spiral,   the non-primes are translated to blanks. In the orange grid below,   the primes are left intact,   and all non-primes are changed to blanks.
Then, in the final transformation of the Ulam spiral (the yellow grid),   translate the primes to a glyph such as a   •   or some other suitable glyph.

The Ulam spiral becomes more visually obvious as the grid increases in size.

For any sized   N × N   grid,   construct and show an Ulam spiral (counter-clockwise) of primes starting at some specified initial number   (the default would be 1),   with some suitably   dotty   (glyph) representation to indicate primes,   and the absence of dots to indicate non-primes.
You should demonstrate the generator by showing at Ulam prime spiral large enough to (almost) fill your terminal screen.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ulam spiral (for primes) step by step in the 360 Assembly programming language

Source code in the 360 programming language

*        Ulam spiral               26/04/2016
ULAM     CSECT
         USING  ULAM,R13           set base register
SAVEAREA B      STM-SAVEAREA(R15)  skip savearea
         DC     17F'0'             savearea
STM      STM    R14,R12,12(R13)    prolog
         ST     R13,4(R15)         save previous SA
         ST     R15,8(R13)         linkage in previous SA
         LR     R13,R15            establish addressability
         LA     R5,1               n=1
         LH     R8,NSIZE           x=nsize
         SRA    R8,1
         LA     R8,1(R8)           x=nsize/2+1
         LR     R9,R8              y=x
         LR     R1,R5              n
         BAL    R14,ISPRIME
         C      R0,=F'1'           if isprime(n)
         BNE    NPRMJ0
         BAL    R14,SPIRALO        spiral(x,y)=o
NPRMJ0   LA     R5,1(R5)           n=n+1
         LA     R6,1               i=1
LOOPI1   LH     R2,NSIZE           do i=1 to nsize-1 by 2
         BCTR   R2,0
         CR     R6,R2              if i>nsize-1
         BH     ELOOPI1
         LR     R7,R6              j=i; do j=1 to i
LOOPJ1   LA     R8,1(R8)           x=x+1
         LR     R1,R5              n
         BAL    R14,ISPRIME
         C      R0,=F'1'           if isprime(n)
         BNE    NPRMJ1
         BAL    R14,SPIRALO        spiral(x,y)=o
NPRMJ1   LA     R5,1(R5)           n=n+1
         BCT    R7,LOOPJ1          next j 
ELOOPJ1  LR     R7,R6              j=i; do j=1 to i
LOOPJ2   BCTR   R9,0               y=y-1
         LR     R1,R5              n
         BAL    R14,ISPRIME
         C      R0,=F'1'           if isprime(n)
         BNE    NPRMJ2
         BAL    R14,SPIRALO        spiral(x,y)=o
NPRMJ2   LA     R5,1(R5)           n=n+1
         BCT    R7,LOOPJ2          next j 
ELOOPJ2  LR     R7,R6              j=i
         LA     R7,1(R7)           j=i+1; do j=1 to i+1
LOOPJ3   BCTR   R8,0               x=x-1
         LR     R1,R5              n
         BAL    R14,ISPRIME
         C      R0,=F'1'           if isprime(n)
         BNE    NPRMJ3
         BAL    R14,SPIRALO        spiral(x,y)=o
NPRMJ3   LA     R5,1(R5)           n=n+1
         BCT    R7,LOOPJ3          next j 
ELOOPJ3  LR     R7,R6              j=i
         LA     R7,1(R7)           j=i+1; do j=1 to i+1
LOOPJ4   LA     R9,1(R9)           y=y+1
         LR     R1,R5              n
         BAL    R14,ISPRIME
         C      R0,=F'1'           if isprime(n)
         BNE    NPRMJ4
         BAL    R14,SPIRALO        spiral(x,y)=o
NPRMJ4   LA     R5,1(R5)           n=n+1
         BCT    R7,LOOPJ4          next j 
ELOOPJ4  LA     R6,2(R6)           i=i+2
         B      LOOPI1
ELOOPI1  LH     R7,NSIZE           j=nsize
         BCTR   R7,0               j=nsize-1; do j=1 to nsize-1
LOOPJ5   LA     R8,1(R8)           x=x+1
         LR     R1,R5              n
         BAL    R14,ISPRIME
         C      R0,=F'1'           if isprime(n)
         BNE    NPRMJ5
         BAL    R14,SPIRALO        spiral(x,y)=o
NPRMJ5   LA     R5,1(R5)           n=n+1
         BCT    R7,LOOPJ5          next j 
ELOOPJ5  LA     R6,1               i=1
LOOPI2   CH     R6,NSIZE           do i=1 to nsize
         BH     ELOOPI2
         LA     R10,PG             reset buffer
         LA     R7,1               j=1
LOOPJ6   CH     R7,NSIZE           do j=1 to nsize
         BH     ELOOPJ6
         LR     R1,R7              j
         BCTR   R1,0               (j-1)
         MH     R1,NSIZE           (j-1)*nsize
         AR     R1,R6              r1=(j-1)*nsize+i
         LA     R14,SPIRAL-1(R1)   @spiral(j,i)
         MVC    0(1,R10),0(R14)    output spiral(j,i)
         LA     R10,1(R10)         pgi=pgi+1
         LA     R7,1(R7)           j=j+1
         B      LOOPJ6
ELOOPJ6  XPRNT  PG,80              print
         LA     R6,1(R6)           i=i+1
         B      LOOPI2
ELOOPI2  L      R13,4(0,R13)       reset previous SA
         LM     R14,R12,12(R13)    restore previous env
         XR     R15,R15            set return code
         BR     R14                call back
ISPRIME  CNOP   0,4     ---------- isprime function
         C      R1,=F'2'           if nn=2
         BNE    NOT2
         LA     R0,1               rr=1
         B      ELOOPII
NOT2     C      R1,=F'2'           if nn<2
         BL     RRZERO
         LR     R2,R1              nn
         LA     R4,2               2
         SRDA   R2,32              shift
         DR     R2,R4              nn/2
         C      R2,=F'0'           if nn//2=0
         BNE    TAGII
RRZERO   SR     R0,R0              rr=0
         B      ELOOPII
TAGII    LA     R0,1               rr=1
         LA     R4,3               ii=3 
LOOPII   LR     R3,R4              ii
         MR     R2,R4              ii*ii
         CR     R3,R1              if ii*ii<=nn
         BH     ELOOPII
         LR     R3,R1              nn
         LA     R2,0               clear
         DR     R2,R4              nn/ii
         LTR    R2,R2              if nn//ii=0
         BNZ    NEXTII
         SR     R0,R0              rr=0
         B      ELOOPII
NEXTII   LA     R4,2(R4)           ii=ii+2
         B      LOOPII
ELOOPII  BR     R14     ---------- end isprime return rr
SPIRALO  CNOP   0,4     ---------- spiralo subroutine
         LR     R1,R8              x
         BCTR   R1,0               x-1
         MH     R1,NSIZE           (x-1)*nsize
         AR     R1,R9              r1=(x-1)*nsize+y
         LA     R10,SPIRAL-1(R1)   r10=@spiral(x,y)
         MVC    0(1,R10),O         spiral(x,y)=o
         BR     R14     ---------- end spiralo
NS       EQU    79                 4n+1
NSIZE    DC     AL2(NS)            =H'ns'
O        DC     CL1'*'             if prime
PG       DC     CL80' '            buffer
         LTORG
SPIRAL   DC     (NS*NS)CL1' '
         YREGS
         END    ULAM

  

You may also check:How to resolve the algorithm Terminal control/Unicode output step by step in the Julia programming language
You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the BaCon programming language
You may also check:How to resolve the algorithm Cyclops numbers step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Repeat step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Lua programming language