The Party Math Trick

You're bored talking to someone at a party and thinking about what to discuss next. This is the party trick for that moment.

I learned this trick from my dad, and he'd perform it often for me as kid. I always enjoy performing it for others, reliving the sense of wonder and amazement it brings.

The trick goes like this:

Ask someone to pick a 3, 4, 5 or however many digit number randomly (and to keep it to themselves). 3 or 4 digits is usually a good balance.

Ask the person to then scramble the digits to make a new number with the same digits and to then subtract the smaller number from the bigger one. Again they should keep all this to themselves. They'll only need to remember the result of the subtraction.

Once they have the result, they should select a digit from the result that is not a 0 or a 9. Again, they should keep this digit secret. Finally they should tell you the remaining digits.

A quick example.

I pick the number 7,834, scrambling the digits I pick 4,837. Subtracting the bigger from the smaller (7,834 - 4,837) -- subtracting the smaller from the bigger is also fine, you just drop the negative sign -- yields 2,997. I pick a digit that's not 0 or 9 (so either 2 or 7) and say the remaining digits.

If I pick 2 I'll say 9, 9 and 7. Then the person performing the trick should be able to figure out that I picked a 2.

This relies on some fun math but it's also easy to program. Go ahead and try it out. Come back to learn the secret behind the trick.

I promise that the page is doing the trick and not just showing the number you click on. Feel free to inspect the client side JS and the data sent to the server if you don't believe me. You'll see it's the same as the information you get doing this trick manually.

How This works

This trick can be done fully without any computers with some easy mental math (or frantically with some back of the napkin work, or you know, your phone) - like at a party.

Like many math tricks, it relies on some properties of numbers, also known as number theory.

The key lies in the curious truth that the result of the subtraction of two numbers that have the same digits where one is a scrambled version of the other is always going to be a multiple of 9. When you take one of the digits out, I only need to find the first multiple of 9 that's bigger than the sum of the remaining digits, and take the difference between that value and the sum of the remaining digits.

It's also why you can't select a 0 or a 9. If you did, then the sum of the remaining digits would be a multiple of 9 already and so I'll know you tried to trick me by secretly picking a 0 or a 9.

That or someone screwed up their subtraction.

Casting Of Nines

The curious piece in all this is why the subtraction of these two numbers yields a result that is always a multiple of 9.

To start, a number, like 743 for example, can be written as (7 * 10^2 + 4 * 10^1 + 3 * 10^0) = 743.

More generally we can say any number N (743 in our concrete example above) can be written as the sum of its digits times 10 to a power. Abstracting the concrete digits - 7, 4, and 3 - for variables c, b, and a we get:

The Proof

N = (c * 100) + (b * 10) + (a * 1)

For numbers with more than 3 digits we would tack on a (d * 1000) and so on and so forth for more digits.

This is equivalent to

N = (c + 99c) + (b + 9b) + (a)

If we remove all the parentheses we see it becomes

N = c + 99c + b + 9b + a

With some regrouping this becomes

N = (c + b + a + 99c + 9b)

Which is equivalent to

N = c + b + a + (99c + 9b)

Which is equivalent to

N = (c + b + a) + [9 * (11c + 1b)]

Which is equivalent to

N - (c + b + a) = 9 * (11c + 1b)

Which finally is equivalent to

N - (c + b + a) = 9 * k - where k = (11c + 1b)

This shows that for any number N, made up of digits a, b, and c, if you subtract the sum of the digits from N, the result is a number divisible by 9 - it doesn't matter what the value for k will be, it is always multipled by a factor of 9. One further observation is k will always be determinable by the digits of N.

Try it, take any number, and subtract from it the sum of its digits and you'll be left with a number that is divisible by 9.

This is what gives us the quick test to determine if a number is divisible by 9, just see if the digits add up to a multiple of 9.

If they do, from the step where we had N = c + b + a + (99c + 9b), (c + b + a) must also be a multiple of 9. After all, our number N was made up of digits c, b, and a.

More broadly, if we sum the digits of a number N, we know the remainder when we divide by 9 will be the same as the remainder when we divide the N by 9.

When we scramble the digits to get a new number, we know that dividing this new number by 9 will still yield the same remainder as the original number divided by 9. We're using the same digits and so get the same sum!

Subtracting two numbers from each other, where both numbers have the same remainder when divided by 9, means any remainder when dividing the result of the subtraction by 9 will be 0.

More formally, if b is the result of the sum of our digits (c + b + a), we can say a ≡ b % 9 for both our original number and its scrambled version (a will be the same value for both numbers).

When we subtract our two numbers, it's the equivalent of subtracting a from a so we're left with 0 ≡ b % 9. b is the sum of the digits of the result from our subtraction. This shows that b must be a multiple of 9.

That is to say, taking two numbers where one is a scrambled version of the other and subtracting one from the other will always yield a number that is divisible by 9.

Knowing that the result of the subtraction must be divisible by 9, and given all but one digit of the result of that subtraction, the sum of the given digits plus the missing digit must also be a multiple of 9!

Simply find the first multiple of 9 bigger than the sum of the digits, subtract the sum from that multiple and you're left with the missing digit!

Congratulations, you're a mind reader!

Food for Thought

Of course, this only works using modulo 9 in our base 10 counting system. This property however holds true no matter what base of math you use so long as you adjust the modulo arithmetic to be 1 less than the base you use.

If we did this in base 7 (representing numbers as digits 0 through 6 multiplied by 7 raised to some power), we'd have to do this trick with mod 6 and multiples of 6.

It's an interesting property of number systems, and all the more interesting as it holds under the rule that the modulus has to be 1 less than the base.