数字求和的算法?
我正在寻找一种数字求和的算法。让我概述一下基本原则:
假设您有一个号码:18268
。
1 + 8 + 2 + 6 + 8 = 25
2 + 5 = 7
7 是我们的最终数字。它基本上是将整个数字中的每个数字相加,直到我们得到一个(也称为“核心”)数字。它经常被命理学家使用。
我正在为此寻找一种算法(不必是特定于语言的)。在过去的一个小时里,我用诸如数字和算法之类的术语在谷歌上进行了搜索,但没有得到合适的结果。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
因为 10-1=9,一点数论就会告诉你,最终答案就是 n mod 9。代码如下:
示例:18268%9 是 7。(另请参阅:投出 9。)
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.)
我会尝试这个:
I would try this:
不适用于负数,但我不知道你会如何处理它。您还可以将
f(x)
更改为迭代:您还可以利用数论,得到以下
f(x)
: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)
:这是很久以前的事了,但我对此的最佳解决方案是:
我不知道这有多好,但它可以轻松地解决可被 9 整除的问题。只是一个很酷的算法。
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.