February 20, 2010

Magician Trick or 1089 the Famous Number

Posted in Miscellaneous tagged , at 11:09 pm by nschoe


When I used to be a ‘magician’, there was a trick I loved particularly : ask a women her ring, put it on a combination padlock and pretend I lost the combination ; when her face eventually turned worried I offered her the possibility to find the combination again together, provided she could perform two little mathematical operations (namely substraction and addition). All she had to do was give me a 3-digit number (for instance 723), ‘mirror’ it,  i.e. create the 3-digit number which is composed of the same digits but the first one in third position and the third one in first position (here 723 becomes 327), and perform the substraction between these two numbers (here 723 – 327 is 396). Once again, she had to ‘mirror’ the number she obtained (here 396 turns into 693) and performed an addition this time (here 396 + 693) to get the combination of the padlock, 1089 and get her ring back.

What a luck that I had previously set the combination to be precisely 1089 ? Not really, it’s of course mathematically related, and here is one proof I produced (I’m definitely sure there is a better proof somewhere on the web, but this one is mine, and I remind readers that this blog doesn’t intend to be a reference or something, it’s just stuff I like and find interesting, but if you have comments to do, advice to give me, resources to make me read, … please do tell, I’ll be happy to read them !).

Maths Time

Some Conventions

First of all, let me define a term I’ll use throughout this post -and which will help me be clearer when dealing with numbers : given a 3-digit number, I’ll call ‘mirrored number’ the 3-digit number wihch is composed of the same three digits, but the first in the third position, and the third in the first position, the second doesn’t move, one can see it as a kind of ‘symmetry’. For instance, if our number is 834, then the mirrored number is 438.

Second, as a remainder -because it’s a mathematical convention- I’ll write absolute values with bars ( “|” and “|”), and not “abs()” as it’s sometimes used on other blogs, my opinion is “abs()” notation is for programming languages, and bars notation is for Maths.

The proof itself

Let A = a2.102 + a1.101 + a0.100 be any 3-digit number such that a2 is different from a0

Let A’ be A mirrored : A’ = a0.102 + a1.101 + a2.100

Let B = A – A’ :

a2.102 + a1.101 + a0.100

–     a0.102 + a1.101 + a2.100


B = (a2 – (a0 + 1)).102 + (10 + a1 – (a1 + 1)).101 + (10 + a0 – a2).100

B = (a2 – a01).102 + 9.101 + (10 + a0 – a2).100

Note that if it happens that we have something like 367 – 763 to perform, we’ll actually perform – (763 – 367). It is an important point for our proof, because that implies that a0 is lesser than a2, thus we need to add 10 to a0 (and of course 1 to a1 [from A’]).

Second thing, as we’re working with 3-digit numbers, the second digit of a number and his mirrored is the same, but as we added 1 to a1 [from A’], we have to add 10 to a1 [ from A] (and of course 1to a0 [from A’]).

Let B’ be B mirrored : B’ = (10 + a0 – a2).102 + 9.101 + (a2 – a0 – 1).100 .

Let’s perform B + B’ :

(a2 – a0 – 1).102 + 9.101 + (10 + a0 – a2).100

+     (10 + a0 – a2).102 + 9.101 + (a2 – a0 – 1).100


=    9.102 + 18.101 + 9.100

=     9.102 + (10 + 8).101+ 9.100

=      10.102 + 8.101 + 9.100

=      1000 + 80 + 9

=    1089

Here’s the result : 1089, always, amazing isn’t it ? Provided that the first and third digit of a 3-digit number are different, by performing these two simple operations, you get 1089 ; so as always, the magician I used to be was cheating ^^

Haskell Time

purify :: (Integral a) => [a] -> [a]
purify nbs = filter isSuitable nbs
where isSuitable n = let firstDigit = n `div` 100
thirdDigit = n `mod` 10
in firstDigit /= thirdDigit

compute1089 :: (Integral a) => a -> a
compute1089 n = let b = abs (n – (mirrored n))
in b + (mirrored b)

mirrored :: (Integral a) => a -> a
mirrored n = let firstDigit = n `div` 100
rest       = n `mod` 100
first’     = rest `div` 10
second’    = rest `mod` 10
rest’      = second’ * 10 + first’
in rest’ * 10 + firstDigit

main = do
let nbrs = purify (filter (`notElem` [111,222,333,444,555,666,777,888]) [1..998])
let computed = map compute1089 nbrs
let isProoved = not (any ( (/=) 1089) computed)
return isProoved

Well, simple code. I agree with that fact that the proof isn’t proper, but it’s valid : one way of prooving that a predicate works is to test all numbers whose predicate is applied to. Here there are 990 numbers, which is perfectly doable for a computer.

Some quick explanations of this code.

The ‘purify’ function eliminates the numbers whose first and third (last) digits are the same. The first digit is got by doing the euclidean division of our number and 100 while the last one is obtained by performing modulo 10.

The ‘compute1089’ just applies the algorithm to any 3-digit number you pass as a parameter.

The ‘mirrored’ function is the most interesting one I think : it applies the same algorithm we’ve just applied to get the first and third digit of a number, it’s advised to take the example step by step with a number on a sheet of paper.

The main function isn’t that much interesting in itself as it only applies our functions and returns True, to tell us that we prooved the predicate ^^

I hope I was clear enough, it’s not that easy to explain, because of the ax digits, and the definitions of A and A’ . If there is any question, or spot you want me to clarify, please ask !



  1. Since it is not appropriate to take weight loss pills that work, if you are
    buying high-quality supplements. There aren’t any pills that are available over-the-counter or on the brain create the feeling of fullness, and by extension in the fat burning craze they often associate it to the blood stream. Can you share Prescribed Best weight loss pills that work.

  2. nschoe said,

    Hi Luke,
    Thanks for your answer, “It was nice to see this interesting property formalized” : it’s good to hear (well read actually) ^^.

    Wow, concerning you Haskell code … I have to say : “thanks for the lesson”. It’s so concise, thus more readable than my own boring code.
    I’m rather new to Haskell, meaning I’m looking for ways to sharpen my skills, you’ve just give me some, thanks.

  3. Luke said,

    Nice writeup. It was nice to see this interesting property formalized.

    This haskell code is more complex than it needs to be, however.

    mirror x = 100 * (x `mod` 10) + 10 * ((x `div` 10) `mod` 10) + (x `div` 100)
    valid x = x /= mirror x
    compute1089 x = let i = abs (x – mirror x) in i + mirror i
    isProved = all (\x -> compute1089 x == 1089) . filter valid $ [1..999]

    Mainly because, of course, the repdigits 111, 222, … already don’t pass the firstDigit /= thirdDigit test, so you don’t need to filter them out separately. Other than that, you used a lot more intermediate naming than I did, which is a stylistic choice 🙂

  4. nschoe said,

    Hi luckytoilet,

    Thanks for posting here.
    First I’ll answer your second paragraph : when we have a number, when me ‘mirror’ it as I called (that’s tricky because I don’t think there is a mathematical term for this, and I can’t use “reverse” nor “opposite”, that’s why I used ‘mirror’), we get a number, with the reverted order of the digits. For instance : 283 (which is 2-8-3) gives 382 when mirrored (which is 3-8-2).
    Because of this ‘mirror’ we apply to the number, it’s easy to see why this doesn’t work with numbers whose first and third digit are the same : take 848 for instance, when ‘mirrored’, it’s still 848. With our notations, it would give : A =848 and A’ = 848.
    When you perform A – A’ it gives 848 – 848 which is 0 : we’re stuck, that’s why I set a condition on our numbers : the first and third digit must be different.

    Concerning 100, it does work : 100 ‘mirrored’ is 001, then 100 – 001 = 099.
    099 ‘mirrored’ is 990, and 990 + 099 is 1089 ^^

    Concerning your first paragraph, can you send me your draft or somewhere you have written to get B + B’ is 0 ?
    Some clues to confirm you’re on the good way : in the expression of B, do you have 9.10^1 in the second digit ? If no, then you’ve made a mistake : every B number we get with every A number should have 9 as the second digit.
    When you perform B + B’ are you sure 1/ that your B expression has all the “10” and “1” added from the step above ? 2/ That you succeed in ‘mirroring’ the B number to get B’ ?

    I should be able to help you a bit more if I saw your draft/paper/code .

  5. luckytoilet said,

    This is an interesting post, but I have a few points.

    When I try to prove this myself, I seem to get B+B’=0. This is using probably the two more obvious approaches. You may have got this conclusion at least once while trying to prove it. Do you know why this is not correct?

    You mentioned that the first and third digits must not be the same. Perhaps this has something to do with the above point? Also this takes away at least 100 of the 1000 possibilities (assuming 1′ is 100) and possibly more; Why would this not work when the first and third digits are the same?

    Also the Haskell code seems to contain lexical errors.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: