Given a number n, find the digital root of n. Digital Root of a number is the recursive sum of its digits until we get a single digit number.
Examples :
Input: n = 1
Output: 1
Explanation: Digital root of 1 is 1.
Input: n = 99999
Output: 9
Explanation: The sum of digits of 99999 is 45 which is not a single digit number, hence the sum of digits of 45 is 9 which is a single digit number.
Table of Content
[Naive Approach] Repetitively Adding Digits - O(d) Time and O(1) Space
The approach computes the digital root of a number by repeatedly summing its digits until a single digit value is obtained. It starts by adding all the digits of the given number; if the result is a single digit, it is returned. Otherwise, the same process is repeated on the new sum until a single digit result is reached.
#include <iostream>
using namespace std;
int digitalRoot(int n)
{
int res = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || res > 9)
{
// If n becomes 0, reset it to res
// and start a new iteration.
if (n == 0)
{
n = res;
res = 0;
}
res += n % 10;
n /= 10;
}
return res;
}
int main()
{
int n = 99999;
cout << digitalRoot(n);
return 0;
}
import java.util.Scanner;
public class GFG {
public static int digitalRoot(int n)
{
int res = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || res > 9) {
// If n becomes 0, reset it to res
// and start a new iteration.
if (n == 0) {
n = res;
res = 0;
}
res += n % 10;
n /= 10;
}
return res;
}
public static void main(String[] args)
{
int n = 99999;
System.out.println(digitalRoot(n));
}
}
def digitalRoot(n):
res = 0
# Repetitively calculate sum until
# it becomes single digit
while n > 0 or res > 9:
# If n becomes 0, reset it to res
# and start a new iteration.
if n == 0:
n = res
res = 0
res += n % 10
n //= 10
return res
if __name__ == "__main__":
n = 99999
print(digitalRoot(n))
public class GFG {
public int digitalRoot(int n)
{
int res = 0;
while (n > 0 || res > 9) {
if (n == 0) {
n = res;
res = 0;
}
res += n % 10;
n /= 10;
}
return res;
}
public static int Main()
{
int n = 99999;
Console.WriteLine(digitalRoot(n));
return 0;
}
}
function digitalRoot(n)
{
let res = 0;
// Repetitively calculate sum until
// it becomes single digit
while (n > 0 || res > 9) {
// If n becomes 0, reset it to res
// and start a new iteration.
if (n === 0) {
n = res;
res = 0;
}
res += n % 10;
n = Math.floor(n / 10);
}
return res;
}
// Driver Code
let n = 99999;
console.log(digitalRoot(n));
Output
9
Time Complexity: O(d), where d is the number of digits in n.
Auxiliary Space: O(1)
[Expected Approach] Using Mathematical Formula - O(1) Time O(1) Space
This approach computes the digital root using a mathematical property instead of repeatedly summing the digits. If the number is 0, the digital root is 0. Otherwise, the digital root is equal to the remainder when the number is divided by 9; however, if the remainder is 0, the digital root is 9 because every non-zero multiple of 9 has a digital root of 9.
- A number and the sum of its digits always leave the same remainder when divided by 9. For example,
572→5 + 7 + 2 = 14→1 + 4 = 5, and both572and5have the same remainder modulo9. - Repeatedly summing the digits (finding the digital root) does not change this remainder.
- Therefore, for any non-zero number, the digital root is simply
n % 9. - The only exception is when
nis a non-zero multiple of9. In this case,n % 9is0, but the digital root is9, so the function returns9. - If
nis0, its digital root is also0, which is handled separately.
#include <iostream>
using namespace std;
int digitalRoot(int n)
{
// If given number is zero its
// digit sum will be zero only
if (n == 0)
return 0;
// If result of modulo operation is
// zero then, the digit sum is 9
if (n % 9 == 0)
return 9;
return (n % 9);
}
int main()
{
int n = 99999;
cout << digitalRoot(n);
return 0;
}
#include <stdio.h>
int digitalRoot(int n)
{
// If given number is zero its
// digit sum will be zero only
if (n == 0)
return 0;
// If result of modulo operation is
// zero then, the digit sum is 9
if (n % 9 == 0)
return 9;
return (n % 9);
}
int main()
{
int n = 99999;
printf("%d", digitalRoot(n));
return 0;
}
public class GFG {
public static int digitalRoot(int n)
{
// If given number is zero its
// digit sum will be zero only
if (n == 0)
return 0;
// If result of modulo operation is
// zero then, the digit sum is 9
if (n % 9 == 0)
return 9;
return (n % 9);
}
public static void main(String[] args)
{
int n = 99999;
System.out.println(digitalRoot(n));
}
}
def digitalRoot(n):
# If given number is zero its
# digit sum will be zero only
if n == 0:
return 0
# If result of modulo operation is
# zero then, the digit sum is 9
if n % 9 == 0:
return 9
return n % 9
if __name__ == "__main__":
n = 99999
print(digitalRoot(n))
public class GFG {
public int digitalRoot(int n)
{
return 1 + (n - 1) % 9;
}
public static void Main()
{
int n = 99999;
Console.WriteLine(digitalRoot(n));
}
}
function digitalRoot(n)
{
// If given number is zero its
// digit sum will be zero only
if (n === 0)
return 0;
// If result of modulo operation is
// zero then, the digit sum is 9
if (n % 9 === 0)
return 9;
return (n % 9);
}
// Driver code
let n = 99999;
console.log(digitalRoot(n));
Output
9