Post Reply 
1089 Magic Trick
06-18-2018, 08:45 AM (This post was last modified: 06-18-2018 09:29 AM by Dieter.)
Post: #2
RE: 1089 Magic Trick
(06-18-2018 03:56 AM)Gamo Wrote:  My program will do any reversed digits and make sure that the top digits is larger than the second digits so that when subtracting the result will not be negative.

Yes, the program checks whether R1≤R2 and then it calculates either R1–R2 or R2–R1 to ensure a positive result. But... why don't you simply use ABS(R1–R2)?
You could also use  X>Y? X<>Y . This makes sure the larger number is in Y and the smaller one in X.

(06-18-2018 03:56 AM)Gamo Wrote:  The problem is there are 648 legitimate 3-digits numbers with no repeated digits and 136 of them produce 198 rather than 1089
...
My reversed result cannot include 0 at the beginning of some specific digits when calculate result is 198 instead of 1089

Example: 132 reversed to 231 then 231-132=099 reverse to 990+099=1089

...while your program reverses the 99 to 99 again so that the result is 198.

That's because you simply copied a digit reversal routine from an earlier thread, without adapting it to this special task. The routine stops the reversing process as soon as the remaining digits are all zero. That's what the x≠0? test is for. But in this special case the input and output always have three digits. So while the present routine reverses a "99" correctly into "99" again, we have to reverse "099" into "990". This means that the loop must not stop if the remaining digits are zero, instead the loop has to be executed exactly 4 times (four, not three because of the way it works).

I have adjusted the reversal routine accordingly, and it also preserves the Y and Z register now so that no storage register is required for the intermediate results.

Code:
LBL A
ENTER    // keep original number on stack
GSB 0    // reverse it
-
ABS      // | original - reversed |
ENTER    // save this on stack
GSB 0    // reverse this
+        // add to previous number
RTN
--------------------
LBL 0    // digit reversal routine
STO 0
STO-0
4        // repeat the following loop
STO I    // exactly 4 times
R↓
LBL 1
1
0
STOx0
÷
ENTER
FRAC
STO+0
-
DSE      // DSE I on 15C
GTO 1
R↓
RCL 0
RTN

235 [A] => 1089
132 [A] => 1089
012 [A] => 1089

Check the reversal routine:

001 [GSB] 0 => 100
012 [GSB] 0 => 210
123 [GSB] 0 => 321

Edit: if you want to see the intermediate results, insert PSE commands after each of the two GSB 0 and another one after the ABS.

235 [A] => "532" ... "297" ... "792" ... 1089

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
1089 Magic Trick - Gamo - 06-18-2018, 03:56 AM
RE: 1089 Magic Trick - Dieter - 06-18-2018 08:45 AM
RE: 1089 Magic Trick - Gamo - 06-18-2018, 09:46 AM
RE: 1089 Magic Trick - Dieter - 06-18-2018, 09:50 AM
RE: 1089 Magic Trick - grsbanks - 06-18-2018, 03:17 PM



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