Martin A has asked for the wisdom of the Perl Monks concerning the following question:

Ok. I know this isn't a perl question, but I know that some of the monks here are very interessed and good att math so I thought this would be a good place fore this question.

Background:
First you take a 6 digit number and make a litteral reverse on that (345612 would be 216543). Then you take the original number and the reversed number and calculate the difference (345612 - 216543 = 129069). Then you remove a digit from the difference (remove the 6 near the end = 12909) and then calculate the sum of all the digits left (1+2+9+0+9 = 21) then you repete this stage on the result until you only get one digit left (2+1 = 3).And now comes the strange stuff. If you take 9 minus the digit you got (9 - 3) you will get the number you removed from the original difference.

NOTE: This works on any number that isn't symetric. And if you don't remove a 9.

So here comes the questions.
Why does this work?
Why do you always get the number you removed?
Is there any algorithem that makes a mathematical revers of the number?
(I only ask this becous I think it is a part of the solution)

I hope you can forgive me for posting non-perl questions.

// Martin

Replies are listed 'Best First'.
Re (tilly) 1: Math Question
by tilly (Archbishop) on Nov 12, 2000 at 00:10 UTC
    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.

Re: Math Question
by Fastolfe (Vicar) on Nov 11, 2000 at 23:38 UTC
    The difference between any two numbers (where one is the mirror of the other) will always be a multiple of 9. By a clever trick of math, this means adding up all of the digits should always get you 9. Since you dropped a digit early on, all you have to do is subtract 9 from your result and you've got the original number.

    I'm not sure what you mean by a "mathematical reverse", but the reverse function should neatly swap things around for you.

    print scalar reverse 12345; # 54321
Adding a Question
by Martin A (Beadle) on Nov 11, 2000 at 23:48 UTC
    Reading the reply i got a thought of another question that I forgot to ask.

    By reversing the numbers and calculating the difference, Why do you allways get a multiple of 9?

    // Martin
      If you have a number n, then the result of n % 9 (that's the remainder when you divide n by 9) is called the residue. The residue of 11 is 2. The residue of 97 is 7. The residue of 999 is 0. A multiple of 9 always has a residue of 0.

      It's a magical fact that if you add up the digits of a number, the sum always has the same residue as the original number. (I was going to explain it later, but tilly already did that so you can read it there if you like.)

      Every number must have the same residue as its reverse. Why? Because each number has the same residue as the sum of its own digits, and you get the same sum whether you add the digits forwards or backwards. Let's try an example. What's the residue of 125? The residue is the remainder after we divide by 9. 125/9 is 13 with a remainder of 8, so the residue of 125 is 8. What's the residue of 521? 521/9 is 57, also with a remainder of 8. And the digit sum of each number is also 8.

      Now imagine adding two numbers with residues of 1 and 2. Imagine each number is a heap of beans. If you split up the first heap into smaller piles of 9 beans each, you will have 1 bean left over. If you split up the second heap similarly, you have 2 beans left over. When you combine the two heaps, you get many little piles of 9 beans, but the beans that were left over before are sitll left over, and there are 3 of them in all. These 3 beans are the residue of the sum of the two original numbers..

      Whenever you add two numbers, the residue of the sum is the sum of the residues of the original numbers.

      The opposite is true for subtraction. If two numbers each have the same residue, say 5, then they each have 5 beans left over after you divide them into piles of 9. If you subtract them, the extra 5's cancel out and you are left with a multiple of 9, which has a residue of 0.

      We saw before that every number has the same residue as its reverse. That means that if you subtract a number from its reverse you get a number with a residue of 0, which means that the difference must be a multiple of 9.

      I hope that explains everything except the magic fact that I left out.