(41C 32S): n = a^2 + b
07-18-2021, 05:00 AM
Post: #1
 Eddie W. Shore Senior Member Posts: 1,590 Joined: Dec 2013
(41C 32S): n = a^2 + b
The following program splits a positive integer n into the sum:

n = a^2 + b

This technique is useful, especially with the HP 32S and HP 32SII. Normally, real numbers take up 9.5 bytes of memory on the HP 32S series. Integers 0 - 100 only consume 1.5 bytes of memory for the original HP 32S and the range is extended to 0 - 254 for the HP 32SII.

When the program terminates, b is on the X stack while a is on the Y stack.

Example: 684

Result:
Y: 26
X: 8
(26^2 + 8 = 684)

HP 41C/Swiss Micros DM41X Program: SQPLUS

Code:
01 LBL^T SQPLUS 02 FIX 0 03 ENTER  04 SQRT 05 RND 06 X^2 07 - 08 LASTX 09 SQRT 10 X<>Y 11 FIX 4 12 RTN

HP 32S and HP 32SII Program: SQPLUS

Code:
S01 LBL S S02 FIX 0 S03 ENTER S04 SQRT S05 RND S06 x^2 S07 - S08 LASTx S09 SQRT S10 x<>y S11 ALL S12 RTN
07-18-2021, 06:17 AM (This post was last modified: 07-18-2021 06:52 AM by C.Ret.)
Post: #2
 C.Ret Member Posts: 259 Joined: Dec 2013
RE: (41C 32S): n = a^2 + b
Hello,

Interesting decomposition. I have not fully understand why this spare memory since, two registers may be more than one. Also, not clear why you are using the round function RND that need to chance display FIX format in the process.

Can we envisage using a simpler version based on INT and stack arithmetic operations ? Such as:

Code:
001 LBL "SQY2X 002   ENTER^  SQRT  INT  ST/Y  ST-Y  ST*Y  X<>Y 009 END

And for system like the HP-15C and classic that have no stack arithmetic facilities:
Code:
01 LBL A 02   ENTER  √x  INT  ÷  LASTX  -  LASTX  ×  LASTX  X<>Y 12 RTN

Having the decomposition results of n = a² + b in the inverse order in the final registers Y: b and X: a may spare the X<>Y instruction then.

EDIT:
In this sunny sunday mornig, I was posting this message when by rereading it, a shorter and simpliest version pop up in my awaking brain that use a MOD instruction :

Code:
01 LBL "SQYX2" 02 ENTER^  SQRT  INT  MOD  LSTX 06 END

Here the last X<>Y is missing so the final results are in the inverse stack order that was asked : Y: $$b$$ X: $$a$$ for the initial input $$n = a^2+b$$.
07-18-2021, 09:19 AM (This post was last modified: 07-18-2021 09:23 AM by J-F Garnier.)
Post: #3
 J-F Garnier Senior Member Posts: 946 Joined: Dec 2013
RE: (41C 32S): n = a^2 + b
(07-18-2021 05:00 AM)Eddie W. Shore Wrote:  This technique is useful, especially with the HP 32S and HP 32SII. Normally, real numbers take up 9.5 bytes of memory on the HP 32S series. Integers 0 - 100 only consume 1.5 bytes of memory for the original HP 32S and the range is extended to 0 - 254 for the HP 32SII.
I didn't remember that the range was up to 254 for the 32SII. That's perfectly correct of course. Thanks for reminding us.

(07-18-2021 06:17 AM)C.Ret Wrote:  I have not fully understand why this spare memory since, two registers may be more than one.
The 32S/32SII have so little memory that using numeric constants (other than the 0..99 or 0..254) in a program must be avoided. For instance, it is much better to do 12 +/- rather than -12). To enter large constants (e.g. 1000), we have better to do for instance 10 ENTER x^2 * or if speed is not a concern 3 10^x.

Quote:EDIT:
In this sunny sunday mornig, I was posting this message when by rereading it, a shorter and simpliest version pop up in my awaking brain that use a MOD instruction :

Code:
01 LBL "SQYX2" 02 ENTER^  SQRT  INT  MOD  LSTX 06 END
Unfortunately, the 32S/32SII lack the MOD function.

J-F
 « Next Oldest | Next Newest »