February 20, 2010

Magician Trick or 1089 the Famous Number

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

Hi,

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 !