Continuing my theme from last time of mental calculation and divisibility tricks that I didn’t learn in school, here’s a cool one:
- A number
is divisible by 11 if the alternating sum of its digits is divisible by 11.
For example, take the number 2574. Its alternating sum is
Let’s check, in R.
M = 2574
# check divisibility directly
if(M %% 11 == 0) {
result = paste(M, "is divisible by 11")
} else {
result = paste(M, "is not divisible by 11")
}
result
## [1] "2574 is divisible by 11"
This can potentially be handy, if you ever want to know if a large number is divisible by eleven, and your pencil and paper are closer than your calculator app. Hey, it could happen!
We can prove the above fact, but first let’s review some facts about modular arithmetic. If you are comfortable with modular arithmetic, skip to the Proof section.
Preliminaries: Modular Arithmetic #
By definition, when we say a % b = c
, we mean that a/b = kb + c
for
some value k
—in other words, that c
is the remainder of a/b
. (Note
that in R, %%
is the operator for modular arithmetic).
When we say (a ~ b) % c
(pronounced “a is congruent to b, modulo c”),
we mean that (a-b) % c = 0
. In other words, a
and b
are
equivalent in the field defined by modulo c
.
Modular Arithmetic with Negative Numbers #
The above is fairly intuitive with nonnegative numbers, but I always get confused when negative numbers appear. Let’s use the observation that
a/b = kb + (b-1) = (k+1) b + (-1)
In other words, (b-1)
is the remainder of a/b
with respect to some
multiple of b
(namely, kb
), but -1
is the remainder of a/b
with
respect to the next multiple of b
(namely, (k+1)*b
).
This means that ((n-1) ~ -1) % n
, and that all these statements are
equivalent:
(n-1) % n = (n-1)
-1 % n = (n-1)
(n-1) % n = -1 % n
Specifically for our application: (10 ~ -1) % 11
; and
10 % 11 = -1 % 11
.
More Handy Facts #
These are ways that we can “push through” the modulo operator into certain expressions:
(a + b) % n = (a % n + b) % n
(ab) % n = ((a % n) * b) % n
(a^b) % n = ((a % n)^b) % n
With these tools in hand, we are ready to attempt the proof.
The Proof #
A number
Let’s write
Then (dropping the summation bounds, for neatness);
The last step follows because (10 ~ -1) % 11
. Because we are working
in “modulo 11” world, the last expression is also equivalent to
The value -1 to an even power is 1; -1 to an odd power is -1. So the expression inside the brackets is the alternating sum of the digits of N.
Now we can see that if the alternating sum modulo 11 is zero, then
N % 11 = 0
. In other words, when the alternating sum is divisible by
11, so is
Testing it Out #
I confess, the proof gives me a headache. But we can test the procedure out on a random sample, to convince ourselves it works. First, let’s implement an alternating sum function.
# function that returns the absolute value of
# the alternating sum of a number's digits
alternating_sum = function(N) {
nstring = as.character(N)
# create a vector of digits
digits = as.numeric(unlist(strsplit(nstring, split="")))
nd = length(digits)
# alternating +/-1; -1 to even powers is 1, to odd powers is -1
signs = (-1)**(1:nd)
abs(sum(digits * signs))
}
If you look carefully, you’ll see the alternating sum I’m calculating above is actually the negative of the one I did “by hand” at the beginning of this post: people will naturally start the sum with a positive coefficient for the first (leftmost) digit, but my code starts the sum with a negative coefficient, because R indexes from 1. But that doesn’t affect divisibility by 11, and in any case, I’m returning the absolute value of the alternating sum, for consistency.
Now let’s take a random sample of integers, and test.
set.seed(20250306)
# test a random sample of integers
mult11 = 0 # we'll count how many multiples of 11 that we saw
# generate 1000 unique integers from 1:999999
samples = sample.int(999999, size=1000)
for(sample in samples) {
direct = (sample %% 11) == 0
bysum = (alternating_sum(sample) %% 11) == 0
if(direct) {mult11 = mult11 + 1}
stopifnot(direct == bysum)
}
print(paste("Test successful; found", as.integer(mult11), "multiples of 11."))
## [1] "Test successful; found 94 multiples of 11."
So the alternating sum trick seems to work! As with the tricks in my previous post, this may not be the most useful tool in your arsenal, given that we all have calculators and computers. But it’s an interesting arithmetical fact, all the same. Who knows? Maybe you can turn it into a bar trick.