Babbage problem: Difference between revisions

Content deleted Content added
m Moved SequenceL so things would be in alpha order
PatGarrett (talk | contribs)
Line 22:
The aim of the task is to write a program that is sufficiently clear and well-documented for such a person to be able to read it and be confident that it does indeed solve the specified problem.
<br><br>
 
=={{header|360 Assembly}}==
An assembler program always seems a bit tricky for non system engineer because it deals directly with the operating system and with the hardware instructions. Here we have a 32-bit computer with 16 32-bit registers. The caller (the operating system to keep it simple) is calling you giving your location address stored in register-15 and has stored in register-14 his return address. To save each program context, register-13 points to a 18 word save area. Do not spend time in understanding the context saving and restoring in the prologue and epilogue part of the program. What you have to know, “360” architecture uses 32-bit signed binary arithmetic, so here the maximum integer value is 2^31-1 (2147483647). Therefore the solution must be less than 2147483647. The multiplication and the division use a pair of registers; coding “MR 4,2” means multiply register-5 by register-2 and place result in the (register-4,register-5) pair; the same way “DR 4,2” means divide the (register-4,register-5) pair by register-2 and place the quotient in register-5 and the reminder in register-4. We use in the below program this intermediate 64-bit integers to find a solution with a value up to 2^31-1 even when we have to compute the square of this value.
<lang 360asm>
* Find the lowest positive integer whose square ends in 269696
* The logic of the assembler program is simple :
* loop for i=524 step 2
* if (i*i modulo 1000000)=269696 then leave loop
* next i
* output 'Solution is: i=' i ' (i*i=' i*i ')'
BABBAGE CSECT beginning of the control section
USING BABBAGE,13 define the base register
B 72(15) skip savearea (72=18*4)
DC 17F'0' savearea (18 full words (17+1))
STM 14,12,12(13) prolog: save the caller registers
ST 13,4(15) prolog: link backwards
ST 15,8(13) prolog: link forwards
LR 13,15 prolog: establish addressability
LA 6,524 let register6 be i and load 524
LOOP LR 5,6 load register5 with i
MR 4,6 multiply register5 with i
LR 7,5 load register7 with the result i*i
D 4,=F'1000000' divide register5 with 1000000
C 4,=F'269696' compare the reminder with 269696
BE ENDLOOP if equal branch to ENDLOOP
LA 6,2(6) load register6 (i) with value i+2
B LOOP branch to LOOP
ENDLOOP XDECO 6,BUFFER+15 edit registrer6 (i)
XDECO 7,BUFFER+34 edit registrer7 (i squared)
XPRNT BUFFER,L'BUFFER print buffer
L 13,4(0,13) epilog: restore the caller savearea
LM 14,12,12(13) epilog: restore the caller registers
XR 15,15 epilog: set return code to 0
BR 14 epilog: branch to caller
BUFFER DC CL80'Solution is: i=............ (i*i=............)'
END BABBAGE end of the control section
</lang>
{{out}}
<pre>
Solution is: i= 25264 (i*i= 638269696)
</pre>
 
 
=={{header|Ada}}==