How to resolve the algorithm Loops/Increment loop index within loop body step by step in the 360 Assembly programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Loops/Increment loop index within loop body step by step in the 360 Assembly programming language
Table of Contents
Problem Statement
Sometimes, one may need (or want) a loop which its iterator (the index variable) is modified within the loop body in addition to the normal incrementation by the (do) loop structure index.
Demonstrate the best way to accomplish this.
Write a loop which:
Extra credit: because of the primes get rather large, use commas within the displayed primes to ease comprehension.
Show all output here.
Not all programming languages allow the modification of a loop's index. If that is the case, then use whatever method that is appropriate or idiomatic for that language. Please add a note if the loop's index isn't modifiable.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/Increment loop index within loop body step by step in the 360 Assembly programming language
Source code in the 360 programming language
* Loops/Increment loop index within loop body - 16/07/2018
LOOPILWB PROLOG
SR R6,R6 i=0
ZAP N,=P'42' n=42
DO WHILE=(C,R6,LT,IMAX) do while(i
BAL R14,ISPRIME call isprime(n)
IF C,R0,EQ,=F'1' THEN if n is prime then
LA R6,1(R6) i=i+1
XDECO R6,XDEC edit i
MVC PG+2(2),XDEC+10 output i
MVC ZN,EM load edit mask
ED ZN,N edit n
MVC PG+7(L'ZN),ZN output n
XPRNT PG,L'PG print buffer
ZAP WP,N n
AP WP,N +n
SP WP,=P'1' +1
ZAP N,WP n=n+n-1
ENDIF , endif
ZAP WP,N n
AP WP,=P'1' +1
ZAP N,WP n=n+1
ENDDO , enddo
EPILOG
ISPRIME EQU * isprime(n) -----------------------
CP N,=P'2' if n=2
BE RETURN1 then return(1)
CP N,=P'3' if n=3
BE RETURN1 then return(1)
ZAP WDP,N n
DP WDP,=PL8'2' /2
CP WDP+8(8),=P'0' if mod(n,2)=0
BE RETURN0 then return(0)
ZAP WDP,N n
DP WDP,=PL8'3' /3
CP WDP+8(8),=P'0' if mod(n,3)=0
BE RETURN0 then return(0)
ZAP J,=P'5' j=5
LWHILE ZAP WP,J j
MP WP,J *j
CP WP,N while(j*j<=n)
BH EWHILE ~
ZAP WDP,N n
DP WDP,J /j
CP WDP+8(8),=P'0' if mod(n,j)=0
BE RETURN0 then return(0)
ZAP WP,J j
AP WP,=P'2' +2
ZAP WDP,N n
DP WDP,WP n/(j+2)
CP WDP+8(8),=P'0' if mod(n,j+2)=0
BE RETURN0 then return(0)
ZAP WP,J j
AP WP,=P'6' +6
ZAP J,WP j=j+6
B LWHILE loopwhile
EWHILE B RETURN1 return(1)
RETURN0 LA R0,0 rc=0
B RETURNX
RETURN1 LA R0,1 rc=1
RETURNX BR R14 return to caller -----------------
IMAX DC F'42' limit
EM DC XL20'402020206B2020206B2020206B2020206B202120' mask
N DS PL8 n
J DS PL8 j
PG DC CL80'i=00 : 000,000,000,000,000' buffer
XDEC DS CL12 temp for XDECO
WP DS PL8 temp for AP,SP,MP
WDP DS PL16 temp for DP
CW DS CL16 temp for UNPK
ZN DS CL20
REGEQU
END LOOPILWB
You may also check:How to resolve the algorithm Price fraction step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Rename a file step by step in the C# programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the GAP programming language
You may also check:How to resolve the algorithm Kosaraju step by step in the C# programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the TI-89 BASIC programming language