Casting Out Nines
November 17, 2017
It doesn’t seem to be taught any more (neither of my daughters have ever heard of it), but casting out nines is a useful computational trick for verifying manual arithmetic calculations. Consider the sum below:
3074 6017 13814 1810 27 3611 ----- 28353
The sum of the digits of 3074 is 14, and the sum of the digits of 14 is 5, so the result of casting out nines from 3074 is 5. The sum of the digits of 6017 is 14, and the sum of the digits of 14 is 5, so the result of casting out nines from 6107 is 5. The number 13814 includes digits 1 and 8, which sum to 9, so we can ignore them and sum the digits 3, 1 and 4, so the result of casting out nines from 13814 is 8. Likewise, the 1 and 8 of 1810 sum to 9, so we ignore them, and the result of casting nines out of 1810 is 1. The digits of the 27 sum to 9, which we cast out, so the result of casting nines out of 27 is 0. Finally, we can cast out the 3 and 6 from 3611, so the result of casting nines out of 3611 is 2. Now the nines results of the six numbers are 5, 5, 8, 1, 0 and 2, and again we cast out the 8 and 1, leaving 12. The sum of the digits of 12 is 3, which is the final result of casting out nines from the addends.
The sum of the digits of 28353 is 21, and the sum of the digits of 21 is 3, so the result of casting nines out of 28353 is 3. Since the addends and the sum have the same result when casting out nines, it is plausible that the sum is correct. If the process of casting out nines yielded different results from the addends and the sum, we could be sure that the sum was incorrect.
Although that formulation sounds complicated, in practice this goes faster than you can think. Consider 28353. The first two digits sum to 10, so cast out a nine and count 1. Then add 3, giving a running nine-sum of 4, and add 5, giving a running nine-sum of 9, which can be cast out. The only remaining digit is 3, which is the result of casting out nines from the sum.
Casting out nines from the addends is even easier, because there are more ways to make nine. From the first two addends, cast out the 3 and 6, then cast out the 7, 4, and 7, leaving 1. Adding the third number gives 0; cast out the 1 and 8 immediately, leaving the 1 carried from the first two numbers, plus 3, 1 and 4, which sums to 9, which is equivalent to 0. Cast out 1 and 8 from the fourth number, leaving 1, cast out the fifth number entirely, and cast out 3 and 6 from the sixth number, leaving the carry of 1 plus the two 1s at the end of the sixth number, for a total of 3.
With a little bit of practice, this becomes ridiculously fast. Your friends will be amazed when you quickly tell them their sum is wrong; they will also hate you.
Mathematically, casting out nines is the same as addition modulo 9. MathWorld gives a good explanation. The process of casting out nines works for multiplication and exponentiation as well as addition.
Your task is to write a program to cast out nines; avoid the temptation to simply take the remainder on division by 9 and do the actual work of summing the digits and casting out nines. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
This code tries to stay true to the spirit of the “by hand”
procedure (as opposed to being efficient for a computer). For
instance, it uses a naive conversion to digits as one would do by
hand (by simply “writing digits separately”). The code uses only
addition, subtraction, and comparison of small numbers (in
particular, no division). One point of divergence is that it only
considers digits sequentially when casting out nines; i.e.,
non-consecutive digits that sum to 9 are not eliminated (e.g., the
3 and 6 from the first two addends of the example).
On second thought, my earlier code can be much simplified. (I decided
to use fold part-way through and didn’t finish the job.)
Goes quite nicely into a double fold:
Here’s a generalization to any radix (so we are “casting out n’s” for radix n+1). More Haskell:
Python 3 solution, not using recursion.
And the test:
Here’s a solution in Ruby.
Output: