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.)
- Start with w, which can be written as a multiple of seven: 7x
- Add twice any number y to it: 7x + 2y
- Multiply by 10 (this step and the next are...): 10 (7x + 2y)
- Add y (... the reverse of chopping a digit off): 10 (7x + 2y) + y
- Multiply the 10 through, and you get: 70x + 20y + y = 70x + 21y
- The coefficients (70 and 21) are divisible by 7, therefore the sum of the two terms will be divisible by 7 as well
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