The casting out 9's algorithm would have been more consistent (and
possibly faster) if it had never used division at all but had summed the
digits of the initial sum and repeated the process until it reached a
single digit and then compared that digit to 9 (ignoring the trivial
case of the original number being 0). It would be a good example for
coding a recursive routine rather than that overused factorial example:)
Gary Weinhold
Data Kinetics, Ltd.
On 8/18/2010 9:33 AM, Bill Fairchild wrote:
It sounds as if this code was done once to prove a theorem involving integral
numbers using mathematical induction. Then perhaps the author copied it into
some other more permanent program once it was debugged, because, Hey! It
correctly tests if a number is divisible by 9.
Or another wild hypothesis is that the author once had a number so large that
it would not fit into a register and he had to test its being an integral
multiple of 9 by some other means.
Or he had way too much time on his hands.
In any case, it was definitely not useful, according to William Blair's
computer science professor's definition.
I give you my permission to replace it with code that divides the original
number by 9 rather than dividing the convolutedly derived number by 9.
Bill Fairchild
Rocket Software
And to check
whether or not a number was divisable by 9 it would add all digits, then
check whether or not dividing that sum by 9 would yield a zero remainder.