📜  使用递归的给定大整数的数字根

📅  最后修改于: 2021-05-04 09:38:08             🧑  作者: Mango

正整数的数字根是通过对整数的数字求和而得出的。如果结果值是一个数字,那么该数字就是数字根。如果结果值包含两个或多个数字,则将这些数字相加并重复该过程。只要需要获得一位数字就可以继续进行。
给定大量N ,任务是找到其数字根。输入数字可能很大,即使我们使用long long int,也可能无法存储。
例子:

方法:

  1. 找出一个数字的所有数字。
  2. 将所有数字一一相加。
  3. 如果最终的总和包含一位以上的数字,请再次调用递归函数以使其为一位数字。
  4. 用一位数字获得的结果是数字的数字根。

下面是上述方法的实现:

C++
// C++ programm to print the digital
// root of a given very large number
 
#include 
using namespace std;
 
// Function to convert given
// sum into string
string convertToString(int sum)
{
    string str = "";
    // Loop to extract digit one by one
    // from the given sum and concatinate
    // into the string
    while (sum) {
        // Type casting for concatination
        str = str + (char)((sum % 10) + '0');
        sum = sum / 10;
    }
    // Return converted string
    return str;
}
 
// Function to get individual digit
// sum from string
string GetIndividulaDigitSum(string str,
                             int len)
{
    int sum = 0;
    // Loop to get individual digit sum
    for (int i = 0; i < len; i++) {
        sum = sum + str[i] - '0';
    }
    // Function call to convert
    // sum into string
    return convertToString(sum);
}
 
// Function to calculate the digital
// root of a very large number
int GetDigitalRoot(string str)
{
    // Base condition
    if (str.length() == 1) {
        return str[0] - '0';
    }
    // Function call to get
    // individual digit sum
    str = GetIndividulaDigitSum(
        str,
        str.length());
    // Recursive function to get digital
    // root of a very large number
    return GetDigitalRoot(str);
}
int main()
{
    string str
        = "675987890789756545689070986776987";
 
    // Function to print final digit
    cout << GetDigitalRoot(str);
}


Java
// Java programm to print the digital
// root of a given very large number
class GFG{
  
// Function to convert given
// sum into String
static String convertToString(int sum)
{
    String str = "";
 
    // Loop to extract digit one by one
    // from the given sum and concatinate
    // into the String
    while (sum > 0) {
 
        // Type casting for concatination
        str = str + (char)((sum % 10) + '0');
        sum = sum / 10;
    }
 
    // Return converted String
    return str;
}
  
// Function to get individual digit
// sum from String
static String GetIndividulaDigitSum(String str,
                             int len)
{
    int sum = 0;
 
    // Loop to get individual digit sum
    for (int i = 0; i < len; i++) {
        sum = sum + str.charAt(i) - '0';
    }
 
    // Function call to convert
    // sum into String
    return convertToString(sum);
}
  
// Function to calculate the digital
// root of a very large number
static int GetDigitalRoot(String str)
{
    // Base condition
    if (str.length() == 1) {
        return str.charAt(0) - '0';
    }
 
    // Function call to get
    // individual digit sum
    str = GetIndividulaDigitSum(
        str,
        str.length());
 
    // Recursive function to get digital
    // root of a very large number
    return GetDigitalRoot(str);
}
 
// Driver code
public static void main(String[] args)
{
    String str
        = "675987890789756545689070986776987";
  
    // Function to print final digit
    System.out.print(GetDigitalRoot(str));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 programm to print the digital
# root of a given very large number
 
# Function to convert given
# sum into string
def convertToString(sum):
    str1 = ""
 
    # Loop to extract digit one by one
    # from the given sum and concatinate
    # into the string
    while (sum):
 
        # Type casting for concatination
        str1 = str1 + chr((sum % 10) + ord('0'))
        sum = sum // 10
 
    # Return converted string
    return str1
 
# Function to get individual digit
# sum from string
def GetIndividulaDigitSum(str1, len1):
    sum = 0
    # Loop to get individual digit sum
    for i in range(len1):
        sum = sum + ord(str1[i]) - ord('0')
 
    # Function call to convert
    # sum into string
    return convertToString(sum)
 
# Function to calculate the digital
# root of a very large number
def GetDigitalRoot(str1):
    # Base condition
    if (len(str1) == 1):
        return ord(str1[0] ) - ord('0')
 
    # Function call to get
    # individual digit sum
    str1 = GetIndividulaDigitSum(str1,len(str1))
    # Recursive function to get digital
    # root of a very large number
    return GetDigitalRoot(str1)
 
if __name__ == '__main__':
    str1 = "675987890789756545689070986776987"
 
    # Function to print final digit
    print(GetDigitalRoot(str1))
 
# This code is contributed by Surendra_Gangwar


C#
// C# programm to print the digital
// root of a given very large number
using System;
class GFG{
  
// Function to convert given
// sum into String
static String convertToString(int sum)
{
  String str = "";
 
  // Loop to extract digit one by one
  // from the given sum and concatinate
  // into the String
  while (sum > 0)
  {
    // Type casting for concatination
    str = str + (char)((sum % 10) + '0');
    sum = sum / 10;
  }
 
  // Return converted String
  return str;
}
  
// Function to get individual digit
// sum from String
static String GetIndividulaDigitSum(String str,
                                    int len)
{
  int sum = 0;
 
  // Loop to get individual digit sum
  for (int i = 0; i < len; i++)
  {
    sum = sum + str[i] - '0';
  }
 
  // Function call to convert
  // sum into String
  return convertToString(sum);
}
  
// Function to calculate the digital
// root of a very large number
static int GetDigitalRoot(String str)
{
  // Base condition
  if (str.Length == 1)
  {
    return str[0] - '0';
  }
 
  // Function call to get
  // individual digit sum
  str = GetIndividulaDigitSum(str,
                              str.Length);
 
  // Recursive function to get digital
  // root of a very large number
  return GetDigitalRoot(str);
}
 
// Driver code
public static void Main(String[] args)
{
  String str =
         "675987890789756545689070986776987";
 
  // Function to print readonly digit
  Console.Write(GetDigitalRoot(str));
}
}
 
// This code is contributed by Rajput-Ji


输出:
5