For anyone curious about why cLive's division-by-seven rule works, it can be proven mathematically in reverse. (This is not so much Perl as it is math -- or maths, for you European types -- so bear with me.) You can follow the same generic steps to determine a similar division rule for any prime N. (For those of you curious why I am restricting it to primes, it is because they have no factors, which will come into play below.) First, realize the basic form is 10a (Nx + by) + cy. This is the expression that we want to determine coefficients for to ensure it results in a multiple of N.

When we multiply through by 10 to whatever power it will raised to -- 10aNx + 10aby + cy -- we can remove the N term because we know it will be divisible by N; all that is left is to choose coefficients (a, b, c) that result in a multiple of N.

We're left, then, with: 10aby + cy = y(10ab + c). Assuming y is not N (which would make this trivial), we are left to look at 10ab + c. There are some very boring cases to ponder:

b = 0
This means that all we are adding to our multiple of N is cy, which means that c must also be a multiple of N. This is boring.
c = 0
This means that all we're adding to our multiple of N is y times some power of 10 times b, which means N is a factor of 10 (so it's 2 or 5), or that y or b is a multiple of N. This is also boring.
a = 0
This makes our initial expression Nx + by + cy, which means (b + c) must be a multiple of N. This is boring because it's basically saying "to get another multiple of N, just add N times some other number".
So in order to not be bored, we don't want any of our coefficients to be zero. And we'd probably prefer a to be greater than zero. We're multiplying y by the expression 10ab + c, which must be divisible by N. Looking at our division-by-seven model above, we can see that we multiplied y by 21, which uses {1,2,1} as the coefficients. (From this point on, {a,b,c} will indicate a set of three coefficients, in that order.) We could have also used {1,1,4} resulting in multiplying y by 14.

Therefore, you choose three coefficients that produce (using the expression above) a multiple of the number. Which ones you choose are up to you. The one for seven is easy to remember because it's easy to reverse. It only involves the last digit of the number: 182, chop off the 2 (really subtract 2, divide by 10), subtract 2*2 from the remainder, leaving 14 (divisible by 7!). If we used the {1,1,4} technique on the number 182, we'd have to say "what multiple of 4 ends in 2? 12. ok, so the magic number is 3 (12 divided by 4). subtract 12, leaving 170. Now divide by 10, leaving 17. now subtract the magic number, 3, leaving 14." (This also would have worked if we'd chosen 8 as the magic number, since 4*8 = 32 which ends in 2. 182 - 32 = 150, 150/10 = 15, 15 - 8 = 7.) So it seems the easiest coefficient sets have c = 1.

So. Here's an easy rule for 11 (which uses {1,1,1}): "chop off the last digit and subtract it from the remainder". Here's one for 13, using {1,9,1}: "chop off the last digit and subtract it times 9 from the remainder". And so on.

Thus endeth the lesson.

_____________________________________________________
Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re: (OT) Division-by-seven, division-by-n
by fergal (Chaplain) on Nov 02, 2004 at 17:37 UTC

    You've shown that if you start with a multiple of 7, you can construct 10 other numbers (1 for each y in 0..9) that are also divisible by 7 and which obey cLive's rule. It's not the same as showing that every number obeys cLive's rule.

    Using the "|" to mean divides (so 7| x + y means "7 divides x + y") and <=> to mean "if and only if".

    (1)7 | x - 2y <=> 7 | 10x - 20y (2)7 | 10x - 20y <=> 7 | 10x + y so 7 | x - 2y <=> 7 | 10x + y

    (1) is true because 10 has no factor in common with 7 so multiplying by 10 does not effect divisbility

    (2) is true because 21y is divisible by 7 so adding 21y will have no effect on divisibility

    This holds for any x and y and every number can be written as 10x+y for some suitable x and y.

    The key to finding a rule for divisibility mod N is finding the inverse of 10 mod N. That is, finding I such that

    (10 * I) % N == 1
    So for example if N is seven the inverse of 10 is 5: 5 * 10 = 50 = 7*7 + 1. So, thinking only in terms of remainder on division by 7,
    10x + y = 5 * (10x + y) = 50x + 5y = x + 5y = x - 2y
    and mod 21, 10 * 19 = 190 = (9*21) +1. So thinking mod 21 we have
    10x + y = 19*(10x+y) = 190x +19y = x + 19x = x - 2y
    so 10x + y is divisible by 21 whenever x - 2y is too.

    This methods works for non-primes, what's important is that the number doesn't share any factors with 10. ie. it's not divisible by 2 or 5.

    Update: fixed a typo I had (N * I) % N = 1 in the definition of inverse. Also fixed a missing / that messed the formatting.

Re: (OT) Division-by-seven, division-by-n
by cLive ;-) (Prior) on Nov 02, 2004 at 22:30 UTC
    What a lot of work japhy! Here's a short version.
    Let N be an integer that is divisible by 7 Let "xy" be a number . Eg, for 203, x=20, y=3 for 1984, x=198 y=4 If this number is divisible by seven, then: 10x + y = N => 7x + 3x + y = N => 3x + y = N => 9x + 3y = N So 10x + y = N 9x + 3y = N => x - 2y = N

    ie, if "xy" is divisible by 7, then x-2y is also divisible by 7.

    And, if you can be bothered, it can easily be shown that the reverse is true also, so we have:

    "xy" % 7 = 0 <=> (x - 2y) % 7 = 0

    cLive ;-)