数字求和的算法?

发布于 2024-08-29 22:59:04 字数 278 浏览 7 评论 0原文

我正在寻找一种数字求和的算法。让我概述一下基本原则:

假设您有一个号码: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

土豪我们做朋友吧 2024-09-05 22:59:04

因为 10-1=9,一点数论就会告诉你,最终答案就是 n mod 9。代码如下:

ans = n%9;
if(ans==0 && n>0) ans=9; 
return ans;

示例: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:

ans = n%9;
if(ans==0 && n>0) ans=9; 
return ans;

Example: 18268%9 is 7. (Also see: Casting out nines.)

失去的东西太少 2024-09-05 22:59:04

我会尝试这个:

int number = 18268;
int core = number;
int total = 0;

while(core > 10)
{
   total = 0;
   number = core;
   while(number > 0)
   {
      total += number % 10;
      number /= 10;
   }

   core = total;
}

I would try this:

int number = 18268;
int core = number;
int total = 0;

while(core > 10)
{
   total = 0;
   number = core;
   while(number > 0)
   {
      total += number % 10;
      number /= 10;
   }

   core = total;
}
笑看君怀她人 2024-09-05 22:59:04

不适用于负数,但我不知道你会如何处理它。您还可以将 f(x) 更改为迭代:

sum( x ) =
    while ( ( x = f( x ) ) >= 10 );
    return x;

f( x ) = 
    if ( x >= 10 ) return f( x / 10 ) + x % 10
    return x

您还可以利用数论,得到以下 f(x)

f( x ) =
    if ( x == 0 ) return 0
    return x % 9

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:

sum( x ) =
    while ( ( x = f( x ) ) >= 10 );
    return x;

f( x ) = 
    if ( x >= 10 ) return f( x / 10 ) + x % 10
    return x

You can also take advantage of number theory, giving you this f(x):

f( x ) =
    if ( x == 0 ) return 0
    return x % 9
温折酒 2024-09-05 22:59:04
  1. 将整数除以 10。
  2. 将数字添加到数组中。
  3. 添加整个数组。
  1. Mod the whole number by 10.
  2. Add the number to an array.
  3. Add the whole array.
煮茶煮酒煮时光 2024-09-05 22:59:04
int number = 18268;
int total = 0;

while(number > 0)
{
   total += number % 10;
   total = total%10;
   number /= 10;
}
int number = 18268;
int total = 0;

while(number > 0)
{
   total += number % 10;
   total = total%10;
   number /= 10;
}
葬心 2024-09-05 22:59:04

这是很久以前的事了,但我对此的最佳解决方案是:

int digitSum(int num){
    if (num < 10) return num;
    else return (n-1)%9+1;
}

我不知道这有多好,但它可以轻松地解决可被 9 整除的问题。只是一个很酷的算法。

this is from a really long time ago, but the best solution i have for this is:

int digitSum(int num){
    if (num < 10) return num;
    else return (n-1)%9+1;
}

I don't know how much better this is,but it will account for the divisible by 9 numbers easily. Just a cool algorithm.

短叹 2024-09-05 22:59:04
    private static int sum(long number) {
    int sum = 0;
    if (number == 0) {
        return 0;
    }
    do {
        int last = (int) (number % 10);
        sum = (sum + last) % 9;
    } while ((number /= 10) > 0);

    return sum == 0 ? 9 : sum;
}
    private static int sum(long number) {
    int sum = 0;
    if (number == 0) {
        return 0;
    }
    do {
        int last = (int) (number % 10);
        sum = (sum + last) % 9;
    } while ((number /= 10) > 0);

    return sum == 0 ? 9 : sum;
}
还不是爱你 2024-09-05 22:59:04
public int DigitSum(long n)
  {
     return (int)(1 + (n - 1) % 9);
  }
public int DigitSum(long n)
  {
     return (int)(1 + (n - 1) % 9);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文