📜  给定大整数的数字根(重复的数字和)

📅  最后修改于: 2021-04-29 04:38:54             🧑  作者: Mango

正整数的数字根是通过对整数的数字求和而得出的。如果结果值是一个数字,那么该数字就是数字根。如果结果值包含两个或多个数字,则将这些数字相加并重复该过程。只要需要获得一位数字就可以继续进行。

给定一个数字,任务是找到其数字根。输入数字可能很大,即使我们使用long long int,也可能无法存储。

在ACM-ICPC中询问

例子 :

Input : num = "1234"
Output : 1
Explanation : The sum of 1+2+3+4 = 10, 
              digSum(x) == 10
              Hence ans will be 1+0 = 1

Input : num = "5674"
Output : 4 

我们在下面的文章中讨论了一种适合很长一段时间的数字的解决方案。
查找数字的位数和,直到总和变为一位数

在这篇文章中,大量讨论了类似的方法。

方法1

查找65785412的数字根
脚步:

  1. 找出一个数字的所有数字
  2. 将所有数字一一相加
  3. 如果最终总和为两位数,请再次加法使其成为一位数
  4. 以一位数字获得的结果是数字的数字根

例子:
输入: 65785412
查找数字根:(6 + 5 + 7 + 8 + 5 + 4 + 1 + 2)= 38 => 11 =>(1 + 1)= 2
输出: 6

方法二

该想法基于以下事实:对于非零数字num,如果数字可被9整除,则数字根为9,否则数字根为num%9。(请参阅http://www.sjsu.edu/faculty/ watkins / Digitsum0.htm了解详情)

查找65785412的数字根
脚步:

  1. 数字总和= 6 + 5 + 7 + 8 + 5 + 4 + 1 + 2 = 38
  2. 由于38不是9的倍数,所以数字根为38%9 = 2。
C++
// C++ program to find  digital root of a number
#include
using namespace std;
  
// Returns digital root of num
int digitalRoot(string num)
{
    // If num is 0.
    if (num.compare("0") == 0)
        return 0;
  
    // Count sum of digits under mod 9
    int ans = 0;
    for (int i=0; i


Java
// Java code for digital root
import java.util.*;
   
public class GfG {
       
    static int digroot(int n)
    {
        int root = 0;
   
        // Loop to do sum while
        // sum is not less than
        // or equal to 9
        while (n > 0 || root > 9) 
        {
             if (n == 0) {
                n = root;
                root = 0;
            }
              
            root += n % 10;
            n /= 10;
        }
        return root;
    }
       
    // Driver code
    public static void main(String argc[])
    {
        int n = 65785412;
        System.out.println(digroot(n));
    }
}
  
// This code is contributed by Gitanjali.
// This code will run for 0000 testcase also.


Python3
# Python3 program to find digital root
# of a number
import math 
  
# Returns digital root of num
def digitalRoot(num):
      
    # If num is 0.
    if (num == "0"):
        return 0
  
    # Count sum of digits under mod 9
    ans = 0
    for i in range (0, len(num)):
        ans = (ans + int(num[i])) % 9
          
  
    # If digit sum is multiple of 9, answer
    # 9, else remainder with 9.
    if(ans == 0):
        return 9
    else: 
        return ans % 9
  
# Driver method
num = "65785412"
  
# Calling digitalRoot function
print (digitalRoot(num))
  
# This code is contributed by Gitanjali.


C#
// C# code for digital root
using System;
  
class GfG {
      
    static int digroot(int n)
    {
        int root = 0;
  
        // Loop to do sum while
        // sum is not less than
        // or equal to 9
        while (n > 0 || root > 9) 
        {
            if (n == 0) {
                n = root;
                root = 0;
            }
              
            root += n % 10;
            n /= 10;
        }
        return root;
    }
      
    // Driver code
    public static void Main()
    {
        int n = 65785412;
        Console.Write(digroot(n));
    }
}
  
// This code is contributed by Smitha
// This code will run for 0000 testcase also.


PHP
 0 || $root > 9) 
    {
        if ($n == 0) 
        {
            $n = $root;
            $root = 0;
        }
          
        $root += $n % 10;
        $n /= 10;
    }
    return $root;
}
  
// Driver code
$num = 65785412;
  
// Calling digitalRoot function
echo digroot($num);
  
// This code is contributed by Sam007
?>


输出 :

2