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.


In reply to RE: Adding a Question by Dominus
in thread Math Question by Martin A

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.