Post Reply 
1089 Magic Trick
06-18-2018, 03:56 AM (This post was last modified: 06-18-2018 08:36 AM by Gamo.)
Post: #1
1089 Magic Trick
I'm trying to program the 1089 trick on HP-11C
This should be a good program to practice and learn RPN programing.

What it do is users input any three digits number in which numbers cannot contain repeated number like (222, 122, 444, 477, 339, etc.) These repeated numbers are not allowed.

Program take three digits numbers,
1. Reverse it
2. Subtracting the reversed number by the original number.
3. Result from steps two make a second reverse
4. Adding the result from its original number.
5. Out put should have 1089

Example:
Start with 532.
The Reverse is 235, and the difference is 532-235=297.
Reversing the difference gives 792, and 297+792=1089

The problem is there are 648 legitimate 3-digits numbers with no repeated digits and 136 of them produce 198 rather than 1089

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.

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


Program: 1089 Magic Trick
Code:

LBL A
STO 1
STO 2
GSB 1
RCL 1
RCL 2
X≤Y     // Test for when top 3 digits is less than the bottom 3 digits then do the swap  
GTO 2
RCL 2
PSE    //  Show the reversed of the original number
RCL 1
-
PSE    // Show the result of the reversed number subtracted by original number
STO 2
STO 3
GSB 1
RCL 2
PSE    // Show the reversed number of the result subtraction
RCL 3
+     // Show the final trick of 1089
RTN
-------------------------------------------------------------------------
LBL 2    // The pauses action below is the same as above
RCL 1
RCL 2
PSE
-
PSE
STO 2
STO 3
GSB 1
RCL 2
PSE
RCL 3
+
RTN
---------------------------------------------------------------------------
LBL 1    // Subroutine for digits reversal 
STO-2
LBL 0
INT
10
STOx2
÷
FRAC
STO+2
LSTx
X≠0
GTO 0
RTN

Gamo
Find all posts by this user
Quote this message in a reply
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
06-18-2018, 09:46 AM
Post: #3
RE: 1089 Magic Trick
Dieter
Very nice routine this is even better than expected since it can even do the calculation when two of the three digits is the same number.

Example:

223 A --> 1,089
337 A --> 1,089

Oh.. if same number is on both side is not working Smile

737 A ---> 0
525 A ---> 0

Thank You

Gamo
Find all posts by this user
Quote this message in a reply
06-18-2018, 09:50 AM (This post was last modified: 06-18-2018 09:51 AM by Dieter.)
Post: #4
RE: 1089 Magic Trick
(06-18-2018 09:46 AM)Gamo Wrote:  Oh.. if same number is on both side is not working Smile

737 A ---> 0
525 A ---> 0

Sure. In this case the reversed number is the same as the original one (737 => 737) so that the difference is zero, and so is the final result (000+000=0). That's why the rules of this trick exclude such numbers. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
06-18-2018, 03:17 PM
Post: #5
RE: 1089 Magic Trick
For a quick mathematical explanation of this... A 3-digit number "abc" is basically:

\(100a+10b+c\) where each of the terms a, b and c is a natural number from 0 to 9 inclusive.

Reversing the digits gets you a new number, which is:

\(100c+10b+a\)

If you want the difference between the two, there are two cases. Either \(a>c\) or \(a<c\). As has already been pointed out, this trick doesn't work if \(a=c\) because the original number and its reverse are the same, thus cancelling out the whole thing.

First scenario: \(a>c\)

\(100a+10b+c-(100c+10b+a)=99a-99c=99(a-c)=(100-1)(a-c)=100(a-c)+(c-a)\)
\(100(a-c)+(c-a)=100(a-c-1)+100+(c-a)=100(a-c-1)+90+(10+c-a)\)

We know that \(a>c\), so the expression \((10+c-a)\) is necessarily going to be less than 10 and greater than or equal to 1, i.e. it's going to be a single digit. Similarly, \(0 \leqslant (a-c-1) \leqslant 8\).

We therefore have the 3 digits of the difference between the original number and its reverse. Let's get the reverse of this difference by reversing the units and hundreds digits:

\(100(10+c-a)+90+(a-c-1)\)

If we now add the difference to its reverse, we get:

\(100(a-c-1+10+c-a)+180+(10+c-a+a-c-1)=100\times9+180+9=1089\)

Second scenario: \(a<c\)

Just start with the reverse of the original number and you'll find yourself in the first scenario again.
Find all posts by this user
Quote this message in a reply
Post Reply 




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