I’m certain that I once heard a name for this
(other than “silly”) but can’t remember it now:
Take a number, say 849672948.
Add up the digits:
8 + 4 + 9 + 6 + 7 + 2 + 9 + 4 + 8 = 57
Repeat until you arrive at a single digit:
5 + 7 = 12
1 + 2 = 3
Were one to write a program to do this (and I can’t for
the life of me imagine why anyone would), it would easily
lend itself to recursion.
So, what’s it called (again, other than “silly”)?
BoatyMcBoatFace(iValue as Integer) as Integer
SumDigits(iValue as Integer) as Integer
Where the first few instructions of SumDigits splits iValue into individual digits. A parent function makes shrinking to the smallest size optional, just call the desired function.
ReduceToSmallestSum(iValue as Integer) as Integer
Which would have the while loop that goes until the sum is one digit.
It’s not really recursion, but it’s how I’d build it – with options for myself.
According to stackoverflow, just use mod 9.
While I thought about it at first (that’s a known property of mod 9), there’s an edge case: when the sum equals 9, 9 mod 9 equals 0. So, for example, 45 returns 0.
But, well, since one knows only 0 can result to 0, any result being 0 with a non-null starting number means it’s actually 9.
What‘s wrong with ’silly’? Maybe a bit more declararive; silly_sum_digits?
Yes, the stackoverflow post mentions that edge case and how to handle it.
I was hoping it had some “official” name, perhaps in
memory of its inventor - “van Rentaal’s function”
But then who would want to be immortalised for something
Yes, it has an official name:
No, no, no! I only want to be immortalised for my work in quantum chromodynamics (if I ever do any).
You‘re right. I overlooked your post.
that’s an easy one, call it silly
So the function should be called
No because it isn’t exactly mod9 as stated previously
“AlmostMod9TheHardWay()” then .