Digital Root of a given large number using Recursion
Given a large number num in the form of string with length as N, the task is to find its digital root.
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.
Examples:
Input: num = 675987890789756545689070986776987
Output: 5
Explanation:
Sum of individual digit of the above number = 212
Sum of individual digit of 212 = 5
So the Digital root is 5
Input: num = 876598758938317432685778263
Output: 2
Explanation:
Sum of individual digit of the above number = 155
Sum of individual digit of 155 = 11
Sum of individual digit of 11 = 2
So the Digital root is 2
Approach:
Follow the below steps to solve the problem:
- Find out all the digits of a number.
- Add all the number one by one.
- If the final sum contains more than one digit, Call the recursive function again to make it a single digit.
- The result obtained in the single-digit is the Digital Root of the number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string convertToString( int sum)
{
string str = "" ;
while (sum) {
str = str + ( char )((sum % 10) + '0' );
sum = sum / 10;
}
return str;
}
string GetIndividulaDigitSum(string str, int len)
{
int sum = 0;
for ( int i = 0; i < len; i++) {
sum = sum + str[i] - '0' ;
}
return convertToString(sum);
}
int GetDigitalRoot(string str)
{
if (str.length() == 1) {
return str[0] - '0' ;
}
str = GetIndividulaDigitSum(str, str.length());
return GetDigitalRoot(str);
}
int main()
{
string str = "675987890789756545689070986776987" ;
cout << GetDigitalRoot(str);
}
|
Java
import java.util.*;
import java.io.*;
class GFG {
static String convertToString( int sum)
{
String str = "" ;
while (sum > 0 ) {
str = str + ( char )((sum % 10 ) + '0' );
sum = sum / 10 ;
}
return str;
}
static String GetIndividulaDigitSum(String str, int len)
{
int sum = 0 ;
for ( int i = 0 ; i < len; i++) {
sum = sum + str.charAt(i) - '0' ;
}
return convertToString(sum);
}
static int GetDigitalRoot(String str)
{
if (str.length() == 1 ) {
return str.charAt( 0 ) - '0' ;
}
str = GetIndividulaDigitSum(str, str.length());
return GetDigitalRoot(str);
}
public static void main(String[] args)
{
String str = "675987890789756545689070986776987" ;
System.out.print(GetDigitalRoot(str));
}
}
|
Python3
def convertToString( sum ):
str1 = ""
while ( sum ):
str1 = str1 + chr (( sum % 10 ) + ord ( '0' ))
sum = sum / / 10
return str1
def GetIndividulaDigitSum(str1, len1):
sum = 0
for i in range (len1):
sum = sum + ord (str1[i]) - ord ( '0' )
return convertToString( sum )
def GetDigitalRoot(str1):
if ( len (str1) = = 1 ):
return ord (str1[ 0 ]) - ord ( '0' )
str1 = GetIndividulaDigitSum(str1, len (str1))
return GetDigitalRoot(str1)
if __name__ = = '__main__' :
str1 = "675987890789756545689070986776987"
print (GetDigitalRoot(str1))
|
C#
using System;
class GFG {
static String convertToString( int sum)
{
String str = "" ;
while (sum > 0) {
str = str + ( char )((sum % 10) + '0' );
sum = sum / 10;
}
return str;
}
static String GetIndividulaDigitSum(String str, int len)
{
int sum = 0;
for ( int i = 0; i < len; i++) {
sum = sum + str[i] - '0' ;
}
return convertToString(sum);
}
static int GetDigitalRoot(String str)
{
if (str.Length == 1) {
return str[0] - '0' ;
}
str = GetIndividulaDigitSum(str, str.Length);
return GetDigitalRoot(str);
}
public static void Main(String[] args)
{
String str = "675987890789756545689070986776987" ;
Console.Write(GetDigitalRoot(str));
}
}
|
Javascript
<script>
function convertToString(sum)
{
let str = "" ;
while (sum > 0)
{
str = str + String.fromCharCode((sum % 10) + '0' .charCodeAt());
sum = parseInt(sum / 10, 10);
}
return str;
}
function GetIndividulaDigitSum(str, len)
{
let sum = 0;
for (let i = 0; i < len; i++) {
sum = sum + str[i].charCodeAt() - '0' .charCodeAt();
}
return convertToString(sum);
}
function GetDigitalRoot(str)
{
if (str.length == 1) {
return (str[0].charCodeAt() - '0' .charCodeAt());
}
str = GetIndividulaDigitSum(str, str.length);
return GetDigitalRoot(str);
}
let str = "675987890789756545689070986776987" ;
document.write(GetDigitalRoot(str));
</script>
|
Time Complexity: O(N), which can be computed as follows:
- O(log N) time to find the sum of digits of num first time, where N is the count of digits initially.
- O(log log N) time to find the sum of digits next time
- O(log log log N) time to find the sum of digits next time
- … and so on.
Auxiliary Space: O(1), since the sum stored in the string will not exceed 3 digits for N ? 105
Last Updated :
20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...