logo for matrixlab-examples.com
leftimage for matrixlab-examples.com

Math magic trick - The 1089 problem

Have fun with Matlab




Here’s a funny math magic trick that you can test with Matlab. We’ll solve this problem with our numerical software as an excuse to explore built-in functions num2str (number to string), str2number (string to number), and fliplr (flip vector or matrix in left/right direction).


This is what we’re going to do:
  1. Pick a three-digit number. The three numbers used must be different, for example 531.
  2. Reverse that number: 531 becomes 135. 
  3. Get the absolute difference of the numbers: 531 – 135 = 396. If the result is a two-digit number, write a 0 at the beginning to make it a three-digit one (for example, if you get 99, change it to 099).
  4. Take the answer and reverse that number: 396 becomes 693.
  5. Add that number to the answer of the subtraction. 693 + 396 = 1089.


The answer will always be 1089! (well... almost!)

  reading minds with math

Now, here we go with our approach in Matlab. First, you need to know the difference between numbers and strings. Sometimes they look similar, but there’s an important difference and you must act accordingly... You can do arithmetical operations with numbers, but you cannot do them with strings.

 
Function num2str changes numbers into strings, so num2str(531) results in string ‘531’. Note that if you type size(531) on the command window, Matlab will answer 1 1, meaning that you have a ‘matrix’ with one row and one column only (that happens to be a scalar!). If you type size(‘531’) - using single quotes for the parameter - on the command window, Matlab will answer 1 3, meaning that you have a matrix with one row and three columns. That’s not a scalar, right? It’s a string with three elements or characters, even though you see it as a number. 

Function str2num changes strings into numbers. A str2num(‘123’) will produce the scalar number 123.  

Function fliplr flips strings (not numbers) from left to right, or invert them so to speak. fliplr(‘351’) will produce the string ‘153’.

 
This is the code that could perform the algorithm above:

clear, clc 

% this is your initial number
a = 123 

% change it to string
as = num2str(a)

% flip the string
asi = fliplr(as)

% change back the inverted string to number
b = str2num(asi) 

% get the difference
c = abs(a-b)

% change to string
cs = num2str(c)

% flip the string
csi = fliplr(cs)

% change back the inverted string to number
d = str2num(csi) 

% add and get 1089
e = c + d

 

Note that there are two instructions that were not considered in the code. First, the digits should be different, and second, if the resulting difference is two-digit only, you should write a 0 at the beginning. Those issues were not considered, but now you could design an iteration to see which 3-digit numbers didn’t produce 1089 after the code given.

Let’s rework our code:

correct = 0;
wrong = 0;

% These are your starting numbers
for a = 100 : 999
   
% flip them
    b = str2num(fliplr(num2str(a)));  

    % get the difference
    c = abs(a-b);   

    % flip the difference
    d = str2num(fliplr(num2str(c)));   

    % add and get 1089
    if (c + d) == 1089
        correct = correct + 1;
   
else
    %if not, display count, starting number and result
        wrong = wrong + 1;
        disp ([wrong a c+d])
   
end
end

correct
wrong


And you’ll get the list of numbers that didn’t have different digits or that produced a two-digit difference... Besides, you counted the "correct" numbers and displayed and counted the "wrong" ones.

     1   100   198
     2   101     0
     3   102   198
     4   110   198
     5   111     0
     ...
     ...
   258   989     0
   259   998   198
   260   999     0
 

correct = 640
wrong =   260




footer for math magic trick page