I'm searching for an algorithm for Digit summing. Let me outline the basic principle:
Say you have a number: 18268
.
1 + 8 + 2 + 6 + 8 = 25
2 + 5 = 7
And 7 is our final number. It's basically adding each number of the whole number until we get down to a single (also known as a 'core') digit. It's often used by numerologists.
I'm searching for an algorithm (doesn't have to be language in-specific) for this. I have searched Google for the last hour with terms such as digit sum algorithm
and whatnot but got no suitable results.
this is from a really long time ago, but the best solution i have for this is:
I don't know how much better this is,but it will account for the divisible by 9 numbers easily. Just a cool algorithm.
Doesn't work with negative numbers, but I don't know how you would handle it anyhow. You can also change
f(x)
to be iterative:You can also take advantage of number theory, giving you this
f(x)
:Because 10-1=9, a little number theory will tell you that the final answer is just n mod 9. Here's code:
Example: 18268%9 is 7. (Also see: Casting out nines.)