The Museum of HP Calculators

HP Forum Archive 12

[ Return to Index | Top of Index ]

HP-41 Challenge!
Message #1 Posted by Jeff on 24 Apr 2003, 8:14 p.m.

If you have a number in X. You need to test this number against the values: 1,2.5,3.125 X<=Y? or similar test. You need to perform (4) Different actions based on the results of the tests. If X<=1.0 then add .1 If X<=2.5 then Multiply by 1.1 If X<=3.125 then add .2 Else Multiply by 1.08 The HP-48GX has the CASE function which performs this very well. What is the best solution for the HP-41? I have tried using the GTO statements and LBL statements. I have tried using flags to direct the program execution but I think that my attemps, Although they work,are not quite good. What is the best Byte/Time ratio that can be obtained?

      
Re: HP-41 Challenge!
Message #2 Posted by Johnny Billquist on 25 Apr 2003, 8:42 a.m.,
in response to message #1 by Jeff

1 - X<=0? GTO 00 1.5 - X<=0? GTO 01 0.625 - X<=0? GTO 02 3.125 + 1.08 * GTO 03 LBL 00 2 + GTO 03 LBL 01 2.5 + 1.1 * GTO 03 LBL 02 3.325 + LBL 03

That's my suggestion anyway.

            
Re: HP-41 Challenge!
Message #3 Posted by Vieira, Luiz C. (Brazil) on 25 Apr 2003, 11:27 a.m.,
in response to message #2 by Johnny Billquist

Hello, John; may I? I just arrengend (formatted) your solution.

1 
- 
X<=0? 
GTO 00 
1.5 
- 
X<=0? 
GTO 01 
0.625 
- 
X<=0? 
GTO 02 
3.125 
+ 
1.08 
* 
GTO 03 
LBL 00 
2 
+ 
GTO 03 
LBL 01 
2.5 
+ 
1.1 
* 
GTO 03 
LBL 02 
3.325 
+ 
LBL 03

here the original program continues, right? May I use your suggestion and make it work like a subroutine call? This way it would look like:

1 
- 
X<=0? 
GTO 00 
1.5 
- 
X<=0? 
GTO 01 
0.625 
- 
X<=0? 
GTO 02 
3.125 
+ 
1.08 
* 
RTN
LBL 00 
2 
+ 
RTN
LBL 01 
2.5 
+ 
1.1 
* 
RTN
LBL 02 
3.325 
+ 
RTN

I hope you don't be mad with me... This is just another way to use your solution.

Best regards.

Luiz C. Vieira - Brazil

                  
Re: HP-41 Challenge!
Message #4 Posted by Jean-Marc Baillard on 25 Apr 2003, 4:57 p.m.,
in response to message #3 by Vieira, Luiz C. (Brazil)

Hi, everyone! here is a solution to this challenge:

1 X<Y? GTO 01 + RTN LBL 01 CLX 2.5 X<Y? GTO 01 CLX 1.1 * RTN LBL 01 CLX 3.125 X<Y? GTO 01 CLX .2 + RTN LBL 01 CLX 1.08 * RTN ( 28 lines , 43 bytes )

Best regards, JMB.

                        
Re: HP-41 Challenge!
Message #5 Posted by Dave Shaffer on 25 Apr 2003, 6:38 p.m.,
in response to message #4 by Jean-Marc Baillard

Try turning it around - check from the high side first.

In Luiz'z notation (and using x<>y for interchange x and y) and assuming the value to be tested is already in x:

3.125 x<>y x>y? GTO 00 2.0 x<>y x>y? GTO 01 1.0 x<>y x>y GTO 02 0.1 + GTO 03 LBL 00 1.08 * GTO 03 LBL 01 0.2 + GTO 03 LBL 02 1.1 * LBL 03 (resume whatever you were doing here)

                              
Re: HP-41 Challenge!
Message #6 Posted by Vieira, Luiz C. (Brazil) on 25 Apr 2003, 7:58 p.m.,
in response to message #5 by Dave Shaffer

Hello, Dave;

yours is a very elegant solution. If I am not wrong, it seems to me Jeff's original verrsion resembles yours. At least I remeber seeing this x<>y comparison "structure" a few months ago.

Is it correct, Jeff?

Best regards. (It seems a lot of us still program -or remember how to- under RPN with style...)

Luiz C. Vieira - Brazil

                                    
Re: HP-41 Challenge!
Message #7 Posted by Dave Shaffer on 25 Apr 2003, 11:46 p.m.,
in response to message #6 by Vieira, Luiz C. (Brazil)

Luiz,

Thank you for your kind words. I sort of messed up my presentation - I intended to have line breaks at each step, as you have showed us how to do! (It looked so good before I sent it!)

I suspect that with a little more attention paid to the x and y variables on the stack and the order of the comparison (greater than or less than) that you could save even a few more steps.

To Jeff: did you really have a problem where this particular order of comparison and subsequent arithmetic was needed?

                                          
You mean like this?
Message #8 Posted by Vieira, Luiz C. (Brazil) on 26 Apr 2003, 12:28 a.m.,
in response to message #7 by Dave Shaffer

Hello, Dave;

some souls are gone, others are about to come, but kindness and politeness are timeless, isn't it? That's what makes this Forum rule: people in here are amazingly kind and polite. Thank you too, Dave; my pleasure.

About the listing: if you wnat something to be shown the way you typed it in, start it with a [pre] and finish it with a [/pre]. Both [pre] and [/pre] will be invisible; to show them it's necessary to use the \\[ and \\] trick. Look at your own listing below:

3.125
x<>y
x>y? 
GTO 00 
2.0 
x<>y 
x>y? 
GTO 01 
1.0 
x<>y 
x>y 
GTO 02 
0.1 
+ 
GTO 03 
LBL 00 
1.08 
* 
GTO 03 
LBL 01 
0.2 
+ 
GTO 03 
LBL 02 
1.1 
* 
LBL 03
(resume whatever you were doing here)

Was it what you wnated? the [pre] and [/pre] are Dave Hicks' gifts for us, amongst others... PLease, have a look here and you'll understand better.

Best regards.

Luiz C. Vieira - Brazil

                                                
Re: You mean like this?
Message #9 Posted by Jeff on 26 Apr 2003, 8:46 a.m.,
in response to message #8 by Vieira, Luiz C. (Brazil)

Thanks to all who posted responses on this thread. I have taken the liberty of compiling all the responses into a list and will be trying each one out for byte/time. I have been working on this routine for a while and thought that there surely was a better method to complete what I needed to have done. This is the reason that I posted this challenge. Below is the list. I will post the byte counts and input/outputs sometime this weekend. Again thanks to all.

E X<>Y X<=Y? GTO 01 2.5 X<>Y X<=Y? GTO 02 3.125 X<>Y X<=Y? GTO 03 1.08 * RTN LBL 01 .1 + RTN LBL 01 1.1 + RTN LBL 03 .2 + RTN "Jeff" my original program.

1 - X<=0? GTO 00 1.5 - X<=0? GTO 01 0.625 - X<=0? GTO 02 3.125 + 1.08 * GTO 03 LBL 00 2 + GTO 03 LBL 01 2.5 + 1.1 * GTO 03 LBL 02 3.325 + LBL 03

1 - X<=0? GTO 00 1.5 - X<=0? GTO 01 0.625 - X<=0? GTO 02 3.125 + 1.08 * GTO 03 LBL 00 2 + GTO 03 LBL 01 2.5 + 1.1 * GTO 03 LBL 02 3.325 + LBL 03

1 - X<=0? GTO 00 1.5 - X<=0? GTO 01 0.625 - X<=0? GTO 02 3.125 + 1.08 * RTN LBL 00 2 + RTN LBL 01 2.5 + 1.1 * RTN LBL 02 3.325 + RTN

1 X<Y? GTO 01 + RTN LBL 01 CLX 2.5 X<Y? GTO 01 CLX 1.1 * RTN LBL 01 CLX 3.125 X<Y? GTO 01 CLX .2 + RTN LBL 01 CLX 1.08 * RTN

3.125 x<>y x>y? GTO 00 2.0 x<>y x>y? GTO 01 1.0 x<>y x>y GTO 02 0.1 + GTO 03 LBL 00 1.08 * GTO 03 LBL 01 0.2 + GTO 03 LBL 02 1.1 * LBL 03

3.125 x<>y x>y? GTO 00 2.0 x<>y x>y? GTO 01 1.0 x<>y x>y GTO 02 0.1 + GTO 03 LBL 00 1.08 * GTO 03 LBL 01 0.2 + GTO 03 LBL 02 1.1 * LBL 03

                                                      
Re: You mean like this?
Message #10 Posted by Jeff on 26 Apr 2003, 9:11 p.m.,
in response to message #9 by Jeff

Thanks again to all that posted regarding this thread. Here are the results for the challenge. Johnny Billquist 64 Bytes/.64 seconds=100 BT Jean-Marc Baillard 53 Bytes/.50 seconds=106 BT Dave Shaffer 54 Bytes/1.04 seconds=51.92 BT Jeff Davis 53 Bytes/.71 seconds=74.67 BT Congratulations Jean-Marc Baillard! These byte counts include an .END and a LBL. Thanks to all. If there are further thoughts to make this sequence faster or shorter please let us know. Best Regards, Jeff

                                                
Re: You mean like this?
Message #11 Posted by Johnny Billquist on 28 Apr 2003, 6:49 a.m.,
in response to message #8 by Vieira, Luiz C. (Brazil)

This solution is a bit shorter than mine, but offers the stack for it. It depends on what you need. I don't like it just because I want to keep stack usage to a minimum, but apart from that it's efficient.

                                          
Re: HP-41 Challenge!
Message #12 Posted by Jeff on 27 Apr 2003, 2:25 p.m.,
in response to message #7 by Dave Shaffer

This particular routine, is being used to calculate a safe distance between centers that will be used in a subsequent calculation. The subsequent calculation will determine how many cavities can fit into a mold using a square or round pattern. Similar to wire packings. Thanks to all, Jeff

                  
Re: HP-41 Challenge!
Message #13 Posted by Johnny Billquist on 28 Apr 2003, 6:50 a.m.,
in response to message #3 by Vieira, Luiz C. (Brazil)

Thanks. I didn't realize it would become so messed up. I wrote it the way you formatted it for me.

Much obliged. I'll try to remember the \[pre\] next time, now that I know.

And yes, it might be prettier to have as a subroutine.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall