The Museum of HP Calculators

HP Forum Archive 06

[ Return to Index | Top of Index ]

Programming Challenge
Message #1 Posted by Luca de Alfaro on 7 Nov 2001, 9:51 p.m.

Here is a programming challenge for the RPN addicts. Write a program that takes as input in the stack:
z: a
y: b
x: c
and outputs
y: (-b - sqrt(b^2 - 4ac))/(2a)
x: (-b + sqrt(b^2 - 4ac))/(2a)
(the solutions to the 2nd degree equation, assuming that they are real).
The program should use only the stack and the Last_x register, but no other registers. The solution should be developed for the 11C or 15C or 32SII architecture (i.e., no HP48 extensions allowed, and in particular, no PICK instructions).
I have a 35-ish step solution that uses Last_x. The solution is not particularily optimized, and I am not sure that Last_x is really needed.
The contest is open for the shortest solution, and for a solution without Last_x!

Luca

      
Re: Programming Challenge
Message #2 Posted by Vieira, Luiz C. (Brazil) on 8 Nov 2001, 12:09 a.m.,
in response to message #1 by Luca de Alfaro

Hello;

I wrote this based in an example given at the HP42S Programming Examples and Technics (p.75), which is based in the original for the HP41, published in one HP Key Notes V6 N2, p. 14. The small routine was created by Paul Baker (Stillwaker, Oklahoma) and I include it's original in here, too. This program changed my way of searching for a solution. Both roots are found AND complex solution was available (flag 0 set). This is the original listing:

LBL SOL
X<> Z
ST/ Z
/
-2
/
ENTER^
ENTER^
X^2
R^
CF 00
X>Y?
SF 00
-
ABS
SQRT
ST- Z
X<>Y
FC? 00
+
.END.
Just enter a, b and c and run it. If flag 0 is set, complex results. This is my (morphed) listing for the HP11C. Enter a, b and c and run it.
ENTER
R^
÷
R^
LASTx
÷
2
CHS
÷
ENTER
ENTER
x^2
R^
-
SF 0
X<0
CF 0
ABS
sqrt
ENTER
R^
x<>y
-
LASTx
R^
F? 0
+
If there is nothing wrong, it should work, even for complex results (real in X, imaginary in Y. Or the opposite...). In this program, flag 0 is clear for complex results. It is 27 steps long (29 if LBL and RTN or R/S are added), but I believe wizards in here will make it look too long...

My R$0.25

            
Re: Programming Challenge - One step shorter
Message #3 Posted by Vieira, Luiz C. (Brazil) on 8 Nov 2001, 9:21 p.m.,
in response to message #2 by Vieira, Luiz C. (Brazil)

Hello;

The program works fine without one step.

ENTER
R^
÷
R^
LASTx
÷
2
CHS
÷
ENTER
ENTER
x^2
R^
-
SF 0
X<0
CF 0
ABS
sqrt
ENTER  <- this ENTER can be removed
R^
x<>y
-
LASTx
R^
F? 0
+

Anything else is kept. Now it is 26 step long plus LBL and RTN or R/S.

            
Re: Programming Challenge
Message #4 Posted by Luca de Alfaro on 9 Nov 2001, 2:38 a.m.,
in response to message #2 by Vieira, Luiz C. (Brazil)

Very nice solutions! Luca

      
Re: Programming Challenge
Message #5 Posted by Fred Lusk (CA) on 8 Nov 2001, 9:40 p.m.,
in response to message #1 by Luca de Alfaro

Greetings...

I whipped this out in about 10 minutes. It is part elegent and part brute force (i.e. I'm sure it can be improved), and is for the HP-42S. I don't have the other models. Sorry.

01 LBL "QE" 02 RCLx ST Z 03 4 04 x 05 X<>Y 06 ENTER 07 X^2 08 RCL- ST Z 09 SQRT 10 R^ [ROLL UP] 11 -2 12 x 13 X<>Y 14 STO- ST Z 15 STO+ ST T 16 R\ [ROLL DOWN] 17 STO/ ST Z 18 / 19 END

Fred

            
Re: Programming Challenge
Message #6 Posted by Fred Lusk on 8 Nov 2001, 9:48 p.m.,
in response to message #5 by Fred Lusk (CA)

Well, it was in columns when I typed it. Let's try it again. BTW...how did you get two fonts in your messages? Are there some codes I need to know?

01 LBL "QE"

02 RCLx ST Z

03 4

04 x

05 X<>Y

06 ENTER

07 X^2

08 RCL- ST Z

09 SQRT

10 R^ [ROLL UP]

11 -2

12 x

13 X<>Y

14 STO- ST Z

15 STO+ ST T

16 R\ [ROLL DOWN]

17 STO/ ST Z

18 /

19 END

Fred

                  
Re: Programming Challenge
Message #7 Posted by Vieira, Luiz C (Brazil) on 9 Nov 2001, 3:49 a.m.,
in response to message #6 by Fred Lusk

Hello;

try Advanced Format Technics http://www.hpmuseum.org/artfmt.htm. Your listing would be like this:

01 LBL "QE"
02 RCLx ST Z
03 4
04 x
05 X<>Y
06 ENTER
07 X^2
08 RCL- ST Z
09 SQRT
10 R^ [ROLL UP]
11 -2
12 x
13 X<>Y
14 STO- ST Z
15 STO+ ST T
16 R\ [ROLL DOWN]
17 STO/ ST Z
18 /
19 END 

Just place a [pre] in the start of it and a [/pre] at its end. Cheers.

                        
Re: Programming Challenge
Message #8 Posted by Fred Lusk (CA) on 11 Nov 2001, 12:55 a.m.,
in response to message #7 by Vieira, Luiz C (Brazil)

Luiz....

Thanks. Now I remember having seen that before. Must have been a "senior moment" (and I'm only 43).

Fred

                              
Re: Programming Challenge
Message #9 Posted by Vieira, Luiz C. (Brazil) on 11 Nov 2001, 2:24 p.m.,
in response to message #8 by Fred Lusk (CA)

Hello;

I'm about to believe that, with some exceptions, we're most at the average of 35-40. I'm 40. So, please, having a "senior moment" is almost like "carpe diem". At least for us, 40's. (as long as it doesn't take all day long...)

Cheers.

      
Re: Programming Challenge
Message #10 Posted by Rupert (Northern Italy, EU) on 10 Nov 2001, 2:04 p.m.,
in response to message #1 by Luca de Alfaro

Some better solutions have already been posted, anyway here is my version for the HP32SII and the HP15C (for real solutions only):

ENTER
R^
/
CHS
R^
LASTx
/
2
/
CHS
X^2
LASTx
ENTER
Rv
Rv
+
SQRT
-
x<>y
LASTx
+

Cannot resist to write it for the HP41, too:

ENTER
R^
ST/ T
/
CHS
R^
2
/
CHS
STO Z
STO T
X^2
+
SQRT
-
X<>Y
LASTX
+
      
Re: Programming Challenge
Message #11 Posted by doug on 10 Nov 2001, 11:06 p.m.,
in response to message #1 by Luca de Alfaro

I am not really formally trained in math, and haven't sat in a algebra or math classroom since high school, but, are you trying to solve a quadratic equation by setting it (=0) and then entering the numerical coefficient's without having to write a parse scan routine? Just wanted to make sure I understand.

            
Re: Programming Challenge
Message #12 Posted by Vieira, Luiz C (Brazil) on 11 Nov 2001, 8:07 a.m.,
in response to message #11 by doug

Hello;

the basic challenge is: having a, b and c from

ax^2+bx+c=0  (y=0)
what is the shortest routine that gives us x1 and x2 that solve the equation. The programs herein should get a, b and c from the stack and return x1 and x2:
  ---   T     ---
   c    Z     ---
   b    Y  ->  x2
   a    X  ->  x1
  ---   L  ->  (if not used, better)
That`s what Luca proposed.

Cheers.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall