Programming Challenge



#13

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


#14

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


#15

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.

#16

Very nice solutions!
Luca

#17

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


#18

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


#19

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.


#20

Luiz....

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

Fred


#21

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.

#22

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
+
#23

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.


#24

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.


Possibly Related Threads...
Thread Author Replies Views Last Post
  HHC 2013 RPN Programming Challenge - Final Thought Jeff O. 7 823 10-17-2013, 11:02 AM
Last Post: Jeff O.
  HHC 2012 RPN Programming Challenge Conundrum Jeff O. 15 1,193 10-08-2012, 03:34 PM
Last Post: Gerson W. Barbosa
  Mini-challenge: HHC2012 RPL programming contest with larger input David Hayden 14 1,245 10-05-2012, 10:36 PM
Last Post: David Hayden
  Weekend programming challenge: Euler's Totient function Allen 36 2,334 06-03-2012, 10:39 PM
Last Post: Paul Dale
  A Sunday Programming Challenge Allen 61 3,078 03-17-2012, 06:48 PM
Last Post: Allen
  HHC2012 programming challenge?? Pal G. 28 1,802 09-25-2011, 05:02 PM
Last Post: Paul Dale
  HHC MMC Programming challenge inC code David Hayden 8 747 12-31-2010, 09:40 AM
Last Post: David Hayden
  Programming Challenge Namir 16 1,335 09-27-2010, 01:13 PM
Last Post: Namir
  RE: 35s sorting routine challenge - Gene's Challenge Miguel Toro 4 552 08-01-2007, 08:36 AM
Last Post: Miguel Toro
  HP-16C programming challenge - Hamming distance Eric Smith 9 776 01-27-2006, 12:53 AM
Last Post: Eric Smith

Forum Jump: