in reply to Math Question

This site is Perl specific. So while some here do know a lot of math, we should keep on track.

So while I will answer it, I will try to make the answer touch on Perl where I can. (I probably won't be very successful though.)

First of all how do you reverse the digits of a number? Well in Perl the easiest way is:

$reversed = reverse $number;
Be careful using this though, reverse does very different things in scalar and list context. (In this case you are assigning to a scalar so you are in scalar context. If you are in doubt you can use the scalar operator to guarantee scalar context. List context is the default.)

Now the entire operation you give is a big shell game. There is exactly one concept. The concept is called the modulus of a number. If you look in perlop you will find that the % operator calculates the remainder that you get when one number is divided by another and they call it the modulus.

Sounds trivial. But a key insight that dates back to Gauss is that for most divisibility questions it is quite sufficient to replace all of your numbers with what they are "congruent to modulo" what you will divide by in the end. (Two are congruent if they have the same remainder.) More than that, thinking this way usually drastically simplifies the problem. For instance type the following into a shell (play around with the quotes if you are on DOS):

perl -pe '$_ = $_%9 . "\n"'
Then try typing a bunch of numbers and you will get the remainders mod 9.

Try that with the numbers in your calculation. You will find that 345612 has 3 as a remainder. Reverse it to get 216543 and the remainder is 3 again. Subtract one from the other and you get 129069 which has a remainder of 0. Hmm, it is divisible by 9! Then remove your digit and you get 12909 which has a remainder of 3. (3+6 is 9, and is what you removed. Hmmm.) Then as you reduce it you keep on getting remainders of 3, until you find that 3 has a remainder of 3. Then 9-3 gives you back 6, amazing.

For those who haven't got the point, here it is. When you think about the world modulo 9 you see 1, 10, 100, 1000, and so on all as 1. (Because 0, 9, 99, 999 and so on are all divisible by 9.) Therefore modulo 9 a number is the same as the sum of its digits. So you took a number. Then you reverse its digits (leaves the remainder alone). Now subtract one from the other. (Now you have something that is divisible by 9.) Now subtract a digit and you change the remainder. Now summing repetitively calculates the remainder mod 9 in a non-obvious way. So in your case you removed a 6 and got a remainder of 3 (which is -6 in mod 9 land). Now take 9 and subtract 3, and mod 9 you are now working out -(-6)=6 mod 9.

Oh, and why the rules on 9? Well mod 9, 9 is 0 so you would get 0. While mod 9 that is right, most people don't see the world mod 9 so they won't be so impressed. (In fact if they did see the world mod 9 they wouldn't be impressed in the first place.)

That is the thing about most of these tricks. They look very impressive to people who are missing a few general principles, but if you know the principles they are pretty straightforward to unravel.