Digital Root (repeated digital sum) of the given large integer
Last Updated :
13 Dec, 2022
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.
Given a number, the task is to find its digital root. The input number may be large and it may not be possible to store even if we use long long int.
Asked in ACM-ICPC
Examples :
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
We have discussed a solution for numbers that can fit in long long it in the below post.
Finding the sum of digits of a number until the sum becomes a single digit
In this post, similar approaches are discussed for large numbers.
Method 1
Find the Digital root of 65785412
Steps:
- Find out all the digits of a number
- Add all the number one by one
- If the final sum is double-digit, add again to make it single digit
- The result obtained in a single digit is the digital root of a number
Example:
Input: 65785412
Find Digital root: (6 + 5 + 7 + 8 + 5 + 4 + 1 + 2) = 38 => 11 => (1 + 1) = 2
Output: 2
Method 2
The idea is based on the fact that for a non-zero number num, the digital root is 9 if the number is divisible by 9, else the digital root is (sum of digits of num) % 9. (Please see http://www.sjsu.edu/faculty/watkins/Digitsum0.htm for details)
Find the digital root of 65785412
Steps:
- Sum of digits = 6 + 5 + 7 + 8 + 5 + 4 + 1 + 2 = 38
- Since 38 is not a multiple of 9, the digital root is 38 % 9 = 2.
C++
#include<bits/stdc++.h>
using namespace std;
int digitalRoot(string num)
{
if (num.compare( "0" ) == 0)
return 0;
int ans = 0;
for ( int i=0; i<num.length(); i++)
ans = (ans + num[i]- '0' ) % 9;
return (ans == 0)? 9 : ans % 9;
}
int main()
{
string num = "65785412" ;
cout<< digitalRoot(num) <<endl;
return 0;
}
|
Java
import java.util.*;
public class GfG {
static int digroot( int n)
{
int root = 0 ;
while (n > 0 || root > 9 )
{
if (n == 0 ) {
n = root;
root = 0 ;
}
root += n % 10 ;
n /= 10 ;
}
return root;
}
public static void main(String argc[])
{
int n = 65785412 ;
System.out.println(digroot(n));
}
}
|
Python3
import math
def digitalRoot(num):
if (num = = "0" ):
return 0
ans = 0
for i in range ( 0 , len (num)):
ans = (ans + int (num[i])) % 9
if (ans = = 0 ):
return 9
else :
return ans % 9
num = "65785412"
print (digitalRoot(num))
|
C#
using System;
class GfG {
static int digroot( int n)
{
int root = 0;
while (n > 0 || root > 9)
{
if (n == 0) {
n = root;
root = 0;
}
root += n % 10;
n /= 10;
}
return root;
}
public static void Main()
{
int n = 65785412;
Console.Write(digroot(n));
}
}
|
PHP
<?php
function digroot( $n )
{
$root = 0;
while ( $n > 0 || $root > 9)
{
if ( $n == 0)
{
$n = $root ;
$root = 0;
}
$root += $n % 10;
$n /= 10;
}
return $root ;
}
$num = 65785412;
echo digroot( $num );
?>
|
Javascript
<script>
function digroot(n)
{
let root = 0;
while (n > 0 || root > 9)
{
if (n == 0) {
n = root;
root = 0;
}
root += n % 10;
n = parseInt(n / 10, 10);
}
return root;
}
let n = 65785412;
document.write(digroot(n));
</script>
|
Time Complexity: O(n), where n is the size of the given string num
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...