04-24-2014, 12:32 AM (This post was last modified: 05-17-2017 12:37 PM by Gerson W. Barbosa.)
Post: #1
 Gerson W. Barbosa Senior Member Posts: 1,272 Joined: Dec 2013
Code:
00 { 25-Byte Prgm }                         01 LBL "Q"                      02 X<> ST Z                                         03 +/- 04 STO/ ST Z 05 STO+ ST X  06 /                  07 STO ST Z         08 X^2                 09 +              10 SQRT       11 RCL+ ST Y                      12 X<>Y                13 RCL- ST L  14 END

Examples:

1) Given x² - 5x + 6 = 0, compute 123456*x₁ and 123456*x₂

.................... 123456 ENTER 1 ENTER 5 +/- ENTER 6 XEQ Q -> Y: 3 ; x₂ = 3
................................................................ X: 2 ; x₁ = 2
........................................................ Rv * -> X: 370,368 ; 123456*x₂
........................................................ Rv * -> X: 246,912 ; 123456*x₁

2) 3x² - 12x + 87 = 0

............................... 3 ENTER 12 +/- ENTER 87 XEQ Q -> Y: 2 .i5 ; x₂ = 2 + 5i
................................................................ X: 2 -i5 ; x₁ = 2 - 5i

3) x² - (4 + 6i)x + (-5 + 10i) = 0

.. 1 ENTER 4 ENTER 6 COMPLEX +/- 5 +/- ENTER 10 COMPLEX XEQ Q -> Y: 3 i4 ; x₂ = 3 + 4i
................................................................ X: 1 i2 ; x₁ = 1 + 2i

Edited to change code. Now it is one step and one byte shorter. This is the older code:

Code:
00 { 26-Byte Prgm }                         01 LBL "Q"                      02 X<> ST Z                                         03 +/- 04 STO/ ST Z 05 /                        06 2                   07 /                  08 STO ST Z         09 X^2                 10 +              11 SQRT       12 RCL+ ST Y                      13 X<>Y                14 RCL- ST L  15 END

Edited again to change subject line.
05-25-2014, 03:30 PM (This post was last modified: 05-26-2014 11:47 AM by Jeff_Kearns.)
Post: #2
 Jeff_Kearns Member Posts: 147 Joined: Dec 2013

Jeff K
05-25-2014, 09:55 PM (This post was last modified: 05-25-2014 09:56 PM by Gerson W. Barbosa.)
Post: #3
 Gerson W. Barbosa Senior Member Posts: 1,272 Joined: Dec 2013
(05-25-2014 03:30 PM)Jeff_Kearns Wrote:  It was interesting to read the evolution of this routine in your 2010 thread here.

Jeff,

The influence and inspiration that led me to this even shorter version are described in this thread from 2012:

Fast Quadratic Formula for the HP-41C

Gerson.
05-27-2014, 05:30 PM (This post was last modified: 05-28-2014 10:21 PM by Jeff_Kearns.)
Post: #4
 Jeff_Kearns Member Posts: 147 Joined: Dec 2013
RE: Short Quadratic Solver (HP-42S) --- and 32Sii??
(05-25-2014 09:55 PM)Gerson W. Barbosa Wrote:  Thank you for your interest!

Gerson,

I am interested! The following routine for the HP-32sii and HP-33s (slightly modified from Eddie Shore's original to display the roots as x + iy) is the one I use but it is 39 steps long!

Q0001 LBL Q ' Set label Q
Q0002 INPUT A
Q0003 INPUT B
Q0004 INPUT C
Q0005 SQ(B)-4xAxC ' Equation (enter by RS [right shift] EQN)
Q0006 STO D
Q0007 RCL D ' Is the discriminant negative?
Q0008 x<0?
Q0009 SF 1 ' Flag 1 - indicate that the roots are complex
Q0010 RCL D
Q0011 FS? 1
Q0012 +/- ' The HP 33 cannot take square roots of x<0
Q0013 square root
Q0014 STO E ' E = abs(sqrt(D))
Q0015 -B/(2xA) ' enter this as an equation
Q0016 STO F
Q0017 E/(2xA) ' enter this an an equation
Q0018 STO G
Q0019 RCL G
Q0020 0
Q0021 FS? 1
Q0022 x<>y
Q0023 RCL F
Q0024 0
Q0025 CMPLX+
Q0026 x<>y
Q0027 STOP ' press the R/S key: display first root
Q0028 RCL G
Q0029 +/-
Q0030 0
Q0031 FS? 1
Q0032 x<>y
Q0033 RCL F
Q0034 0
Q0035 CMPLX+
Q0036 x<>y
Q0037 STOP
Q0038 CF 1 ' clean up command
Q0039 RTN

Can you think of a way of dramatically shortening it, in line with your 15-liner for the HP-42s, so that it still gives real and complex roots? The memory in the HP-32Sii is so limited and shortening this program might allow me to squeeze one more program in there...

Thanks,

Jeff K
05-27-2014, 06:57 PM
Post: #5
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
(05-27-2014 05:30 PM)Jeff_Kearns Wrote:  Can you think of a way of dramatically shortening it, (...) so that it still gives real and complex roots?

Copied most of it from here: Short quadratic solver (HP-15C)
Code:
Q01 LBL Q Q02 ENTER Q03 R^ Q04 / Q05 R^ Q06 LASTx Q07 / Q08 2 Q09 +/- Q10 / Q11 ENTER Q12 ENTER Q13 x^2 Q14 R^ Q15 - Q16 SQRT Q17 ENTER Q18 +/- Q19 CMPLX- Q20 RTN

Doesn't give you the complex solution but that should be easy to add. There are probably shorter solutions possible by using a register.

Cheers
Thomas
05-28-2014, 04:18 AM
Post: #6
 Gerson W. Barbosa Senior Member Posts: 1,272 Joined: Dec 2013
(05-27-2014 06:57 PM)Thomas Klemm Wrote:
(05-27-2014 05:30 PM)Jeff_Kearns Wrote:  Can you think of a way of dramatically shortening it, (...) so that it still gives real and complex roots?

Copied most of it from here: Short quadratic solver (HP-15C)
Code:
Q01 LBL Q Q02 ENTER Q03 R^ Q04 / Q05 R^ Q06 LASTx Q07 / Q08 2 Q09 +/- Q10 / Q11 ENTER Q12 ENTER Q13 x^2 Q14 R^ Q15 - Q16 SQRT Q17 ENTER Q18 +/- Q19 CMPLX- Q20 RTN

Doesn't give you the complex solution but that should be easy to add. There are probably shorter solutions possible by using a register.

Cheers
Thomas

Nice use of CMPLX- for saving a step or two!

This might do while we don't think of something better:

Code:
 Q01 LBL Q Q02 CF 1 Q03 ENTER Q04 R^ Q05 / Q06 R^ Q07 LASTx Q08 / Q09 -2 Q10 / Q11 ENTER Q12 ENTER Q13 x^2 Q14 R^ Q15 - Q16 x<0? Q17 SF 1 Q18 ABS Q19 SQRT Q20 FS? 1 Q21 RTN Q22 ENTER Q23 +/- Q24 CMPLX- Q25 RTN

If the roots are complex, the flag 1 announciator will be lit and the real and complex parts will be in registers Y and X, respectively.

Cheers,

Gerson[/code]
05-28-2014, 10:17 PM (This post was last modified: 05-28-2014 11:12 PM by Jeff_Kearns.)
Post: #7
 Jeff_Kearns Member Posts: 147 Joined: Dec 2013
(05-28-2014 04:18 AM)Gerson W. Barbosa Wrote:  If the roots are complex, the flag 1 annunciator will be lit and the real and complex parts will be in registers Y and X, respectively.

Cheers,

Gerson

Gerson -

The code (without INPUT prompts) works OK for real roots but not for complex roots (IMHO)...

I prefer to see the results (if complex) displayed as x + iy, as per the 39 step routine based on Eddie Shore's program to which I added a couple of x<>y statements to display 'correctly'. There are two issues with this particular routine that I can't quite figure out:

1) even when I insert a x<>y before the final RTN, I still get the imaginary part in x, i.e. ix + y. With or without a x<>y before the final RTN, the displayed result is the same.

2) Only one complex root is solved correctly. As an example, if you try solving 5x² + 2x + 1 = 0, you get the following: Flag 1 is set with the imaginary part 0.4 in x, and the real part -0.2 in y. Upon R/S the second root (x = -0.2 - 0.4i) is not shown. Granted, all complex roots are of the form x = D ± Ei, so one can deduce the second root and it doesn't really matter I guess BUT...

Can you suggest a better solution?

Regards,

Jeff K
05-29-2014, 04:23 AM (This post was last modified: 05-29-2014 03:53 PM by Gerson W. Barbosa.)
Post: #8
 Gerson W. Barbosa Senior Member Posts: 1,272 Joined: Dec 2013
(05-28-2014 10:17 PM)Jeff_Kearns Wrote:
(05-28-2014 04:18 AM)Gerson W. Barbosa Wrote:  If the roots are complex, the flag 1 annunciator will be lit and the real and complex parts will be in registers Y and X, respectively.

Cheers,

Gerson

Gerson -

The code (without INPUT prompts) works OK for real roots but not for complex roots (IMHO)...

I prefer to see the results (if complex) displayed as x + iy, as per the 39 step routine based on Eddie Shore's program to which I added a couple of x<>y statements to display 'correctly'. There are two issues with this particular routine that I can't quite figure out:

1) even when I insert a x<>y before the final RTN, I still get the imaginary part in x, i.e. ix + y. With or without a x<>y before the final RTN, the displayed result is the same.

2) Only one complex root is solved correctly. As an example, if you try solving 5x² + 2x + 1 = 0, you get the following: Flag 1 is set with the imaginary part 0.4 in x, and the real part -0.2 in y. Upon R/S the second root (x = -0.2 - 0.4i) is not shown. Granted, all complex roots are of the form x = D ± Ei, so one can deduce the second root and it doesn't really matter I guess BUT...

Can you suggest a better solution?

Regards,

Jeff K

Jeff,

I hope this is better, although not so short anymore:

Code:
 Q01 LBL Q Q02 CF 1 Q03 ENTER Q04 R↑ Q05 ÷ Q06 R↑ Q07 LASTx Q08 ÷ Q09 ⁻2 Q10 ÷ Q11 ENTER Q12 ENTER Q13 x² Q14 R↑ Q15 - Q16 x<0? Q17 SF 1 Q18 ABS Q19 SQRT Q20 ENTER Q21 +/- Q22 CMPLX+ Q23 FS? 1 Q24 CMPLX- Q25 FS? 1 Q26 R↑ Q27 RTN CK=3BA1 LEN 048.5 (HP-32SII)

Usage examples on the HP-32SII

1 ENTER 5 +/- ENTER 6 XEQ Q --> 2 x<>y --> 3 (Two real roots, because the flag 1 annunciator is off)

1 ENTER 1 +/- ENTER 1 XEQ Q --> 0.5 x<>y 8.66025404E-1
R↓ 0.5 x<>y -0.86602540378 (Two complex roots, 0.5 ± (√3)÷2 i, because the flag 1 annunciator is on)

Alternatively, you can use a slightly modified version of Eddie Shore's program to save a few steps:

Code:
 Q0001 LBL Q  Q0002 CF 1 Q0003 INPUT A Q0004 INPUT B Q0005 INPUT C Q0006 SQ(B)-4×A×C  Q0007 x<0? Q0008 SF 1  Q0009 ABS Q0010 √x Q0011 STO E  Q0012 -B÷2÷A Q0013 STO F Q0014 E÷2÷A Q0015 STO G Q0016 0 Q0017 FS? 1 Q0018 x<>y Q0019 RCL F Q0020 0 Q0021 CMPLX+ Q0022 x<>y Q0023 STOP  Q0024 RCL G Q0025 +/- Q0026 0 Q0027 FS? 1 Q0028 x<>y Q0029 RCL F Q0030 0 Q0031 CMPLX+ Q0032 x<>y Q0033 RTN CK=545E LN=169    (hp 33s)

Regards,

Gerson.

P.S.: Regarding issue #1, that happens because the first RTN instruction (step Q21), not the last, is executed when flag 1 is set.
05-29-2014, 05:10 AM
Post: #9
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
(05-29-2014 04:23 AM)Gerson W. Barbosa Wrote:
Code:
Q0007 STO D

You can remove this step as D is never used.

Cheers
Thomas
05-29-2014, 03:48 PM
Post: #10
 Gerson W. Barbosa Senior Member Posts: 1,272 Joined: Dec 2013
(05-29-2014 05:10 AM)Thomas Klemm Wrote:
(05-29-2014 04:23 AM)Gerson W. Barbosa Wrote:
Code:
Q0007 STO D

You can remove this step as D is never used.

Duely removed above. Thank you very much!

Gerson.
05-30-2014, 03:56 AM
Post: #11
 Jeff_Kearns Member Posts: 147 Joined: Dec 2013
(05-29-2014 03:48 PM)Gerson W. Barbosa Wrote:  Thank you very much!
Gerson.
And I thank you both! --- Jeff K
05-31-2014, 03:31 AM
Post: #12
 Gerson W. Barbosa Senior Member Posts: 1,272 Joined: Dec 2013
(05-30-2014 03:56 AM)Jeff_Kearns Wrote:
(05-29-2014 03:48 PM)Gerson W. Barbosa Wrote:  Thank you very much!
Gerson.
And I thank you both! --- Jeff K

You're most welcome!

BTW, the HP-42S code (post #1) has been shortened to 14 steps and 25 bytes, including LBL and END. Perhaps a new world record for the HP-42S :-) (Not being the most accurate, at least that might be the shortest one ever - well, until someone manages to save yet another step or byte)

Regards,

Gerson.
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)