Post Reply 
(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
STO 1
0
STO 0
Rv
LBL 1
10
/
INT
STO 1
LSTx
FRAC
10
STOx0
x
STO+0
RCL 1
X≠0
GTO 1
RCL 0
RTN

Example: 123456 f [A] > 654321

Gamo
Find all posts by this user
Quote this message in a reply
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.

X<0 and X<0 = X≥0

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
Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
06-04-2018, 01:27 PM
Post: #4
RE: (11C) Reverse Integer
(06-04-2018 01:17 PM)Gamo Wrote:  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.

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
Find all posts by this user
Quote this message in a reply
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
002 STO 0
003 STO-0
004 LBL 1
005 1
006 0
007 ÷
008 INT
009 LstX
010 FRAC
011 1
012 0
013 STOx0
014 x
015 STO+0
016 R↓
017 X≠0?
018 GTO 1
019 RCL 0
020 RTN

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
Find all posts by this user
Quote this message in a reply
06-04-2018, 09:12 PM
Post: #6
RE: (11C) Reverse Integer
16 steps:

Code:
001 LBL A
002 STO 0
003 STO- 0
004 LBL 0
005 INT
006 1
007 0
008 STO* 0
009 ÷
010 FRAC
011 STO+ 0
012 LASTX
013 x#0?
014 GTO 0
015 RCL 0
016 RTN
Find all posts by this user
Quote this message in a reply
06-04-2018, 10:05 PM
Post: #7
RE: (11C) Reverse Integer
(06-04-2018 09:12 PM)Didier Lachieze Wrote:  16 steps:

Great.
This is the fourth version, and each was an improvement over the previous one.
Isn't this what forums are all about?

Dieter
Find all posts by this user
Quote this message in a reply
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 Smile

Gamo
Find all posts by this user
Quote this message in a reply
06-05-2018, 10:48 AM
Post: #9
RE: (11C) Reverse Integer
(06-05-2018 01:17 AM)Gamo Wrote:  Dieter version took 7.20 second
Didier version took 6.39 second

How do you get these times – with an accuracy of 1/100 second?

Dieter
Find all posts by this user
Quote this message in a reply
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
Didier version took 6.39 second

How do you get these times – with an accuracy of 1/100 second?

Dieter

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
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)