The Museum of HP Calculators

HP Forum Archive 17

 Short quadratic solver (HP-15C)Message #1 Posted by Gerson W. Barbosa on 22 May 2007, 7:44 p.m. Two and a half months ago Allen presented an RPL program and a quadratic formula quite suitable for short programs: (scroll past the middle of the thread) This is a 22-step program based on that formula. It was written then and no further attempt has been made to size-optmize it, so it's still possible someone come up with an even shorter program. ```001- LBL E 002- ENTER 003- R^ 004- / 005- LSTx 006- R^ 007- x<>y 008- 2 009- * 010- / 011- CHS 012- ENTER 013- ENTER 014- x^2 015- R^ 016- - 017- SQRT 018- + 019- x<>y 020- LSTx 021- - 022- RTN Usage: a ENTER b ENTER c GSB E => x1 x<>y => x2 If flag 8 is set the program will also handle complex roots. ``` The record for the HP-41 appears to be a 16-step program available in the Software Library. What might be the record for the HP-15C? Gerson. P.S.: Of course, on the 33S I prefer to use Mr. Hanson's program available in the link. On my everyday 15C, however, every step counts...

 Re: Short quadratic solver (HP-15C)Message #2 Posted by Thomas Klemm on 23 May 2007, 3:42 a.m.,in response to message #1 by Gerson W. Barbosa Saved one step: ```001- LBL E 002- ENTER 003- R^ 004- / 005- R^ 006- LSTx 007- / 008- 2 009- CHS 010- / 011- ENTER 012- ENTER 013- x^2 014- R^ 015- - 016- SQRT 017- - 018- x<>y 019- LSTx 020- + 021- RTN ```

 Re: Short quadratic solver (HP-15C)Message #3 Posted by Gerson W. Barbosa on 23 May 2007, 11:48 a.m.,in response to message #2 by Thomas Klemm Quote: Saved one step Great job, Thomas! And I have noticed now the greater root always shows up first, no mater the signs of the constants (on the previous program the lesser root appears first). That's what I would prefer on a two-line display calculator. I would suggest the author of the shortest program submitted it to the Software Library (HP-15C). You're the winner so far! :-) Best regards, Gerson.

 Re: Short quadratic solver (HP-15C)Message #4 Posted by allen on 24 May 2007, 8:46 p.m.,in response to message #1 by Gerson W. Barbosa Beautiful program!!! I think it unfortunate with all of the Visual C++/Basic/... etc languages, the skill of writing efficient code is slowly becoming a niche market, rather than the mainstream. I'll try this out on my 15C once I get home!

 Re: Short quadratic solver (HP-15C)Message #5 Posted by Gerson W. Barbosa on 24 May 2007, 11:23 p.m.,in response to message #4 by allen Hello Allen, Perhaps it's time to start using the registers. I haven't been able to get anything shorter than the 21 steps Thomas had already obtained, but I think someone can still save one step or two. ```001- LBL E 002- ENTER 003- R^ 004- x<>y 005- Rv 006- / 007- STO 0 008- x<>y 009- LSTx 010- / 011- 2 012- / 013- STO 1 014- x^2 015- RCL- 0 016- SQRT 017- RCL- 1 018- ENTER 019- 1/x 020- RCL* 0 021- RTN ``` Regards, Gerson.

 Re: Short quadratic solver (HP-12C)Message #6 Posted by Kalevipoeg on 26 May 2007, 1:17 a.m.,in response to message #5 by Gerson W. Barbosa The quadratic solver on the HP-12C seems to be a real challenge : ) Case 1. Only 4-level stack is used ```01 ENTER 02 RDN 03 RDN 04 X<>Y 05 ENTER 06 + 07 LST X 08 RDN 09 CHS 10 / 11 ENTER 12 * 13 LST X 14 X<>Y 15 RDN 16 RDN 17 X<>Y 18 / 19 - 20 SQRT 21 - 22 X<>Y 23 LST X 24 + ``` Case 2. Register R0 can be used ```01 STO 0 02 RDN 03 X<>Y 04 STO/0 05 ENTER 06 + 07 CHS 08 / 09 ENTER 10 ENTER 11 ENTER 12 * 13 RCL 0 14 - 15 SQRT 16 - 17 X<>Y 18 LST x 19 + ``` Usage: a [ENTER] b [ENTER] c [R/S]

 Re: Short quadratic solver (HP-12C)Message #7 Posted by Gerson W. Barbosa on 26 May 2007, 10:17 a.m.,in response to message #6 by Kalevipoeg Congratulations! Quote: The quadratic solver on the HP-12C seems to be a real challenge : ) I do agree. The lack of R^ is a problem for stack-based programs. Using only the stack allows for slightly faster programs, but this is not an issue in this case. Your 19-step program (21 on the HP-15C) works also on the HP-33C and on the HP-15C (real and complex roots). In case you don't have a physical HP-15C, you can try a simulated one on Nonpareil if you wish. Programs for the HP-12C and other RPN calculators are also welcome. Regards, Gerson.

 Re: Short quadratic solver (HP-15C)Message #8 Posted by Kalevipoeg on 26 May 2007, 11:14 a.m.,in response to message #7 by Gerson W. Barbosa This should work on the 11C, 15C, 34C etc. I tested with my 11C. Unfortunately there is no 15C in my collection... ```001 LBL E 002 STO 0 003 RDN 004 X<>Y 005 STO/0 006 ENTER 007 + 008 CHS 009 / 010 ENTER 011 ENTER 012 x^2 013 RCL 0 014 - 015 SQRT 016 - 017 X<>Y 018 LST X 019 + 020 RTN ```

 Re: Short quadratic solver (HP-15C)Message #9 Posted by Gerson W. Barbosa on 26 May 2007, 2:34 p.m.,in response to message #8 by Kalevipoeg Quote: This should work on the 11C, 15C, 34C etc. Yes, your 20-step program does work on the HP-15C! Forgot to replace ENTER * with x^2. Sorry! Regards, Gerson.

 Re: Short quadratic solver (HP-12C)Message #10 Posted by Kalevipoeg on 26 May 2007, 3:25 p.m.,in response to message #9 by Gerson W. Barbosa It's all right. Back to the HP-12C programs. Case 1. Only the stack is used (21 steps) ```01 0 02 + 03 RDN 04 X<>Y 05 / 06 2 07 / 08 CHS 09 ENTER 10 * 11 LST X 12 X<>Y 13 RDN 14 RDN 15 / 16 - 17 SQRT 18 - 19 X<>Y 20 LST X 21 + ``` The HP-12C is getting close...

 Re: Short quadratic solver (HP-12C)Message #11 Posted by tony (nz) on 26 May 2007, 4:07 p.m.,in response to message #10 by Kalevipoeg Hello kalevipoeg! This educalc post shows we think alike, but don't let this stop further improvement :-) Cheers, Tony

 Re: Short quadratic solver (HP-12C)Message #12 Posted by Kalevipoeg on 27 May 2007, 1:34 a.m.,in response to message #11 by tony (nz) Yes, it looks quite similar to mine. This was a nice "puzzle" anyway. Thanks to Gerson.

 Re: Short quadratic solver (HP-12C)Message #13 Posted by Kalevipoeg on 27 May 2007, 7:21 a.m.,in response to message #11 by tony (nz) Some further thoughts. We use RPN (REVERSE Polish Notation) and therefore we could input data in REVERSE order :-) In this case c, b, a. We can just cut out the lines 01-04 from my first program listing and then we get 20 line program for the HP-12C. ```01 ENTER 02 + 03 LST X 04 RDN 05 CHS 06 / 07 ENTER 08 * 09 LST X 10 X<>Y 11 RDN 12 RDN 13 X<>Y 14 / 15 - 16 SQRT 17 - 18 X<>Y 19 LST X 20 + ``` Example. f(x) = x^2 + 5x + 4 ```4 [ENTER] 5 [ENTER] 1 [R/S] running....-1.0000 [X<>Y].....-4.0000 ``` What might be the record for the HP-15C by using reverse input?

 Re: Short quadratic solver (HP-12C)Message #14 Posted by Gerson W. Barbosa on 27 May 2007, 10:37 a.m.,in response to message #13 by Kalevipoeg Quote: What might be the record for the HP-15C by using reverse input? At first glance, it appears the optimum input order would be b, c and a. So, we should delete lines 002 through 005 in my second program and get this: ```001- LBL E 002- / 003- STO 0 004- x<>y 005- LSTx 006- / 007- 2 008- / 009- STO 1 010- x^2 011- RCL- 0 012- SQRT 013- RCL- 1 014- ENTER 015- 1/x 016- RCL* 0 017- RTN ``` Using your example: ```f(x) = x^2 + 5x + 4 5 [ENTER] 4 [ENTER] 1 [GSB] [E] running.... -4.0000 [X<>Y]..... -1.0000 ``` However, this is not the most natural way to enter the constants. Besides, this is not the record for this case, as you still could cut one step or two :-)

Go back to the main exhibit hall