(11C) Reverse Integer
|
06-04-2018, 10:16 AM
(This post was last modified: 06-04-2018 01:20 PM by Gamo.)
Post: #1
|
|||
|
|||
(11C) Reverse Integer
This program was from a member who shared in the Facebook Calculator Group and I like to share here.
Program: Reverse Integer Quote:LBL A Example: 123456 f [A] > 654321 Gamo |
|||
06-04-2018, 12:42 PM
(This post was last modified: 06-04-2018 12:43 PM by Dieter.)
Post: #2
|
|||
|
|||
RE: (11C) Reverse Integer and Inverse Logic
(06-04-2018 10:16 AM)Gamo Wrote: To do the Inverse Logic for X≥0 program must use X<0 twice. No, this does not work. Two consecutive X<0? tests do not give a X≥0? test. This will always test true, regardless of the input. But you can easily build the missing X≥0? with X≠0? followed by X>0?. General rule: if you have two consecutive tests commands, the result is a NOT(test1) OR test2. So X≠0? followed by X>0? means X=0? OR X>0? which is exactly what you want. But then, if you want to set flag 1 for negative input, why don't you simply do it this way: CF 1 X<0? SF 1 Want it the other way round so that flag 1 is set for input ≥ 0 ? SF 1 X<0? CF 1 BTW, what is the flag 0 in the final steps used for? I cannot see that flag 0 is set or cleared anywhere. This should be a F?1 test, I think. And now for the best part: All this is not required! As far as I can see flag 1 is supposed to be set if the input is negative. This way the output is returned with negative sign as well. But this does not require any tests. No X≥0? is required, no flag, no nothing. Simply omit the complete flag and X≥0 tests: the program will return the correct result both for positive and negative input. No additional measures are required. That's the reason why your program version returns the correct results although the X<0? X<0? does not work. So you can delete all steps between LBL A up to and including LBL 2. These are not required. At the end of the program, after the GTO 1 a simple RCL 0 is all you need. Don't believe me? Try it! Dieter |
|||
06-04-2018, 01:17 PM
(This post was last modified: 06-04-2018 01:21 PM by Gamo.)
Post: #3
|
|||
|
|||
RE: (11C) Reverse Integer and Inverse Logic
Thank You Dieter
You're right !! No need to put flags and conditional test at the beginging of the program at all, I'm wondering why he put flags in this program. I'm update to shorter version without flags. Thanks again Gamo |
|||
06-04-2018, 01:27 PM
Post: #4
|
|||
|
|||
RE: (11C) Reverse Integer
(06-04-2018 01:17 PM)Gamo Wrote: You're right !! Great! And now try it with only one data register and at most 20 steps. And don't forget, every "10" requires two steps. ;-) BTW, now that the program has been significantly changed it is only loosely based on the original FB version. Dieter |
|||
06-04-2018, 05:16 PM
Post: #5
|
|||
|
|||
RE: (11C) Reverse Integer
(06-04-2018 01:27 PM)Dieter Wrote: Great! And now try it with only one data register and at most 20 steps. And don't forget, every "10" requires two steps. ;-) Here's what I got: Code: 001 LBL A Step 002/003 clear R0 "en passant" whithout disturbing the stack. And the final RTN can be omitted. Then it's merely 19 steps. ;-) Dieter |
|||
06-04-2018, 09:12 PM
Post: #6
|
|||
|
|||
RE: (11C) Reverse Integer
16 steps:
Code: 001 LBL A |
|||
06-04-2018, 10:05 PM
Post: #7
|
|||
|
|||
RE: (11C) Reverse Integer | |||
06-05-2018, 01:17 AM
Post: #8
|
|||
|
|||
RE: (11C) Reverse Integer
Thank You to Dieter and Didier Lachieze
I time both of your program with 654321 reversed to 123456 Dieter version took 7.20 second Didier version took 6.39 second This program are a good example for learning to code efficiently. Thanks again Gamo |
|||
06-05-2018, 10:48 AM
Post: #9
|
|||
|
|||
RE: (11C) Reverse Integer | |||
06-05-2018, 01:17 PM
Post: #10
|
|||
|
|||
RE: (11C) Reverse Integer
(06-05-2018 10:48 AM)Dieter Wrote:(06-05-2018 01:17 AM)Gamo Wrote: Dieter version took 7.20 second Really good question Dieter, I missed noting how accurate these are, even 1 decimal place is hard to accept if timed by hand. However I also enjoyed reading the program tweaking. Thanks to all 3 of you for a brief refresher in the timeless art of RPN program refinement. --Bob Prosperi |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)