算法的数字相加? [英] Algorithm for digit summing?
问题描述
我在寻找一种算法,数字求和。让我概括的基本原则:
I'm searching for an algorithm for Digit summing. Let me outline the basic principle:
假设你有一个数字: 18268
1 + 8 + 2 + 6 + 8 = 25
2 + 5 = 7
和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.
任何帮助将是巨大的,谢谢。
Any help would be great, thanks.
推荐答案
由于10-1 = 9,有点数论会告诉你,最后的答案是N模9.这是code:
Because 10-1=9, a little number theory will tell you that the final answer is just n mod 9. Here's code:
ans = n%9;
if(ans==0 && n>0) ans=9;
return ans;
例如:18268%9 7。(另请参阅:铸造了九)
这篇关于算法的数字相加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!